MCPcopy Index your code
hub / github.com/ddbourgin/numpy-ml / _maximize_alpha

Method _maximize_alpha

numpy_ml/lda/lda.py:99–135  ·  view source on GitHub ↗

Optimize alpha using Blei's O(n) Newton-Raphson modification for a Hessian with special structure

(self, max_iters=1000, tol=0.1)

Source from the content-addressed store, hash-verified

97 return beta
98
99 def _maximize_alpha(self, max_iters=1000, tol=0.1):
100 """
101 Optimize alpha using Blei's O(n) Newton-Raphson modification
102 for a Hessian with special structure
103 """
104 D = self.D
105 T = self.T
106
107 alpha = self.alpha
108 gamma = self.gamma
109
110 for _ in range(max_iters):
111 alpha_old = alpha
112
113 # Calculate gradient
114 g = D * (digamma(np.sum(alpha)) - digamma(alpha)) + np.sum(
115 digamma(gamma) - np.tile(digamma(np.sum(gamma, axis=1)), (T, 1)).T,
116 axis=0,
117 )
118
119 # Calculate Hessian diagonal component
120 h = -D * polygamma(1, alpha)
121
122 # Calculate Hessian constant component
123 z = D * polygamma(1, np.sum(alpha))
124
125 # Calculate constant
126 c = np.sum(g / h) / (z ** (-1.0) + np.sum(h ** (-1.0)))
127
128 # Update alpha
129 alpha = alpha - (g - c) / h
130
131 # Check convergence
132 if np.sqrt(np.mean(np.square(alpha - alpha_old))) < tol:
133 break
134
135 return alpha
136
137 def _E_step(self):
138 """

Callers 1

_M_stepMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected