@@ -155,73 +155,40 @@ Check that your answers agree with `u.mean()` and `u.var()`.
155
155
156
156
#### Bernoulli distribution
157
157
158
- Another useful (and more interesting) distribution is the Bernoulli distribution
158
+ Another useful distribution is the Bernoulli distribution on $S = \{ 0,1\} $, which has PMF:
159
+ $$
160
+ p(x_i)=
161
+ \begin{cases}
162
+ p & \text{if $x_i = 1$}\\
163
+ 1-p & \text{if $x_i = 0$}
164
+ \end{cases}
165
+ $$
166
+ Here $x_i \in S$ is the outcome of the random variable.
159
167
160
- We can import the uniform distribution on $S = \{ 1, \ldots, n \} $ from SciPy like so:
168
+ We can import the Bernoulli distribution on $S = \{ 0,1 \} $ from SciPy like so:
161
169
162
170
``` {code-cell} ipython3
163
- n = 10
164
- u = scipy.stats.randint(1, n+1 )
171
+ p = 0.4
172
+ u = scipy.stats.bernoulli(p )
165
173
```
166
174
167
175
168
- Here's the mean and variance
176
+ Here's the mean and variance:
169
177
170
178
``` {code-cell} ipython3
171
179
u.mean(), u.var()
172
180
```
173
181
174
- The formula for the mean is $(n+1)/2 $, and the formula for the variance is $(n^2 - 1)/12 $.
182
+ The formula for the mean is $p $, and the formula for the variance is $p(1-p) $.
175
183
176
184
177
- Now let's evaluate the PMF
185
+ Now let's evaluate the PMF:
178
186
179
187
``` {code-cell} ipython3
188
+ u.pmf(0)
180
189
u.pmf(1)
181
190
```
182
191
183
- ``` {code-cell} ipython3
184
- u.pmf(2)
185
- ```
186
-
187
-
188
- Here's a plot of the probability mass function:
189
-
190
- ``` {code-cell} ipython3
191
- fig, ax = plt.subplots()
192
- S = np.arange(1, n+1)
193
- ax.plot(S, u.pmf(S), linestyle='', marker='o', alpha=0.8, ms=4)
194
- ax.vlines(S, 0, u.pmf(S), lw=0.2)
195
- ax.set_xticks(S)
196
- plt.show()
197
- ```
198
-
199
-
200
- Here's a plot of the CDF:
201
-
202
- ``` {code-cell} ipython3
203
- fig, ax = plt.subplots()
204
- S = np.arange(1, n+1)
205
- ax.step(S, u.cdf(S))
206
- ax.vlines(S, 0, u.cdf(S), lw=0.2)
207
- ax.set_xticks(S)
208
- plt.show()
209
- ```
210
-
211
-
212
- The CDF jumps up by $p(x_i)$ and $x_i$.
213
-
214
-
215
- ``` {exercise}
216
- :label: prob_ex2
217
-
218
- Calculate the mean and variance for this parameterization (i.e., $n=10$)
219
- directly from the PMF, using the expressions given above.
220
-
221
- Check that your answers agree with `u.mean()` and `u.var()`.
222
- ```
223
-
224
-
225
192
226
193
#### Binomial distribution
227
194
0 commit comments