Exercise 4.1 [MLE for the univariate Gaussian †]
Show that the MLE for a univariate Gaussian is given by
μ ^ = 1 N ∑ n = 1 N y n \hat{\mu} = \frac{1}{N} \sum_{n=1}^N y_n μ ^ = N 1 n = 1 ∑ N y n
σ ^ 2 = 1 N ∑ n = 1 N ( y n − μ ^ ) 2 \hat{\sigma}^2 = \frac{1}{N} \sum_{n=1}^N (y_n - \hat{\mu})^2 σ ^ 2 = N 1 n = 1 ∑ N ( y n − μ ^ ) 2
解答:
写出 log
log p ( D ∣ μ , σ 2 ) = − N 2 ( log 2 π + log σ 2 ) − ∑ n = 1 N ( x n − μ ) 2 2 σ 2 \log p(D\mid \mu, \sigma^2)= -\frac{N}{2}(\log2\pi+\log \sigma^2) -\sum_{n=1}^N \frac{(x_n-\mu)^2}{2\sigma^2} log p ( D ∣ μ , σ 2 ) = − 2 N ( log 2 π + log σ 2 ) − n = 1 ∑ N 2 σ 2 ( x n − μ ) 2
令 ∂ log p ( D ∣ μ , σ 2 ) ∂ μ = 0 , ∂ log p ( D ∣ μ , σ 2 ) ∂ σ 2 = 0 \frac{\partial{\log p(D\mid \mu, \sigma^2)}}{\partial{\mu}}=0, \frac{\partial{\log p(D\mid \mu, \sigma^2)}}{\partial{\sigma^2}}=0 ∂ μ ∂ l o g p ( D ∣ μ , σ 2 ) = 0 , ∂ σ 2 ∂ l o g p ( D ∣ μ , σ 2 ) = 0 ,解得上式。
Exercise 4.2 [MAP estimation for 1D Gaussians †]
Consider samples x 1 , … , x n x_1, \dots, x_n x 1 , … , x n from a Gaussian random variable with known variance σ 2 \sigma^2 σ 2 and unknown mean μ \mu μ . We further assume a prior distribution (also Gaussian) over the mean, μ ∼ N ( m , s 2 ) \mu \sim \mathcal{N}(m, s^2) μ ∼ N ( m , s 2 ) , with fixed mean m m m and fixed variance s 2 s^2 s 2 . Thus the only unknown is μ \mu μ .
a. Calculate the MAP estimate μ ^ M A P \hat{\mu}_{MAP} μ ^ M A P . You can state the result without proof. Alternatively, with a lot more work, you can compute derivatives of the log posterior, set to zero and solve.
b. Show that as the number of samples n n n increase, the MAP estimate converges to the maximum likelihood estimate.
c. Suppose n n n is small and fixed. What does the MAP estimator converge to if we increase the prior variance s 2 s^2 s 2 ?
d. Suppose n n n is small and fixed. What does the MAP estimator converge to if we decrease the prior variance s 2 s^2 s 2 ?
解答:
μ ^ M A P = arg max μ p ( μ , σ 2 ∣ D ) = arg max μ p ( μ ∣ m , s 2 ) p ( D ∣ μ ) p ( D ) = arg max μ log p ( μ ∣ m , s 2 ) + log p ( D ∣ μ ) = arg max μ log { 1 2 π s 2 exp − ( μ − m ) 2 2 s 2 } + log { ( 2 π σ 2 ) − n 2 ∏ i = 1 n exp − ( x i − μ ) 2 2 σ 2 } = arg max μ − 1 2 log ( 2 π s 2 ) − ( μ − m ) 2 2 s 2 − n 2 log 2 π σ 2 − ∑ i = 1 n ( x i − μ ) 2 2 σ 2 = arg max μ − σ 2 ( μ − m ) 2 + s 2 ∑ i = 1 n ( x i − μ ) 2 2 s 2 σ 2 = m σ 2 + s 2 ∑ i = 1 n x i σ 2 + n s 2 \begin{aligned}
\hat{\mu}_{MAP} &=\arg \max_{\mu} p(\mu,\sigma^2\mid D)\\
&=\arg \max_{\mu} \frac{p(\mu\mid m, s^2)p(D\mid \mu)}{p(D)}\\
&=\arg \max_{\mu} \log p(\mu\mid m, s^2)+\log p(D\mid \mu)\\
&=\arg \max_{\mu} \log{\left\{\frac{1}{\sqrt{2\pi s^2}}\exp{-\frac{(\mu -m)^2}{2s^2}} \right\}}+\log \left\{(2\pi \sigma^2)^{-\frac{n}{2}}\prod_{i=1}^n \exp{-\frac{(x_i-\mu)^2}{2\sigma^2}}\right\}\\
&=\arg \max_{\mu} -\frac{1}{2}\log (2\pi s^2)-\frac{(\mu-m)^2}{2s^2}-\frac{n}{2}\log{2\pi \sigma^2}-\sum_{i=1}^n \frac{(x_i-\mu)^2}{2\sigma^2}\\
&=\arg \max_{\mu} - \frac{\sigma^2(\mu-m)^2+s^2\sum_{i=1}^n (x_i-\mu)^2}{2s^2 \sigma^2}\\
&=\frac{m\sigma^2+s^2\sum_{i=1}^n x_i}{\sigma^2+ns^2}
\end{aligned} μ ^ M A P = arg μ max p ( μ , σ 2 ∣ D ) = arg μ max p ( D ) p ( μ ∣ m , s 2 ) p ( D ∣ μ ) = arg μ max log p ( μ ∣ m , s 2 ) + log p ( D ∣ μ ) = arg μ max log { 2 π s 2 1 exp − 2 s 2 ( μ − m ) 2 } + log { ( 2 π σ 2 ) − 2 n i = 1 ∏ n exp − 2 σ 2 ( x i − μ ) 2 } = arg μ max − 2 1 log ( 2 π s 2 ) − 2 s 2 ( μ − m ) 2 − 2 n log 2 π σ 2 − i = 1 ∑ n 2 σ 2 ( x i − μ ) 2 = arg μ max − 2 s 2 σ 2 σ 2 ( μ − m ) 2 + s 2 ∑ i = 1 n ( x i − μ ) 2 = σ 2 + n s 2 m σ 2 + s 2 ∑ i = 1 n x i
b. 随着采样数量的增加 μ ^ M A P → ∑ i = 1 n x i n \hat{\mu}_{MAP}\rightarrow \frac{\sum_{i=1}^n x_i}{n} μ ^ M A P → n ∑ i = 1 n x i ,即MLE。
c. 随着 s 2 s^2 s 2 增加也是收敛于 MLE。
d. 随着 s 2 s^2 s 2 减小收敛于先验均值 m m m 。
Exercise 4.3 [Gaussian posterior credible interval]
Let X ∼ N ( μ , σ 2 = 4 ) X \sim \mathcal{N}(\mu, \sigma^2 = 4) X ∼ N ( μ , σ 2 = 4 ) where μ \mu μ is unknown but has prior μ ∼ N ( μ 0 , σ 0 2 = 9 ) \mu \sim \mathcal{N}(\mu_0, \sigma^2_0 = 9) μ ∼ N ( μ 0 , σ 0 2 = 9 ) . The posterior after seeing n n n samples is μ ∼ N ( μ n , σ n 2 ) \mu \sim \mathcal{N}(\mu_n, \sigma^2_n) μ ∼ N ( μ n , σ n 2 ) . (This is called a credible interval, and is the Bayesian analog of a confidence interval.) How big does n n n have to be to ensure
p ( ℓ ≤ μ n ≤ u ∣ D ) ≥ 0.95 p(\ell \le \mu_n \le u|D) \ge 0.95 p ( ℓ ≤ μ n ≤ u ∣ D ) ≥ 0.95
where ( ℓ , u ) (\ell, u) ( ℓ , u ) is an interval (centered on μ n \mu_n μ n ) of width 1 and D D D is the data? Hint: recall that 95% of the probability mass of a Gaussian is within ± 1.96 σ \pm 1.96\sigma ± 1.96 σ of the mean.
解答:
由题干可知 σ n ≤ 1 2 × 1.96 \sigma_n\leq\frac{1}{2\times 1.96} σ n ≤ 2 × 1.96 1 ,又由习题 4.2 可知后验方差 σ n 2 = s 2 σ 2 σ 2 + s 2 n \sigma_n^2=\frac{s^2\sigma^2}{\sigma^2+s^2n} σ n 2 = σ 2 + s 2 n s 2 σ 2 ,即要求 s 2 σ 2 σ 2 + s 2 n ≤ 1 ( 2 × 1.96 ) 2 → n ≥ 61.02 \frac{s^2\sigma^2}{\sigma^2+s^2n}\leq \frac{1}{(2\times 1.96)^2}\rightarrow n\geq 61.02 σ 2 + s 2 n s 2 σ 2 ≤ ( 2 × 1.96 ) 2 1 → n ≥ 61.02 ,即要大于等于 62。
Exercise 4.4 [BIC for Gaussians †]
The Bayesian information criterion (BIC) is a penalized log-likelihood function that can be used for model selection. It is defined as
B I C = log p ( D ∣ θ ^ M L ) − d 2 log ( N ) BIC = \log p(D|\hat{\theta}_{ML}) - \frac{d}{2} \log(N) B I C = log p ( D ∣ θ ^ M L ) − 2 d log ( N )
where d d d is the number of free parameters in the model and N N N is the number of samples. In this question, we will see how to use this to choose between a full covariance Gaussian and a Gaussian with a diagonal covariance. Obviously a full covariance Gaussian has higher likelihood, but it may not be “worth” the extra parameters if the improvement over a diagonal covariance matrix is too small. So we use the BIC score to choose the model. We can write
log p ( D ∣ Σ ^ , μ ^ ) = − N 2 tr ( Σ ^ − 1 S ^ ) − N 2 log ( ∣ Σ ^ ∣ ) \log p(D|\hat{\Sigma}, \hat{\mu}) = -\frac{N}{2} \text{tr}(\hat{\Sigma}^{-1}\hat{S}) - \frac{N}{2} \log(|\hat{\Sigma}|) log p ( D ∣ Σ ^ , μ ^ ) = − 2 N tr ( Σ ^ − 1 S ^ ) − 2 N log ( ∣ Σ ^ ∣ )
S ^ = 1 N ∑ i = 1 N ( x i − x ˉ ) ( x i − x ˉ ) T \hat{S} = \frac{1}{N} \sum_{i=1}^N (x_i - \bar{x})(x_i - \bar{x})^T S ^ = N 1 i = 1 ∑ N ( x i − x ˉ ) ( x i − x ˉ ) T
where S ^ \hat{S} S ^ is the scatter matrix (empirical covariance), the trace of a matrix is the sum of its diagonals, and we have used the trace trick.
a. Derive the BIC score for a Gaussian in D D D dimensions with full covariance matrix. Simplify your answer as much as possible, exploiting the form of the MLE. Be sure to specify the number of free parameters d d d .
b. Derive the BIC score for a Gaussian in D D D dimensions with a diagonal covariance matrix. Be sure to specify the number of free parameters d d d . Hint: for the digaonal case, the ML estimate of Σ \Sigma Σ is the same as Σ ^ M L \hat{\Sigma}_{ML} Σ ^ M L except the off-diagonal terms are zero:
Σ ^ d i a g = diag ( Σ ^ M L ( 1 , 1 ) , … , Σ ^ M L ( D , D ) ) \hat{\Sigma}_{diag} = \text{diag}(\hat{\Sigma}_{ML}(1, 1), \dots, \hat{\Sigma}_{ML}(D,D)) Σ ^ d ia g = diag ( Σ ^ M L ( 1 , 1 ) , … , Σ ^ M L ( D , D ))
解答:
BIC = log p ( D ∣ Σ ^ , μ ^ ) − d 2 log ( N ) = − N 2 t r ( Σ ^ − 1 S ^ ) − N 2 log ( ∣ Σ ^ ∣ ) − d 2 log ( N ) = − N 2 − N 2 log ( ∣ Σ ^ ∣ ) − D ( D + 1 ) 2 log ( N ) \begin{aligned}
\text{BIC} &=\log p(D\mid \hat{\Sigma},\hat{\mu})-\frac{d}{2}\log(N)\\
&=-\frac{N}{2}\mathrm{tr}(\hat{\Sigma}^{-1}\hat{S})-\frac{N}{2}\log (\lvert\hat{\Sigma}\rvert) - \frac{d}{2}\log (N)\\
&=-\frac{N}{2}-\frac{N}{2}\log (\lvert\hat{\Sigma}\rvert)-\frac{D(D+1)}{2}\log (N)
\end{aligned} BIC = log p ( D ∣ Σ ^ , μ ^ ) − 2 d log ( N ) = − 2 N tr ( Σ ^ − 1 S ^ ) − 2 N log (∣ Σ ^ ∣) − 2 d log ( N ) = − 2 N − 2 N log (∣ Σ ^ ∣) − 2 D ( D + 1 ) log ( N )
BIC = − N 2 t r ( Σ ^ − 1 S ^ ) − N 2 log ( ∣ Σ ^ ∣ ) − d 2 log ( N ) = − N 2 − N 2 log ( ∣ Σ ^ ∣ ) − D log ( N ) \begin{aligned}
\text{BIC}&=-\frac{N}{2}\mathrm{tr}(\hat{\Sigma}^{-1}\hat{S})-\frac{N}{2}\log (\lvert\hat{\Sigma}\rvert) - \frac{d}{2}\log (N)\\
&=-\frac{N}{2}-\frac{N}{2}\log (\lvert\hat{\Sigma}\rvert)-D\log(N)
\end{aligned} BIC = − 2 N tr ( Σ ^ − 1 S ^ ) − 2 N log (∣ Σ ^ ∣) − 2 d log ( N ) = − 2 N − 2 N log (∣ Σ ^ ∣) − D log ( N )
⚠️ 修正:
BIC f u l l = log p ( D ∣ Σ ^ M L , μ ^ ) − d 2 log ( N ) = − N 2 tr ( Σ ^ M L − 1 S ^ ) − N 2 log ( ∣ Σ ^ M L ∣ ) − D ( D + 3 ) 4 log ( N ) 协方差矩阵上三角元素个数和均值个数 = − N D 2 − N 2 log ( ∣ Σ ^ M L ∣ ) − D ( D + 3 ) 4 log ( N ) \begin{aligned}
\text{BIC}_{full} &= \log p(D\mid \hat{\Sigma}_{ML},\hat{\mu}) - \frac{d}{2}\log(N)\\
&= -\frac{N}{2}\text{tr}(\hat{\Sigma}_{ML}^{-1}\hat{S}) - \frac{N}{2}\log(|\hat{\Sigma}_{ML}|) - \frac{D(D+3)}{4}\log(N) \quad \text{协方差矩阵上三角元素个数和均值个数}\\
&= -\frac{ND}{2} - \frac{N}{2}\log(|\hat{\Sigma}_{ML}|) - \frac{D(D+3)}{4}\log(N)
\end{aligned} BIC f u l l = log p ( D ∣ Σ ^ M L , μ ^ ) − 2 d log ( N ) = − 2 N tr ( Σ ^ M L − 1 S ^ ) − 2 N log ( ∣ Σ ^ M L ∣ ) − 4 D ( D + 3 ) log ( N ) 协方差矩阵上三角元素个数和均值个数 = − 2 N D − 2 N log ( ∣ Σ ^ M L ∣ ) − 4 D ( D + 3 ) log ( N )
BIC d i a g = log p ( D ∣ Σ ^ d i a g , μ ^ ) − d 2 log ( N ) = − N 2 tr ( Σ ^ d i a g − 1 S ^ ) − N 2 log ( ∣ Σ ^ d i a g ∣ ) − 2 D 2 log ( N ) = − N D 2 − N 2 log ( ∣ Σ ^ d i a g ∣ ) − D log ( N ) \begin{aligned}
\text{BIC}_{diag} &= \log p(D\mid \hat{\Sigma}_{diag},\hat{\mu}) - \frac{d}{2}\log(N)\\
&= -\frac{N}{2}\text{tr}(\hat{\Sigma}_{diag}^{-1}\hat{S}) - \frac{N}{2}\log(|\hat{\Sigma}_{diag}|) - \frac{2D}{2}\log(N) \\
&= -\frac{ND}{2} - \frac{N}{2}\log(|\hat{\Sigma}_{diag}|) - D\log(N)
\end{aligned} BIC d ia g = log p ( D ∣ Σ ^ d ia g , μ ^ ) − 2 d log ( N ) = − 2 N tr ( Σ ^ d ia g − 1 S ^ ) − 2 N log ( ∣ Σ ^ d ia g ∣ ) − 2 2 D log ( N ) = − 2 N D − 2 N log ( ∣ Σ ^ d ia g ∣ ) − D log ( N )
Exercise 4.5 [BIC for a 2d discrete distribution]
Let x ∈ { 0 , 1 } x \in \{0, 1\} x ∈ { 0 , 1 } denote the result of a coin toss (x = 0 x = 0 x = 0 for tails, x = 1 x = 1 x = 1 for heads). The coin is potentially biased, so that heads occurs with probability θ 1 \theta_1 θ 1 . Suppose that someone else observes the coin flip and reports to you the outcome, y y y . But this person is unreliable and only reports the result correctly with probability θ 2 \theta_2 θ 2 ; i.e., p ( y ∣ x , θ 2 ) p(y|x, \theta_2) p ( y ∣ x , θ 2 ) is given by
y = 0 y = 0 y = 0 y = 1 y = 1 y = 1 x = 0 x = 0 x = 0 θ 2 \theta_2 θ 2 1 − θ 2 1 - \theta_2 1 − θ 2 x = 1 x = 1 x = 1 1 − θ 2 1 - \theta_2 1 − θ 2 θ 2 \theta_2 θ 2
Assume that θ 2 \theta_2 θ 2 is independent of x x x and θ 1 \theta_1 θ 1 .
a. Write down the joint probability distribution p ( x , y ∣ θ ) p(x, y|\theta) p ( x , y ∣ θ ) as a 2x2 table, in terms of θ = ( θ 1 , θ 2 ) \theta = (\theta_1, \theta_2) θ = ( θ 1 , θ 2 ) .
b. Suppose have the following dataset: x = ( 1 , 1 , 0 , 1 , 1 , 0 , 0 ) x = (1, 1, 0, 1, 1, 0, 0) x = ( 1 , 1 , 0 , 1 , 1 , 0 , 0 ) , y = ( 1 , 0 , 0 , 0 , 1 , 0 , 1 ) y = (1, 0, 0, 0, 1, 0, 1) y = ( 1 , 0 , 0 , 0 , 1 , 0 , 1 ) . What are the MLEs for θ 1 \theta_1 θ 1 and θ 2 \theta_2 θ 2 ? Justify your answer. Hint: note that the likelihood function factorizes,
p ( x , y ∣ θ ) = p ( y ∣ x , θ 2 ) p ( x ∣ θ 1 ) p(x, y|\theta) = p(y|x, \theta_2)p(x|\theta_1) p ( x , y ∣ θ ) = p ( y ∣ x , θ 2 ) p ( x ∣ θ 1 )
What is p ( D ∣ θ ^ , M 2 ) p(D|\hat{\theta}, M_2) p ( D ∣ θ ^ , M 2 ) where M 2 M_2 M 2 denotes this 2-parameter model? (You may leave your answer in fractional form if you wish.)
c. Now consider a model with 4 parameters, θ = ( θ 0 , 0 , θ 0 , 1 , θ 1 , 0 , θ 1 , 1 ) \theta = (\theta_{0,0}, \theta_{0,1}, \theta_{1,0}, \theta_{1,1}) θ = ( θ 0 , 0 , θ 0 , 1 , θ 1 , 0 , θ 1 , 1 ) , representing p ( x , y ∣ θ ) = θ x , y p(x, y|\theta) = \theta_{x,y} p ( x , y ∣ θ ) = θ x , y . (Only 3 of these parameters are free to vary, since they must sum to one.) What is the MLE of θ \theta θ ? What is p ( D ∣ θ ^ , M 4 ) p(D|\hat{\theta}, M_4) p ( D ∣ θ ^ , M 4 ) where M 4 M_4 M 4 denotes this 4-parameter model?
d. Suppose we are not sure which model is correct. We compute the leave-one-out cross validated log likelihood of the 2-parameter model and the 4-parameter model as follows:
L ( m ) = ∑ i = 1 n log p ( x i , y i ∣ m , θ ^ ( D − i ) ) L(m) = \sum_{i=1}^n \log p(x_i, y_i|m, \hat{\theta}(D_{-i})) L ( m ) = i = 1 ∑ n log p ( x i , y i ∣ m , θ ^ ( D − i ))
and θ ^ ( D − i ) \hat{\theta}(D_{-i}) θ ^ ( D − i ) denotes the MLE computed on D D D excluding row i i i . Which model will CV pick and why? Hint: notice how the table of counts changes when you omit each training case one at a time.
e. Recall that an alternative to CV is to use the BIC score, defined as
B I C ( M , D ) ≜ log p ( D ∣ θ ^ M L E ) − dof ( M ) 2 log N BIC(M,D) \triangleq \log p(D|\hat{\theta}_{MLE}) - \frac{\text{dof}(M)}{2} \log N B I C ( M , D ) ≜ log p ( D ∣ θ ^ M L E ) − 2 dof ( M ) log N
where dof ( M ) \text{dof}(M) dof ( M ) is the number of free parameters in the model, Compute the BIC scores for both models (use log base e e e ). Which model does BIC prefer?
解答:
a. p ( x = 0 , y = 0 ∣ θ ) = θ 2 ( 1 − θ 1 ) , p ( x = 1 , y = 0 ∣ θ ) = ( 1 − θ 2 ) θ 1 , p ( x = 0 , y = 1 ) = ( 1 − θ 2 ) ( 1 − θ 1 ) , p ( x = 1 , y = 1 ∣ θ ) = θ 2 θ 1 p(x=0,y=0\mid\theta)=\theta_2(1-\theta_1), p(x=1,y=0\mid \theta)=(1-\theta_2)\theta_1, p(x=0,y=1)=(1-\theta_2)(1-\theta_1), p(x=1,y=1\mid \theta)=\theta_2\theta_1 p ( x = 0 , y = 0 ∣ θ ) = θ 2 ( 1 − θ 1 ) , p ( x = 1 , y = 0 ∣ θ ) = ( 1 − θ 2 ) θ 1 , p ( x = 0 , y = 1 ) = ( 1 − θ 2 ) ( 1 − θ 1 ) , p ( x = 1 , y = 1 ∣ θ ) = θ 2 θ 1 。
b.
p ( { x n , y n } n = 1 N ∣ θ ) = θ 1 4 ( 1 − θ 1 ) 3 θ 2 4 ( 1 − θ 2 ) 3 p(\{x_n, y_n\}_{n=1}^N\mid \theta)=\theta_1^4(1-\theta_1)^3\theta_2^4(1-\theta_2)^3 p ({ x n , y n } n = 1 N ∣ θ ) = θ 1 4 ( 1 − θ 1 ) 3 θ 2 4 ( 1 − θ 2 ) 3
易得 θ 1 = θ 2 = 4 7 \theta_1=\theta_2=\frac{4}{7} θ 1 = θ 2 = 7 4 ,同时求得 p ( D ∣ θ ^ , M 2 ) = ( 4 7 ) 8 ( 3 7 ) 6 p(D|\hat{\theta}, M_2)=(\frac{4}{7})^8 (\frac{3}{7})^6 p ( D ∣ θ ^ , M 2 ) = ( 7 4 ) 8 ( 7 3 ) 6 。
log p ( { x n , y n } n = 1 N ∣ θ ) = log θ 0 , 0 2 θ 0 , 1 1 θ 1 , 0 2 θ 1 , 1 2 θ 0 , 0 = arg max θ 0 , 0 log p ( { x n , y n } n = 1 N ∣ θ ) s.t. θ 0 , 0 + θ 0 , 1 + θ 1 , 0 + θ 1 , 1 = 1 θ 0 , 0 = arg max θ 0 , 0 2 log θ 0 , 0 + α ( θ 0 , 0 + θ 0 , 1 + θ 1 , 0 + θ 1 , 1 − 1 ) \begin{aligned}
\log p(\{x_n, y_n\}_{n=1}^N\mid \theta)&=\log\theta_{0,0}^2\theta_{0,1}^1\theta_{1,0}^2\theta_{1,1}^2\\
\theta_{0,0} &=\arg \max_{\theta_{0,0}} \log p(\{x_n, y_n\}_{n=1}^N\mid \theta) \text{ s.t. } \theta_{0,0}+\theta_{0,1}+\theta_{1,0}+\theta_{1,1}=1\\
\theta_{0,0}&= \arg \max_{\theta_{0,0}} 2\log \theta_{0,0}+\alpha(\theta_{0,0}+\theta_{0,1}+\theta_{1,0}+\theta_{1,1}-1)\\
\end{aligned} log p ({ x n , y n } n = 1 N ∣ θ ) θ 0 , 0 θ 0 , 0 = log θ 0 , 0 2 θ 0 , 1 1 θ 1 , 0 2 θ 1 , 1 2 = arg θ 0 , 0 max log p ({ x n , y n } n = 1 N ∣ θ ) s.t. θ 0 , 0 + θ 0 , 1 + θ 1 , 0 + θ 1 , 1 = 1 = arg θ 0 , 0 max 2 log θ 0 , 0 + α ( θ 0 , 0 + θ 0 , 1 + θ 1 , 0 + θ 1 , 1 − 1 )
易得 θ 0 , 0 = θ 1 , 0 = θ 1 , 1 = 2 7 , θ 0 , 1 = 1 7 \theta_{0,0}=\theta_{1,0}=\theta_{1,1}=\frac{2}{7},\theta_{0,1}=\frac{1}{7} θ 0 , 0 = θ 1 , 0 = θ 1 , 1 = 7 2 , θ 0 , 1 = 7 1 ,同时求得 p ( D ∣ θ ^ , M 4 ) = ( 2 7 ) 6 1 7 p(D\mid \hat{\theta},M_4)=(\frac{2}{7})^6\frac{1}{7} p ( D ∣ θ ^ , M 4 ) = ( 7 2 ) 6 7 1 。
L ( M 2 ) = 2 log p ( x = 0 , y = 0 ∣ M 2 , θ ^ ( D − ( 0 , 0 ) ) ) + 2 log p ( x = 1 , y = 0 ) + log p ( x = 0 , y = 1 ) + 2 log p ( x = 1 , y = 1 ) = 2 log p ( x = 0 , y = 0 ∣ θ 1 = 2 3 , θ 2 = 1 2 ) + ⋯ = − 2 log 6 + ⋯ 去掉(0,1)会出现无穷大 \begin{aligned}
L(M_2)&=2 \log p(x=0,y=0\mid M_2, \hat{\theta}(D_{-(0,0)}))+2\log p(x=1,y=0)+\log p(x=0,y=1)+2\log p(x=1, y=1)\\
&=2\log p(x=0,y=0\mid \theta_1=\frac{2}{3},\theta_2=\frac{1}{2})+\cdots\\
&=-2 \log 6 +\cdots \quad \text{去掉(0,1)会出现无穷大}
\end{aligned} L ( M 2 ) = 2 log p ( x = 0 , y = 0 ∣ M 2 , θ ^ ( D − ( 0 , 0 ) )) + 2 log p ( x = 1 , y = 0 ) + log p ( x = 0 , y = 1 ) + 2 log p ( x = 1 , y = 1 ) = 2 log p ( x = 0 , y = 0 ∣ θ 1 = 3 2 , θ 2 = 2 1 ) + ⋯ = − 2 log 6 + ⋯ 去掉( 0 , 1 )会出现无穷大
L ( M 4 ) = 2 log p ( x = 0 , y = 0 ∣ M 4 , θ ^ ( D − ( 0 , 0 ) ) ) + 2 log p ( x = 1 , y = 0 ) + log p ( x = 0 , y = 1 ) + 2 log p ( x = 1 , y = 1 ) = 2 log p ( x = 0 , y = 0 ∣ θ 0 , 0 = 1 6 , θ 0 , 1 = 1 6 , θ 1 , 0 = 1 3 , θ 1 , 1 = 1 3 ) + ⋯ = − 2 log 6 + ⋯ \begin{aligned}
L(M_4)&=2 \log p(x=0,y=0\mid M_4, \hat{\theta}(D_{-(0,0)}))+2\log p(x=1,y=0)+\log p(x=0,y=1)+2\log p(x=1, y=1)\\
&=2\log p(x=0,y=0\mid \theta_{0,0}=\frac{1}{6}, \theta_{0,1}=\frac{1}{6}, \theta_{1,0}=\frac{1}{3}, \theta_{1,1}=\frac{1}{3})+\cdots\\
&=-2\log 6+\cdots
\end{aligned} L ( M 4 ) = 2 log p ( x = 0 , y = 0 ∣ M 4 , θ ^ ( D − ( 0 , 0 ) )) + 2 log p ( x = 1 , y = 0 ) + log p ( x = 0 , y = 1 ) + 2 log p ( x = 1 , y = 1 ) = 2 log p ( x = 0 , y = 0 ∣ θ 0 , 0 = 6 1 , θ 0 , 1 = 6 1 , θ 1 , 0 = 3 1 , θ 1 , 1 = 3 1 ) + ⋯ = − 2 log 6 + ⋯
BIC ( M 2 , D ) = log ( ( 4 7 ) 8 ( 3 7 ) 6 ) − log 7 \text{BIC}(M_2, D)=\log \left((\frac{4}{7})^8 (\frac{3}{7})^6\right)-\log 7 BIC ( M 2 , D ) = log ( ( 7 4 ) 8 ( 7 3 ) 6 ) − log 7
BIC ( M 4 , D ) = log ( ( 2 7 ) 6 1 7 ) − 2 log 7 \text{BIC}(M_4,D)=\log \left((\frac{2}{7})^6\frac{1}{7} \right)-2\log7 BIC ( M 4 , D ) = log ( ( 7 2 ) 6 7 1 ) − 2 log 7
⚠️ 纠正:
e. 四参数模型参数和要求为1,所以自由参数为 4 − 1 = 3 4-1=3 4 − 1 = 3 ,最终答案为 BIC ( M 4 , D ) = log ( ( 2 7 ) 6 1 7 ) − 3 2 log 7 \text{BIC}(M_4,D)=\log \left((\frac{2}{7})^6\frac{1}{7} \right)-\frac{3}{2}\log7 BIC ( M 4 , D ) = log ( ( 7 2 ) 6 7 1 ) − 2 3 log 7
Exercise 4.6 [A mixture of conjugate priors is conjugate †]
Consider a mixture prior
p ( θ ) = ∑ k p ( z = k ) p ( θ ∣ z = k ) p(\theta) = \sum_k p(z = k)p(\theta|z = k) p ( θ ) = k ∑ p ( z = k ) p ( θ ∣ z = k )
where each p ( θ ∣ z = k ) p(\theta|z = k) p ( θ ∣ z = k ) is conjugate to the likelihood. Prove that this is a conjugate prior.
解答:
已知 p ( θ ∣ D ) = p ( θ ) p ( D ∣ θ ) p ( D ) p(\theta \mid D) = \frac{p(\theta)p(D \mid \theta)}{p(D)} p ( θ ∣ D ) = p ( D ) p ( θ ) p ( D ∣ θ ) ,代入混合先验
p ( θ ∣ D ) = ( ∑ k p ( z = k ) p ( θ ∣ z = k ) ) p ( D ∣ θ ) p ( D ) = ∑ k p ( z = k ) [ p ( θ ∣ z = k ) p ( D ∣ θ ) ] p ( D ) \begin{aligned}
p(\theta \mid D) &= \frac{\left( \sum_k p(z=k)p(\theta \mid z=k) \right) p(D \mid \theta)}{p(D)}\\
&= \frac{\sum_k p(z=k) \left[ p(\theta \mid z=k) p(D \mid \theta) \right]}{p(D)}
\end{aligned} p ( θ ∣ D ) = p ( D ) ( ∑ k p ( z = k ) p ( θ ∣ z = k ) ) p ( D ∣ θ ) = p ( D ) ∑ k p ( z = k ) [ p ( θ ∣ z = k ) p ( D ∣ θ ) ]
又有在给定隐变量下的后验分布
p ( θ ∣ D , z = k ) = p ( θ ∣ z = k ) p ( D ∣ θ ) p ( D ∣ z = k ) p ( θ ∣ z = k ) p ( D ∣ θ ) = p ( θ ∣ D , z = k ) p ( D ∣ z = k ) p ( θ ∣ D ) = ∑ k p ( z = k ) p ( D ∣ z = k ) p ( θ ∣ D , z = k ) p ( D ) 代入混合先验 = ∑ k ( p ( z = k ) p ( D ∣ z = k ) p ( D ) ) p ( θ ∣ D , z = k ) = ∑ k p ( z = k ∣ D ) p ( θ ∣ D , z = k ) \begin{aligned}
p(\theta \mid D, z=k)& = \frac{p(\theta \mid z=k) p(D \mid \theta)}{p(D \mid z=k)}\\
p(\theta \mid z=k) p(D \mid \theta)& = p(\theta \mid D, z=k) p(D \mid z=k)\\
p(\theta \mid D) &= \frac{\sum_k p(z=k) p(D \mid z=k) p(\theta \mid D, z=k)}{p(D)} \quad \text{代入混合先验}\\
&= \sum_k \left( \frac{p(z=k) p(D \mid z=k)}{p(D)} \right) p(\theta \mid D, z=k)\\
&= \sum_k p(z=k \mid D) p(\theta \mid D, z=k)
\end{aligned} p ( θ ∣ D , z = k ) p ( θ ∣ z = k ) p ( D ∣ θ ) p ( θ ∣ D ) = p ( D ∣ z = k ) p ( θ ∣ z = k ) p ( D ∣ θ ) = p ( θ ∣ D , z = k ) p ( D ∣ z = k ) = p ( D ) ∑ k p ( z = k ) p ( D ∣ z = k ) p ( θ ∣ D , z = k ) 代入混合先验 = k ∑ ( p ( D ) p ( z = k ) p ( D ∣ z = k ) ) p ( θ ∣ D , z = k ) = k ∑ p ( z = k ∣ D ) p ( θ ∣ D , z = k )
因为组件的先验 p ( θ ∣ z = k ) p(\theta \mid z=k) p ( θ ∣ z = k ) 是共轭的,所以当它乘上似然 p ( D ∣ θ ) p(D \mid \theta) p ( D ∣ θ ) 后,计算出的组件的后验 p ( θ ∣ D , z = k ) p(\theta \mid D, z=k) p ( θ ∣ D , z = k ) ,在数学形式上,必定与它原来的组件的先验 p ( θ ∣ z = k ) p(\theta \mid z=k) p ( θ ∣ z = k ) 属于同一个分布族。
Exercise 4.7 [ML estimator σ m l e 2 \sigma^2_{mle} σ m l e 2 is biased]
Show that σ ^ M L E 2 = 1 N ∑ n = 1 N ( x n − μ ^ ) 2 \hat{\sigma}^2_{MLE} = \frac{1}{N} \sum_{n=1}^N(x_n - \hat{\mu})^2 σ ^ M L E 2 = N 1 ∑ n = 1 N ( x n − μ ^ ) 2 is a biased estimator of σ 2 \sigma^2 σ 2 , i.e., show
E X 1 , … , X n ∼ N ( μ , σ 2 ) [ σ ^ 2 ( X 1 , … , X n ) ] ≠ σ 2 \mathbb{E}_{X_1,\dots,X_n \sim \mathcal{N}(\mu,\sigma^2)}[\hat{\sigma}^2(X_1, \dots, X_n)] \neq \sigma^2 E X 1 , … , X n ∼ N ( μ , σ 2 ) [ σ ^ 2 ( X 1 , … , X n )] = σ 2
Hint: note that X 1 , … , X N X_1, \dots, X_N X 1 , … , X N are independent, and use the fact that the expectation of a product of independent random variables is the product of the expectations.
解答:
E X 1 , … , X n ∼ N ( μ , σ 2 ) [ σ ^ 2 ( X 1 , … , X n ) ] = E [ 1 N ∑ n = 1 N ( X n − μ ^ ) 2 ] = E [ 1 N ( ∑ n = 1 N X n 2 + 1 N 2 ( ∑ n = 1 N X n ) 2 − 2 N ( ∑ n = 1 N X n ) 2 ) ] = 1 N E [ ∑ n = 1 N X n 2 ] + 1 N 3 E [ ( ∑ n = 1 N X n ) 2 ] − 2 N 2 E [ ( ∑ n = 1 N X n ) 2 ] = σ 2 + μ 2 + 1 − 2 N N 3 E [ ( ∑ n = 1 N X n ) 2 ] = σ 2 + μ 2 + 1 − 2 N N 3 [ N ( N − 1 ) μ 2 + N ( σ 2 + μ 2 ) ] = N 2 − 2 N + 1 N 2 σ 2 + 1 − N N μ 2 \begin{aligned}
\mathbb{E}_{X_1,\dots,X_n\sim \mathcal{N}(\mu,\sigma^2)}[\hat{\sigma}^2(X_1,\dots, X_n)]&=\mathbb{E}[\frac{1}{N}\sum_{n=1}^N (X_n -\hat{\mu})^2]\\
&=\mathbb{E}[\frac{1}{N}(\sum_{n=1}^N X_n^2+\frac{1}{N^2}(\sum_{n=1}^NX_n)^2-\frac{2}{N}(\sum_{n=1}^NX_n)^2)]\\
&=\frac{1}{N}\mathbb{E}[\sum_{n=1}^N X_n^2]+\frac{1}{N^3}\mathbb{E}[(\sum_{n=1}^NX_n)^2]-\frac{2}{N^2}\mathbb{E}[(\sum_{n=1}^NX_n)^2] \\
&=\sigma^2+\mu^2+\frac{1-2N}{N^3}\mathbb{E}[(\sum_{n=1}^NX_n)^2]\\
&=\sigma^2+\mu^2+\frac{1-2N}{N^3}[N(N-1)\mu^2+N(\sigma^2+\mu^2)]\\
&=\frac{N^2-2N+1}{N^2}\sigma^2+\frac{1-N}{N}\mu^2
\end{aligned} E X 1 , … , X n ∼ N ( μ , σ 2 ) [ σ ^ 2 ( X 1 , … , X n )] = E [ N 1 n = 1 ∑ N ( X n − μ ^ ) 2 ] = E [ N 1 ( n = 1 ∑ N X n 2 + N 2 1 ( n = 1 ∑ N X n ) 2 − N 2 ( n = 1 ∑ N X n ) 2 )] = N 1 E [ n = 1 ∑ N X n 2 ] + N 3 1 E [( n = 1 ∑ N X n ) 2 ] − N 2 2 E [( n = 1 ∑ N X n ) 2 ] = σ 2 + μ 2 + N 3 1 − 2 N E [( n = 1 ∑ N X n ) 2 ] = σ 2 + μ 2 + N 3 1 − 2 N [ N ( N − 1 ) μ 2 + N ( σ 2 + μ 2 )] = N 2 N 2 − 2 N + 1 σ 2 + N 1 − N μ 2
⚠️ 纠错: 第二行展开错误
E [ σ ^ 2 ] = ( σ 2 + μ 2 ) − ( μ 2 + σ 2 N ) = σ 2 − σ 2 N = N − 1 N σ 2 \begin{aligned}
\mathbb{E}[\hat{\sigma}^2] &= (\sigma^2 + \mu^2) - \left(\mu^2 + \frac{\sigma^2}{N}\right)\\
&= \sigma^2 - \frac{\sigma^2}{N} = \frac{N-1}{N}\sigma^2
\end{aligned} E [ σ ^ 2 ] = ( σ 2 + μ 2 ) − ( μ 2 + N σ 2 ) = σ 2 − N σ 2 = N N − 1 σ 2
Exercise 4.8 [Estimation of σ 2 \sigma^2 σ 2 when μ \mu μ is known †]
Suppose we sample x 1 , … , x N ∼ N ( μ , σ 2 ) x_1, \dots, x_N \sim \mathcal{N}(\mu, \sigma^2) x 1 , … , x N ∼ N ( μ , σ 2 ) where μ \mu μ is a known constant. Derive an expression for the MLE for σ 2 \sigma^2 σ 2 in this case. Is it unbiased?
解答:
σ ^ 2 = arg max σ 2 ∑ n = 1 N [ − 1 2 log 2 π − 1 2 log σ 2 − ( x n − μ ) 2 2 σ 2 ] = arg max σ 2 − N 2 log σ 2 − 1 2 σ 2 ∑ n = 1 N ( x n − μ ) 2 = ∑ n = 1 N ( x n − μ ) 2 2 N \begin{aligned}
\hat{\sigma}^2&=\arg \max_{\sigma^2} \sum_{n=1}^N [-\frac{1}{2}\log{2\pi}-\frac{1}{2}\log{\sigma^2}-\frac{(x_n-\mu)^2}{2\sigma^2}]\\
&=\arg \max_{\sigma^2} -\frac{N}{2}\log \sigma^2-\frac{1}{2\sigma^2}\sum_{n=1}^N(x_n-\mu)^2\\
&=\frac{\sum_{n=1}^N (x_n-\mu)^2}{2N}
\end{aligned} σ ^ 2 = arg σ 2 max n = 1 ∑ N [ − 2 1 log 2 π − 2 1 log σ 2 − 2 σ 2 ( x n − μ ) 2 ] = arg σ 2 max − 2 N log σ 2 − 2 σ 2 1 n = 1 ∑ N ( x n − μ ) 2 = 2 N ∑ n = 1 N ( x n − μ ) 2
仍然是有偏的
⚠️ 纠正: 求导时犯错,应为 ∑ n = 1 N ( x n − μ ) 2 N \frac{\sum_{n=1}^N (x_n-\mu)^2}{N} N ∑ n = 1 N ( x n − μ ) 2 ,并且在这个特定场景下(μ \mu μ 是已知的常数),它是无偏估计。
E [ σ ^ 2 ] = E [ 1 N ∑ n = 1 N ( X n − μ ) 2 ] = 1 N ∑ n = 1 N E [ ( X n − μ ) 2 ] \begin{aligned}
\mathbb{E}[\hat{\sigma}^2] &= \mathbb{E}\left[ \frac{1}{N}\sum_{n=1}^N (X_n-\mu)^2 \right] \\
&= \frac{1}{N}\sum_{n=1}^N \mathbb{E}[(X_n-\mu)^2]
\end{aligned} E [ σ ^ 2 ] = E [ N 1 n = 1 ∑ N ( X n − μ ) 2 ] = N 1 n = 1 ∑ N E [( X n − μ ) 2 ]
由于题目已知 μ \mu μ 就是真实的总体均值,所以根据方差的严格数学定义,E [ ( X n − μ ) 2 ] \mathbb{E}[(X_n-\mu)^2] E [( X n − μ ) 2 ] 就是 X n X_n X n 的真实方差 σ 2 \sigma^2 σ 2 。
Exercise 4.9 [Variance and MSE of estimators for Gaussian variance †]
Prove that the standard error for the MLE for a Gaussian variance is
V [ σ m l e 2 ] = 2 ( N − 1 ) N 2 σ 2 \sqrt{\mathbb{V}[\sigma^2_{mle}]} = \sqrt{\frac{2(N - 1)}{N^2}} \sigma^2 V [ σ m l e 2 ] = N 2 2 ( N − 1 ) σ 2
Hint: use the fact that
N − 1 σ 2 σ u n b 2 ∼ χ N − 1 2 \frac{N - 1}{\sigma^2} \sigma^2_{unb} \sim \chi^2_{N-1} σ 2 N − 1 σ u nb 2 ∼ χ N − 1 2
and that V [ χ N − 1 2 ] = 2 ( N − 1 ) \mathbb{V}[\chi^2_{N-1}] = 2(N - 1) V [ χ N − 1 2 ] = 2 ( N − 1 ) . Finally, show that M S E ( σ u n b 2 ) = 2 N − 1 σ 4 MSE(\sigma^2_{unb}) = \frac{2}{N-1} \sigma^4 M S E ( σ u nb 2 ) = N − 1 2 σ 4 and M S E ( σ m l e 2 ) = 2 N − 1 N 2 σ 4 MSE(\sigma^2_{mle}) = \frac{2N-1}{N^2}\sigma^4 M S E ( σ m l e 2 ) = N 2 2 N − 1 σ 4 .
解答:
已知 σ m l e 2 = N − 1 N σ u n b 2 \sigma_{mle}^2=\frac{N-1}{N}\sigma_{unb}^2 σ m l e 2 = N N − 1 σ u nb 2 ,因此 N σ 2 σ m l e 2 ∼ χ N − 1 2 \frac{N}{\sigma^2}\sigma_{mle}^2\sim\chi_{N-1}^2 σ 2 N σ m l e 2 ∼ χ N − 1 2 ,可得 V [ σ m l e 2 ] = 2 ( N − 1 ) N 2 σ 2 \sqrt{\mathbb{V}[\sigma^2_{mle}]} = \sqrt{\frac{2(N - 1)}{N^2}} \sigma^2 V [ σ m l e 2 ] = N 2 2 ( N − 1 ) σ 2 。
⚠️ 补充:
利用偏差-方差分解公式:
MSE ( θ ^ ) = V [ θ ^ ] + ( Bias ( θ ^ ) ) 2 \text{MSE}(\hat{\theta}) = \mathbb{V}[\hat{\theta}] + (\text{Bias}(\hat{\theta}))^2 MSE ( θ ^ ) = V [ θ ^ ] + ( Bias ( θ ^ ) ) 2
( N − 1 ) 2 σ 4 V [ σ u n b 2 ] = 2 ( N − 1 ) ⟹ V [ σ u n b 2 ] = 2 N − 1 σ 4 MSE ( σ u n b 2 ) = 2 N − 1 σ 4 + 0 2 = 2 N − 1 σ 4 \begin{aligned}
\frac{(N-1)^2}{\sigma^4} \mathbb{V}[\sigma^2_{unb}] &= 2(N-1) \implies \mathbb{V}[\sigma^2_{unb}] = \frac{2}{N-1}\sigma^4\\
\text{MSE}(\sigma^2_{unb}) &= \frac{2}{N-1}\sigma^4 + 0^2 = \frac{2}{N-1}\sigma^4
\end{aligned} σ 4 ( N − 1 ) 2 V [ σ u nb 2 ] MSE ( σ u nb 2 ) = 2 ( N − 1 ) ⟹ V [ σ u nb 2 ] = N − 1 2 σ 4 = N − 1 2 σ 4 + 0 2 = N − 1 2 σ 4
由习题 4.7 可知偏差为:
Bias ( σ m l e 2 ) = E [ σ m l e 2 ] − σ 2 = N − 1 N σ 2 − σ 2 = − 1 N σ 2 \text{Bias}(\sigma^2_{mle}) = \mathbb{E}[\sigma^2_{mle}] - \sigma^2 = \frac{N-1}{N}\sigma^2 - \sigma^2 = -\frac{1}{N}\sigma^2 Bias ( σ m l e 2 ) = E [ σ m l e 2 ] − σ 2 = N N − 1 σ 2 − σ 2 = − N 1 σ 2
又由前半部分结论 V [ σ m l e 2 ] = 2 ( N − 1 ) N 2 σ 4 \mathbb{V}[\sigma_{mle}^2] = \frac{2(N-1)}{N^2} \sigma^4 V [ σ m l e 2 ] = N 2 2 ( N − 1 ) σ 4 ,可得
MSE ( σ m l e 2 ) = 2 N − 2 N 2 σ 4 + 1 N 2 σ 4 = 2 N − 1 N 2 σ 4 \text{MSE}(\sigma^2_{mle}) = \frac{2N-2}{N^2} \sigma^4 + \frac{1}{N^2}\sigma^4 = \frac{2N-1}{N^2} \sigma^4 MSE ( σ m l e 2 ) = N 2 2 N − 2 σ 4 + N 2 1 σ 4 = N 2 2 N − 1 σ 4