The price of a commodity can be described by the Schwartz mean reverting SDE
dS=α(μ−logS)Sdt+σSdW where W is the standard Brownian motion and alpha is the strength of mean reversion.
From it is possible to derive the PDE for the price of the forward contract having the commodity as underlying asset
(1)∂t∂F+α(μ−αμ−r−logS)S∂S∂F+21σ2S2∂S2∂2F=0 whose analytical solution is F(S,τ)=exp(e−ατlogS+(μ−2ασ2−αμ−r)(1−e−ατ)+4ασ2(1−e−2ατ)) where τ=T−t is the time to expiry (T is the time of delivery/expiry).
Using Euler explicit method, i.e. forward difference on ∂t∂F and central difference on ∂S∂F and ∂S2∂2F, we can discretize eq (1) as
Fin+1=aFi−1n+bFin+cFi+1n where a=2ΔSSΔt(αμ−(μ−r)−αlog(S)−ΔSσ2S), b=(1−σ2S2ΔS2Δt) and c=2ΔSSΔt(−αμ+(μ−r)+αlog(S)−ΔSσ2S).
To run Explicit Euler we have then to choose the number N of time steps, which also set Δt since Δt=T/N, and the size of ΔS. Since the finite difference scheme divides the cartesian plane (time is the X-axis, and spot price is the Y-axis) in a grid, if we take more time steps and/or smaller ΔS the grid will be more dense and the accuracy of the approximation should increase (provided that 0<(ΔS)2Δt<21).
However, the code I wrote using the equations above doesn't work in this way, in particular to have big accuracy I have to use a large ΔS, and the accuracy decreses when using small values of ΔS to point that by using ΔS=0.1 the relative error explodes to 10165 as you can see in the image below (dS stands for ΔS).
If you want you can inspect the matlab code (see attachment), I'm sure there is an error somewhere but cannot find it. I think one problem is the fact that I'm using real data (vector spot_prices in the code) and so I cannot discretize along the y-axis, right?
dS=α(μ−logS)Sdt+σSdW where W is the standard Brownian motion and alpha is the strength of mean reversion.
From it is possible to derive the PDE for the price of the forward contract having the commodity as underlying asset
(1)∂t∂F+α(μ−αμ−r−logS)S∂S∂F+21σ2S2∂S2∂2F=0 whose analytical solution is F(S,τ)=exp(e−ατlogS+(μ−2ασ2−αμ−r)(1−e−ατ)+4ασ2(1−e−2ατ)) where τ=T−t is the time to expiry (T is the time of delivery/expiry).
Using Euler explicit method, i.e. forward difference on ∂t∂F and central difference on ∂S∂F and ∂S2∂2F, we can discretize eq (1) as
Fin+1=aFi−1n+bFin+cFi+1n where a=2ΔSSΔt(αμ−(μ−r)−αlog(S)−ΔSσ2S), b=(1−σ2S2ΔS2Δt) and c=2ΔSSΔt(−αμ+(μ−r)+αlog(S)−ΔSσ2S).
To run Explicit Euler we have then to choose the number N of time steps, which also set Δt since Δt=T/N, and the size of ΔS. Since the finite difference scheme divides the cartesian plane (time is the X-axis, and spot price is the Y-axis) in a grid, if we take more time steps and/or smaller ΔS the grid will be more dense and the accuracy of the approximation should increase (provided that 0<(ΔS)2Δt<21).
However, the code I wrote using the equations above doesn't work in this way, in particular to have big accuracy I have to use a large ΔS, and the accuracy decreses when using small values of ΔS to point that by using ΔS=0.1 the relative error explodes to 10165 as you can see in the image below (dS stands for ΔS).
If you want you can inspect the matlab code (see attachment), I'm sure there is an error somewhere but cannot find it. I think one problem is the fact that I'm using real data (vector spot_prices in the code) and so I cannot discretize along the y-axis, right?
