| 10 | |
| 11 | class NB: |
| 12 | def fit(self, X, Y): |
| 13 | self.pyy = [] |
| 14 | self.tinfo = [] |
| 15 | N, D = X.shape |
| 16 | for c in (0, 1): |
| 17 | pyy_c = (1.0 + np.sum(Y == c)) / (N + 1.0 + 1.0) |
| 18 | self.pyy.append(pyy_c) |
| 19 | |
| 20 | # for each dimension, we need to store the data we need to calculate |
| 21 | # the posterior predictive distribution |
| 22 | # t-distribution with 3 params: df, center, scale |
| 23 | Xc = X[Y == c] |
| 24 | tinfo_c = [] |
| 25 | for d in xrange(D): |
| 26 | # first calculate the parameters of the normal gamma |
| 27 | xbar = Xc[:,d].mean() |
| 28 | mu = N*xbar / (1.0 + N) |
| 29 | precision = 1.0 + N |
| 30 | alpha = 1.0 + N/2.0 |
| 31 | beta = 1.0 + 0.5*Xc[:,d].var()*N + 0.5*N*(xbar*xbar)/precision |
| 32 | |
| 33 | tinfo_cd = { |
| 34 | 'df': 2*alpha, |
| 35 | 'center': mu, |
| 36 | 'scale': np.sqrt( beta*(precision + 1)/(alpha * precision) ), |
| 37 | } |
| 38 | tinfo_c.append(tinfo_cd) |
| 39 | self.tinfo.append(tinfo_c) |
| 40 | |
| 41 | def predict_proba(self, X): |
| 42 | N, D = X.shape |