Skip to content

Instantly share code, notes, and snippets.

@mschauer
Last active October 2, 2020 17:20
Show Gist options
  • Save mschauer/c8e2923ef8574eac761ca59f734ab83f to your computer and use it in GitHub Desktop.
Save mschauer/c8e2923ef8574eac761ca59f734ab83f to your computer and use it in GitHub Desktop.
Stochastic process example with Girsanov likelihood
struct StoProMeasure
θ::Float64
end
struct WienerMeasure
end
function logdensity(P::StoProMeasure, tr, ::WienerMeasure)
som = 0.0
t, x = tr
for i in 1:length(x)-1
b = - x[i] + P.θ*atan(x[i])
som += b*(x[i+1] - x[i] - 0.5 * b * (t[i+1]-t[i]))
end
som
end
function sample(P::StoProMeasure, T, dt)
x = 0.0
t = 0.0
ts = [t]
xs = [x]
for i in 1:round(Int,T/dt)
x += - x*dt + P.θ*atan(x)*dt + sqrt(dt)*randn()
t += dt
push!(ts, t)
push!(xs, x)
end
tr = ts, xs
end
tr = sample(StoProMeasure(2.0), 1000, 0.1) # true parameter 2.0
loglikelihood = [x => logdensity(StoProMeasure(x),tr, WienerMeasure()) for x in -1.5:0.1:2.5]
_, i = findmax(last.(loglikelihood))
θml = loglikelihood[i][1]
println("Maximum likelihood estimate of θ: ", θml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment