@@ -155,74 +155,43 @@ 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
159
160
- We can import the uniform distribution on $S = \{ 1, \ldots, n\} $ from SciPy like so:
161
-
162
- ``` {code-cell} ipython3
163
- n = 10
164
- u = scipy.stats.randint(1, n+1)
165
- ```
160
+ $$
161
+ p(x_i)=
162
+ \begin{cases}
163
+ p & \text{if $x_i = 1$}\\
164
+ 1-p & \text{if $x_i = 0$}
165
+ \end{cases}
166
+ $$
166
167
168
+ Here $x_i \in S$ is the outcome of the random variable.
167
169
168
- Here's the mean and variance
170
+ We can import the Bernoulli distribution on $S = \{ 0,1 \} $ from SciPy like so:
169
171
170
172
``` {code-cell} ipython3
171
- u.mean(), u.var()
173
+ p = 0.4
174
+ u = scipy.stats.bernoulli(p)
172
175
```
173
176
174
- The formula for the mean is $(n+1)/2$, and the formula for the variance is $(n^2 - 1)/12$.
175
-
176
177
177
- Now let 's evaluate the PMF
178
+ Here 's the mean and variance:
178
179
179
180
``` {code-cell} ipython3
180
- u.pmf(1 )
181
+ u.mean(), u.var( )
181
182
```
182
183
183
- ``` {code-cell} ipython3
184
- u.pmf(2)
185
- ```
184
+ The formula for the mean is $p$, and the formula for the variance is $p(1-p)$.
186
185
187
186
188
- Here 's a plot of the probability mass function :
187
+ Now let 's evaluate the PMF :
189
188
190
189
``` {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()`.
190
+ u.pmf(0)
191
+ u.pmf(1)
222
192
```
223
193
224
194
225
-
226
195
#### Binomial distribution
227
196
228
197
Another useful (and more interesting) distribution is the ** binomial distribution** on $S=\{ 0, \ldots, n\} $, which has PMF
0 commit comments