-
Notifications
You must be signed in to change notification settings - Fork 0
/
estimation.py
executable file
·238 lines (212 loc) · 8.09 KB
/
estimation.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
from mpl_toolkits.mplot3d import Axes3D
img_format = 'png'
# Question 1: Probability and Parameter Estimation
#question 1.1
#postcondition: gaussian.[img_format] now has the 3 gaussian plots
# N(-1,1),N(0,2),N(2,3)
def gaussian_plots():
param = [(-1,1), (0,2), (2,3)] #standard deviation values
for i, params in enumerate(param):
# k gaussians have been plotted, k <= len(param)
# N(m,s) for m,s in param[k] is next to be plotted
mean, std_dev = params
range = np.arange(-4*std_dev + mean, 4*std_dev + mean, 0.001)
plt_label = '$(\mu, \sigma) = (%d,%d)$' % (mean, std_dev)
plt.plot(range, norm.pdf(range,mean,std_dev), label=plt_label)
plt.legend()
plt.savefig('gaussian.%s' % img_format, format=img_format)
plt.close()
#question 1.2
#returns: N random values from the normal distribution with
# mu=(1,1), sigma = ((0.3,0.2),(0.2,0.2))
def data(N):
mean = [1,1]
cov = [[0.3,0.2],[0.2,0.2]]
y = np.random.multivariate_normal(mean,cov,N)
return y
def mean(data):
return np.mean(np.transpose(data), axis=1)
def covariance(data):
return np.cov(np.transpose(data))
# question 1.3
#pre : sample is a set of points generated from the data(N) function
#post: scatter.[img_format] has a scatterplot of the sample points,
# along with points which mark the sample and true mean.
def estimate_params(sample):
x,y = zip(*sample)
sample_mean = mean(sample)
mean_x, mean_y = sample_mean
sample_cov = covariance(sample)
plt.scatter(x,y)
plt.scatter(1,1,s=50, c='red',
marker='x', label='$\mu$') #true mean plotted red
plt.scatter(mean_x,mean_y,s=50, c='green',
marker='x', label='$\mu_{ML}$') #sample mean plotted green
plt.legend()
plt.savefig('scatter.%s' % img_format, format=img_format) #plotting the points
plt.close()
return (sample_mean, sample_cov)
# Question 3: Non-parametric estimation - histograms
# question 1.5
#precondition: data is generated from data(N), bins is a natural number
#postcondition: Two histograms have been plotted, one for the
# marginal probability density of each coordinate
def histogram_plot(data, bins):
x1,x2 = zip(*data)
plt.figure(1)
plt.hist(x1, bins=bins, label='$p(x_1)$') #histogram for p(x1)
plt.annotate('#bins = %d' % bins, xy=(0.01, 0.95), xycoords='axes fraction')
plt.legend()
plt.savefig('histogram1.%s' % img_format, format=img_format)
plt.figure(2)
plt.hist(x2, bins=bins,label='$p(x_2)$') #histogram for p(x2)
plt.annotate('#bins = %d' % bins, xy=(0.01, 0.95), xycoords='axes fraction')
plt.legend()
plt.savefig('histogram2.%s' % img_format, format=img_format)
plt.close()
# question 1.6
#precondition: data is generated from data(N), bins is a natural number
#postcondition: Two histograms have been plotted, one for the marginal
# probability density of each coordinate
def histogram_and_analytical_plot(data, bins):
x1,_ = zip(*data)
mean = 1 #mean for analytical solution
std_dev = 0.3**(0.5) #stdev for analytical solution
range = np.arange(-3.5*std_dev + mean, 3.5*std_dev + mean, 0.001)
plt.hist(x1, bins=bins, normed=True, label='$p(x_1)$')
plt.plot(range, norm.pdf(range,mean,std_dev), linewidth=2,
label='$\mathcal{N}(x|1,0.3)$')
plt.legend()
plt.savefig('hist_and_analytical.%s' % img_format, format=img_format)
plt.close()
# question 1.7
#precondition: data contains N (x,y) tuples, generated from the data(N) function
# with
#postcondition: 3 2d-histograms have been plotted for x,y in data
def histogram_plot_2d(data):
x, y = zip(*data)
plt.figure(1)
plt.hist2d(x,y,bins=10) #10x10 plot for data
plt.annotate('#bins = 10', xy=(0.01, 0.95), xycoords='axes fraction',
color='white')
plt.savefig('histogram3d_10bins.%s' % img_format, format=img_format)
plt.figure(2)
plt.hist2d(x,y,bins=15) #15x15 plot for data
plt.annotate('#bins = 15', xy=(0.01, 0.95), xycoords='axes fraction',
color='white')
plt.savefig('histogram3d_15bins.%s' % img_format, format=img_format)
plt.figure(3)
plt.hist2d(x,y,bins=20) #20x20 plot for data
plt.annotate('#bins = 20', xy=(0.01, 0.95), xycoords='axes fraction',
color='white')
plt.savefig('histogram3d_20bins.%s' % img_format, format=img_format)
plt.close()
if __name__ == "__main__":
data100 = data(100)
data1000 = data(1000)
data10000 = data(10000)
histogram_plot_2d(data10000)
"""data = np.array([[ 0.72193061, 0.55650806],
[ 0.71297482, 1.07638665],
[ 0.0092223 , 0.31207955],
[ 1.26288325, 0.53376191],
[ 0.37936011, 0.41056112],
[ 0.45977093, 0.90105456],
[ 1.21328513, 1.38968883],
[ 1.79445033, 1.84837734],
[ 1.92186304, 1.96301853],
[ 0.15731425, 0.25997341],
[ 0.62793672, 0.70533181],
[ 0.99486393, 1.01006694],
[ 1.8476812 , 0.85853678],
[ 0.14284534, 0.88913123],
[ 0.97716776, 1.09018915],
[ 0.97161201, 0.58900998],
[ 1.73500472, 1.44673582],
[ 0.9647784 , 0.67339812],
[-0.0737985 , 0.29697366],
[ 0.52818022, 0.17606585],
[ 0.65179958, 0.52030596],
[ 1.17995644, 0.97501462],
[ 0.93388565, 1.11121914],
[ 0.37516049, 0.47811406],
[ 2.28856566, 1.92727012],
[ 0.75213189, 0.60649678],
[ 0.7318502 , 1.03739217],
[ 1.0689742 , 1.78706917],
[ 1.49248181, 1.68003937],
[ 0.92929904, 0.76517611],
[ 1.01305702, 0.87592965],
[ 1.68066055, 1.74996003],
[ 0.47905599, 0.8866 ],
[ 1.34995982, 1.17785485],
[ 0.14056147, 0.43742631],
[ 1.14034598, 0.59048899],
[ 0.81685558, 0.86283475],
[ 0.77493163, 0.78814645],
[ 0.78272775, 1.16437973],
[-0.03891889, 0.39602963],
[ 0.24695063, 0.68468749],
[ 0.65705899, 1.19921722],
[ 0.75625179, 0.51066502],
[ 1.94282437, 2.00203365],
[ 0.3120499 , 0.73915184],
[ 0.40481216, 0.47550674],
[ 0.99378278, 1.40024804],
[ 1.29908681, 1.40593826],
[ 0.25305162, 0.00595536],
[ 1.42712167, 1.18255341],
[ 1.37167545, 1.50937496],
[ 1.00078164, 1.08894058],
[ 0.30535403, 0.49734938],
[ 1.33500435, 0.96301662],
[ 0.91019813, 0.81695105],
[ 0.71188197, 1.17003723],
[ 0.69000046, 1.04905076],
[ 0.87673898, 0.42501247],
[ 1.25174865, 0.93950734],
[ 1.84500802, 1.21012506],
[-0.11342139, 0.45389379],
[ 0.50949098, 1.35315014],
[ 1.66303637, 1.52663501],
[ 1.25161124, 1.09516981],
[ 1.03730311, 1.22388444],
[ 1.3490821 , 1.19041928],
[-0.40000405, 0.21139045],
[ 0.16126175, 0.24460852],
[ 0.47175214, 0.4866838 ],
[ 0.61041822, 0.39119264],
[ 0.91784034, 1.15885657],
[ 1.47795819, 1.29158918],
[-0.14656642, 0.31257236],
[ 1.3808889 , 1.50453295],
[ 1.37033896, 1.46412463],
[-0.0744577 , 0.63083995],
[ 0.12192975, 0.47079 ],
[ 1.74950922, 1.52973459],
[ 1.41792453, 1.54522115],
[ 0.07715062, -0.05034012],
[ 1.83700728, 1.57702777],
[ 0.94535332, 0.64067317],
[ 1.58643266, 1.61009736],
[ 0.8661403 , 1.0710702 ],
[ 1.20950009, 1.21510719],
[-0.42484873, 0.12512812],
[ 1.03568596, 0.68615155],
[ 0.99644923, 1.05526292],
[ 1.0486149 , 0.90956878],
[ 1.0250742 , 0.81378442],
[ 1.11217724, 1.29401667],
[ 1.31009392, 1.38829251],
[ 0.70994221, 0.98957382],
[ 1.18102012, 1.26734074],
[ 0.40896285, 0.27386058],
[ 0.8875179 , 0.71563476],
[ 2.09703402, 2.06702828],
[ 0.67191743, 0.80783912],
[ 0.56457787, 0.44008001],
[ 1.26197261, 0.61118427]])"""