-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpoisson-mle.e
82 lines (57 loc) · 1.82 KB
/
poisson-mle.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
new;
library maxlikmt;
// Single data set example
y = { 5, 0, 1, 1, 0, 3, 2, 3, 4, 1 };
// Theta vector
theta_plot = seqa(0.1, 0.1, 3.5/0.1);
// Likelihood function
likelihood = (exp(-10*theta_plot).*theta_plot^20)/207360;
// Plot the likelihood function
struct plotControl myPlot;
myPlot = plotGetDefaults("xy");
// Set title on the graph
plotSetTitle(&myPlot, "Likelihood and Log-Likelihood Function of Poisson Function", "Arial", 16);
// Set the first curve, 'y1' to the left Y-axis
// Set the second curve 'y2' to the right Y-axis
string which = { "left", "right" };
plotSetWhichYAxis(&myPlot, which);
// Set line style
plotSetLineStyle(&myPlot, 1|2);
// Set up the tic style
plotSetTicLabelFont(&myPlot, "Arial", 14);
// Turn on grid
plotSetGrid(&myPlot, "on");
// Turn on the legend
plotSetLegend(&myPlot, "Likelihood"$|"Log-Likelihood", "top left");
myPlot.Legend.fontSize = 12;
myPlot.Legend.font = "Arial";
plotCanvasSize("px", 1200|600);
// Set line colors
plotXY(myPlot, theta_plot, likelihood/1E-7~ln(likelihood/1E-7));
// Find maximum likelihood estimator
theta_start = 1.2;
// Control structure
//Declare 'ctl' to be a maxlikmtControl struct
struct maxlikmtControl ctl;
//Fill 'ctl' with default settings
ctl = maxlikmtControlCreate();
ctl.numObs = rows(y);
//Declare 'out' to be a maxlikmtResults
//structure to hold the estimation results
struct maxlikmtResults out;
//Perform estimation and print report
out = maxlikmtprt(maxlikmt(&lpsn, theta_start, y, ctl));
print "Estimated theta :";
out.par.obj.m;
//Log-likelihood procedure
proc lpsn(theta, y, ind);
local n;
struct modelResults mm;
n = rows(y);
//If the first element of 'ind' is non-zero
//compute the function value
if ind[1];
mm.function = -n*theta + sumc(y)*ln(theta) - ln(207360);
endif;
retp(mm);
endp;