Skip to content

Commit 48d305d

Browse files
committed
Corrections to lsm
1 parent 268e985 commit 48d305d

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/pricing_methods/least_squares_montecarlo.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ end
3939
Extracts the simulated spot paths from a `Vector` of state vectors. Returns a matrix of size (nsteps, npaths).
4040
Each column corresponds to a single simulation path.
4141
"""
42-
function extract_spot_grid(sol)
43-
# Each path is a Vector of state vectors; we extract first component at each time step
44-
return hcat([getindex.(s.u, 1) for s in sol.u]...) # size: (nsteps, npaths)
42+
function extract_spot_grid(sol::CustomEnsembleSolution)
43+
# Each s is a solution in sol.solutions, where s.u is a vector of states
44+
return hcat([getindex.(s.u, 1) for s in sol.solutions]...) # size: (nsteps, npaths)
4545
end
4646

47+
4748
function solve(
4849
prob::PricingProblem{VanillaOption{American, C, Spot}, I},
4950
method::LSM

src/pricing_methods/montecarlo.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ struct BlackScholesExact <: SimulationStrategy
3333
trajectories
3434
steps
3535
kwargs::NamedTuple
36+
seeds::Union{Nothing, Vector{Int}}
3637
end
3738

3839
EulerMaruyama(trajectories, steps; kwargs...) = EulerMaruyama(trajectories, steps, (; kwargs...))
39-
BlackScholesExact(trajectories, steps=1; kwargs...) = BlackScholesExact(trajectories, steps, (; kwargs...))
40+
BlackScholesExact(trajectories, steps=1,seeds=nothing; kwargs...) = BlackScholesExact(trajectories, steps, (; kwargs...), seeds)
4041

4142
# ------------------ SDE Problem Builders ------------------
4243

@@ -77,7 +78,11 @@ end
7778

7879
function montecarlo_solution(problem::Union{NoiseProblem, SDEProblem}, strategy::S) where {S <: SimulationStrategy}
7980
dt = problem.tspan[end] / strategy.steps
80-
seeds = Base.rand(1:10^9, strategy.trajectories) #generate seeds
81+
N = strategy.trajectories
82+
83+
seeds = strategy isa BlackScholesExact && strategy.seeds !== nothing ? strategy.seeds :
84+
Base.rand(1_000_000_000:2_000_000_000, N)
85+
8186
modify = (p, seed, _) -> remake(p; seed=seed)
8287
custom_prob = CustomEnsembleProblem(problem, collect(seeds), modify)
8388
return solve_custom_ensemble(custom_prob; dt=dt)

test/least_squares_montecarlo.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
dynamics = LognormalDynamics()
1616
trajectories = 1
1717
steps = 100
18-
strategy = BlackScholesExact(trajectories, steps; seed=42) # Deterministic seed
18+
seeds = [42]
19+
strategy = BlackScholesExact(trajectories, steps, seeds) # Deterministic seed
1920
degree = 3
2021
method = LSM(dynamics, strategy, degree)
2122

0 commit comments

Comments
 (0)