-
Notifications
You must be signed in to change notification settings - Fork 0
/
GMM.py
56 lines (46 loc) · 1.85 KB
/
GMM.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
# -*- coding: utf-8 -*-
"""
@author: Po-Kan (William) Shih
@advisor: Dr.Bahman Moraffah
Visualize 1-D Gaussian mixture model
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as st
plt.rcParams['figure.dpi'] = 80
plt.rcParams['font.size'] = 10
np.random.seed(0)
N = 500 # sample size
comp_prob = [0.3, 0.4, 0.3] # probability of each component
mean = [0.0, 3.0, 6.0] # mean of each Gaussian component
var = [1.5, 1.0, 1.2] # variance of each Gaussian component
# generate random sample
sample = np.zeros((N,1))
for i in range(N):
component = np.random.multinomial(1, comp_prob, size = 1)
point = component[0,0] * np.random.normal(mean[0], np.sqrt(var[0]), 1) + \
component[0,1] * np.random.normal(mean[1], np.sqrt(var[1]), 1) + \
component[0,2] * np.random.normal(mean[2], np.sqrt(var[2]), 1)
sample[i] = point
# plot histogram of samples
plt.hist(sample, bins = 50)
plt.show()
# compute probabilities of samples
sample_prob = np.zeros((N,1))
for i in range(N):
prob = comp_prob[0] * st.norm(mean[0], np.sqrt(var[0])).pdf(sample[i]) + \
comp_prob[1] * st.norm(mean[1], np.sqrt(var[1])).pdf(sample[i]) + \
comp_prob[2] * st.norm(mean[2], np.sqrt(var[2])).pdf(sample[i])
sample_prob[i] = prob
# plot the PDF of the GMM
x = np.linspace(-3, 9, N)
xpdf = comp_prob[0] * st.norm(mean[0], np.sqrt(var[0])).pdf(x)
ypdf = comp_prob[1] * st.norm(mean[1], np.sqrt(var[1])).pdf(x)
zpdf = comp_prob[2] * st.norm(mean[2], np.sqrt(var[2])).pdf(x)
sup = xpdf + ypdf + zpdf
plt.plot(x, xpdf, 'b', label = '$\mu$ = 0, $\sigma^2$ = 1.5, $\pi$ = 0.3')
plt.plot(x, ypdf, 'r', label = '$\mu$ = 3, $\sigma^2$ = 1, $\pi$ = 0.4')
plt.plot(x, zpdf, 'g', label = '$\mu$ = 6, $\sigma^2$ = 1.2, $\pi$ = 0.3')
plt.plot(x, sup, 'k')
plt.legend()
plt.show()