Skip to content

Commit c783e05

Browse files
committed
Correcting sampling of log_price for heston
1 parent f41cfd5 commit c783e05

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/distributions/heston.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ struct LogHestonDistribution{TS0,TV0,Tκ,Tθ,Tσ,Tρ,Tr,T} <: ContinuousMultivar
110110
T::T
111111
end
112112

113+
114+
import Distributions: length
115+
116+
function Base.length(::LogHestonDistribution)
117+
return 2 # The Heston model is a 2-dimensional process (Asset Price, Variance)
118+
end
119+
113120
"""
114121
sample_V_T(rng, d::LogHestonDistribution)
115122
@@ -251,6 +258,23 @@ function rand(rng::AbstractRNG, d::LogHestonDistribution; antithetic = false, kw
251258
return [log_S_T, V_T]
252259
end
253260

261+
function Distributions.rand!(
262+
rng::AbstractRNG,
263+
d::LogHestonDistribution,
264+
x::AbstractVector{<:Real};
265+
antithetic = false,
266+
kwargs...
267+
)
268+
# Call your allocating rand function
269+
sample = rand(rng, d; antithetic=antithetic, kwargs...)
270+
271+
# Copy the result into the pre-allocated vector x
272+
x[1] = sample[1]
273+
x[2] = sample[2]
274+
275+
return x
276+
end
277+
254278
function sample_log_S_T(
255279
V_T,
256280
integral_V,

src/pricing_methods/montecarlo.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,15 @@ end
401401
402402
Draws a specified number of samples from a given probability distribution (`law`).
403403
This is primarily used by the `ExactSimulation` strategy.
404+
It assumes that the first component of the multi-dimensional law correspond with the log-price.
404405
"""
405406
function log_sample(rng, law::ContinuousUnivariateDistribution, trajectories)
406-
log_sample = Distributions.rand(rng, law, trajectories)
407-
return log_sample
407+
return Distributions.rand(rng, law, trajectories)
408408
end
409409

410410
function log_sample(rng, law::ContinuousMultivariateDistribution, trajectories)
411-
print((rng, law, trajectories))
412-
log_sample, _ = Distributions.rand(rng, law, trajectories)
413-
return log_sample
411+
X = Distributions.rand(rng, law, trajectories) # shape: (n, trajectories)
412+
return view(X, 1, :) # returns a lightweight view of the first row (log-price)
414413
end
415414

416415

@@ -479,9 +478,10 @@ function solve(
479478

480479
# Get samples using the dispatched helper
481480
sample_at_expiry = get_final_samples(prob, method)
482-
481+
@show typeof(sample_at_expiry)
483482
# Common logic for pricing
484483
payoffs = reduce_payoffs(sample_at_expiry, prob.payoff, config.variance_reduction)
484+
@show typeof(payoffs)
485485
discount = df(prob.market_inputs.rate, prob.payoff.expiry)
486486
price = discount * mean(payoffs)
487487

0 commit comments

Comments
 (0)