# Maximum Likelihood estimation

I am trying to implement this function and solve for parameters: Now as many can see it is a maximum likelihood estimation: I have this data:

data = RandomFunction[
OrnsteinUhlenbeckProcess[0, 4, 5, 13], {0, 120, 1}][][[1, 1]];


Now I want to find these parameters given I have this MLF, as above:

tdata = Range[0, 120];

func = -(n/2) Log[σ^2/(2 η)] -
1/2 Sum[Log[
1 - E^(-2 η (Subscript[t, i] - Subscript[t, i - 1]))], {i,
1, n}] - η/σ^2 Sum[(Subscript[x, Subscript[t,
i]] - μ - (Subscript[x, Subscript[t,
i - 1]] - μ) E^(-(η (Subscript[t, i] - Subscript[t,
i - 1]))))^2/(1 -
E^(-(2 η (Subscript[t, i] - Subscript[t, i - 1])))), {i, 1,
n}]

xis = Table[Subscript[x, Subscript[t, i]], {i, 0, Length[data] - 1}];
ts = Thread[Subscript[t, Range[0, Length[data] - 1]]];
ff = (func /. n -> Length[data] - 1);
ffun = ff /. xrules /. trules // FullSimplify;
f1 = D[file, μ] == 0 // FullSimplify
f2 = D[file, η] == 0 // FullSimplify
f3 = D[file, σ] == 0 // FullSimplify


Now, if I have done anything wrong in how to state the problem in mathematica, please let me know.

No there are to possibilities here, it might be that I have stated the problem in Mathematica wrongly or I simply cannot figure out how to use mathematica properly to solve for these values. I was expected a numerical estimation method.

Could someone please take the time to help me, as I believe this would give me the opportunity to understand how to state more complex problems in mathematica, and how to properly use the software to solve different challenges. If there is anything that is required for me to improve this question please let me know.

Thank you.

• I would try to avoid subscripts. Where is file defined? – gwr Nov 4 '16 at 7:48

Following @gwr 's suggestion to lose the subscripts, here is what you might consider:

SeedRandom
n = 120
x = RandomFunction[OrnsteinUhlenbeckProcess[0, 4, 5, 13], {0, n, 1}][][[1, 1]];
t = Range[0, n];

(* Log likelihood *)
logL = Simplify[-(n/2) Log[σ^2/(2 η)] -
1/2 Sum[Log[1 - E^(-2 η (t[[i]] - t[[i - 1]]))], {i, 2, n + 1}] -
η/σ^2 Sum[(x[[i]] - μ - (x[[i - 1]] - μ) E^(-(η (t[[i]] - t[[i - 1]]))))^2/
(1 - E^(-(2 η (t[[i]] - t[[i - 1]])))), {i, 2, n + 1}]]

(* Maximum likelihood estimates *)
sol = FindMaximum[logL, {μ, η, σ}]
(* {-85.48303148676939,{μ -> -0.1892112456802267,η -> 2.551881067839081,
σ -> 2.8021689808948893}} *)

(* Estimate asymptotic covariance matrix *)
dlogL = Simplify[D[logL, {{μ, η, σ}}]];
cov = -(Inverse[D[dlogL, {{μ, η, σ}}]]) /. sol[]
(* {{0.015050491546966437,0.006627137383154969,0.0035250798446800325},
{0.006627137383154969,0.7044551717696488,0.37471091753243285},
{0.003525079844680033,0.37471091753243296,0.23203199652508358}} *)

(* Standard errors of estimates *)
seμ = cov[[1, 1]]^0.5
(* 0.1226804448433671 *)
seη = cov[[2, 2]]^0.5
(* 0.8393182779909233 *)
seσ = cov[[3, 3]]^0.5
(* 0.4816969965913049 *)


Something still might be off because not all random number seeds result in reasonable estimates.

Here is an other approach but still with bad results --- I have looked some years ago to the estimation of this kind of sde and I think it is normal

 step := 0.01
tt := 120
data = RandomFunction[
OrnsteinUhlenbeckProcess[0, 4, 5, 13], {0, tt, step}][][[1,
1]];
\[DoubleStruckCapitalL][μ_, η_, σ_] := -((
Length[data] - 1)/2) (Log[σ^2/(2 η)] +
Log[1 - E^(-2 η tt)]) - η/σ^2 Total[((Drop[data,
1] - μ) - (Drop[data, -1] - μ) E^(-η tt))^2/(
1 - E^(-2 η tt))]
FindRoot[{D[\[DoubleStruckCapitalL][μ, η, σ], μ] \
== 0, D[\[DoubleStruckCapitalL][μ, η, σ], σ] ==
0, D[\[DoubleStruckCapitalL][μ, η, σ], η] ==
0}, {μ, .1}, {σ, 3}, {η, 3}]


As the interval between two times is a constant in your simulation it suffice to define the variable step. FindRoot need some guess points to start. On my computer with step = 0.01 here are the results

{μ -> 0.0865566, σ -> 3.77693, η -> 3.77693}

but even with an exact guess there are discrepancies. Have look to this paper http://www.investmentscience.com/Content/howtoArticles/MLE_for_OR_mean_reverting.pdf