Skip to content

Refine naming rules and descriptions #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions bayesml/gaussianmixture/_gaussianmixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def set_h0_params(
self.h0_w_mats[:] = h0_w_mats
self.h0_w_mats_inv[:] = np.linalg.inv(self.h0_w_mats)

self._calc_prior_char()
self._calc_prior_features()
self.reset_hn_params()
return self

Expand Down Expand Up @@ -634,8 +634,8 @@ def set_hn_params(
self.hn_w_mats[:] = hn_w_mats
self.hn_w_mats_inv[:] = np.linalg.inv(self.hn_w_mats)

self._calc_q_pi_char()
self._calc_q_lambda_char()
self._calc_q_pi_features()
self._calc_q_lambda_features()

self.calc_pred_dist()
return self
Expand All @@ -658,7 +658,7 @@ def get_hn_params(self):
"hn_nus":self.hn_nus,
"hn_w_mats":self.hn_w_mats}

def _calc_prior_char(self):
def _calc_prior_features(self):
self._ln_c_h0_alpha = gammaln(self.h0_alpha_vec.sum()) - gammaln(self.h0_alpha_vec).sum()
self._ln_b_h0_w_nus = (
- self.h0_nus*np.linalg.slogdet(self.h0_w_mats)[1]
Expand Down Expand Up @@ -700,13 +700,13 @@ def _calc_vl(self):
- np.sum(self.h0_w_mats_inv * self._e_lambda_mats,axis=(1,2))
) / 2.0

# E[ln q(Z|pi)]
# -E[ln q(Z|pi)]
self._vl_q_z = -np.sum(xlogy(self.r_vecs,self.r_vecs))

# E[ln q(pi)]
# -E[ln q(pi)]
self._vl_q_pi = ss_dirichlet.entropy(self.hn_alpha_vec)

# E[ln q(mu,Lambda)]
# -E[ln q(mu,Lambda)]
self._vl_q_mu_lambda = np.sum(
+ self.c_degree * (1.0 + np.log(2.0*np.pi) - np.log(self.hn_kappas))
- self._ln_b_hn_w_nus * 2.0
Expand Down Expand Up @@ -742,14 +742,14 @@ def _init_random_responsibility(self,x):
self.r_vecs[:] = self.rng.dirichlet(np.ones(self.c_num_classes),self.r_vecs.shape[0])
self._calc_n_x_bar_s(x)

def _calc_q_pi_char(self):
def _calc_q_pi_features(self):
self._e_ln_pi_vec[:] = digamma(self.hn_alpha_vec) - digamma(self.hn_alpha_vec.sum())

def _update_q_pi(self):
self.hn_alpha_vec[:] = self.h0_alpha_vec + self.ns
self._calc_q_pi_char()
self._calc_q_pi_features()

def _calc_q_lambda_char(self):
def _calc_q_lambda_features(self):
self._e_lambda_mats[:] = self.hn_nus[:,np.newaxis,np.newaxis] * self.hn_w_mats
self._e_ln_lambda_dets[:] = (np.sum(digamma((self.hn_nus[:,np.newaxis]-np.arange(self.c_degree)) / 2.0),axis=1)
+ self.c_degree*np.log(2.0)
Expand All @@ -774,7 +774,7 @@ def _update_q_mu_lambda(self):
@ (self.x_bar_vecs - self.h0_m_vecs)[:,np.newaxis,:])
)
self.hn_w_mats[:] = np.linalg.inv(self.hn_w_mats_inv)
self._calc_q_lambda_char()
self._calc_q_lambda_features()

def _update_q_z(self,x):
self._ln_rho[:] = (self._e_ln_pi_vec
Expand All @@ -801,7 +801,7 @@ def _init_subsampling(self,x):
/ _size * self.hn_nus[k]
+ np.eye(self.c_degree) * 1.0E-5) # avoid singular matrix
self.hn_w_mats[k] = np.linalg.inv(self.hn_w_mats_inv[k])
self._calc_q_lambda_char()
self._calc_q_lambda_features()

def _init_rho_r(self):
self._ln_rho[:] = 0.0
Expand All @@ -820,6 +820,7 @@ def update_posterior(
Parameters
----------
x : numpy.ndarray
(sample_size,c_degree)-dimensional ndarray.
All the elements must be real number.
max_itr : int, optional
maximum number of iterations, by default 100
Expand Down Expand Up @@ -897,8 +898,8 @@ def update_posterior(
self.hn_nus[:] = tmp_nus
self.hn_w_mats[:] = tmp_w_mats
self.hn_w_mats_inv[:] = tmp_w_mats_inv
self._calc_q_pi_char()
self._calc_q_lambda_char()
self._calc_q_pi_features()
self._calc_q_lambda_features()
self._update_q_z(x)
return self

Expand Down Expand Up @@ -1168,6 +1169,9 @@ def estimate_latent_vars(self,x,loss="0-1"):

Parameters
----------
x : numpy.ndarray
(sample_size,c_degree)-dimensional ndarray.
All the elements must be real number.
loss : str, optional
Loss function underlying the Bayes risk function, by default \"0-1\".
This function supports \"squared\", \"0-1\", and \"KL\".
Expand Down
38 changes: 19 additions & 19 deletions bayesml/hiddenmarkovnormal/_hiddenmarkovnormal.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def set_h0_params(

self.h0_w_mats_inv[:] = np.linalg.inv(self.h0_w_mats)

self._calc_prior_char()
self._calc_prior_features()
self.reset_hn_params()
return self

Expand Down Expand Up @@ -796,9 +796,9 @@ def set_hn_params(

self.hn_w_mats_inv[:] = np.linalg.inv(self.hn_w_mats)

self._calc_q_pi_char()
self._calc_q_a_char()
self._calc_q_lambda_char()
self._calc_q_pi_features()
self._calc_q_a_features()
self._calc_q_lambda_features()

self.calc_pred_dist()
return self
Expand All @@ -823,7 +823,7 @@ def get_hn_params(self):
'hn_nus':self.hn_nus,
'hn_w_mats':self.hn_w_mats}

def _calc_prior_char(self):
def _calc_prior_features(self):
self._ln_c_h0_eta_vec = gammaln(self.h0_eta_vec.sum()) - gammaln(self.h0_eta_vec).sum()
self._ln_c_h0_zeta_vecs_sum = np.sum(gammaln(self.h0_zeta_vecs.sum(axis=1)) - gammaln(self.h0_zeta_vecs).sum(axis=1))
self._ln_b_h0_w_nus = (
Expand Down Expand Up @@ -851,16 +851,16 @@ def _calc_n_m_x_bar_s(self,x):
@ (x[:,np.newaxis,:] - self.x_bar_vecs[indices])[:,:,np.newaxis,:]),
axis=0) / self.ns[indices,np.newaxis,np.newaxis]

def _calc_q_pi_char(self):
def _calc_q_pi_features(self):
self._ln_pi_tilde_vec[:] = digamma(self.hn_eta_vec) - digamma(self.hn_eta_vec.sum())
self._pi_tilde_vec[:] = np.exp(self._ln_pi_tilde_vec - self._ln_pi_tilde_vec.max())

def _calc_q_a_char(self):
def _calc_q_a_features(self):
self._ln_a_tilde_mat[:] = digamma(self.hn_zeta_vecs) - digamma(self.hn_zeta_vecs.sum(axis=1,keepdims=True))
self._a_tilde_mat[:] = np.exp(self._ln_a_tilde_mat - self._ln_a_tilde_mat.max())
self._ln_c_hn_zeta_vecs_sum = np.sum(gammaln(self.hn_zeta_vecs.sum(axis=1)) - gammaln(self.hn_zeta_vecs).sum(axis=1))

def _calc_q_lambda_char(self):
def _calc_q_lambda_features(self):
self._e_lambda_mats[:] = self.hn_nus[:,np.newaxis,np.newaxis] * self.hn_w_mats
self._e_ln_lambda_dets[:] = (np.sum(digamma((self.hn_nus[:,np.newaxis]-np.arange(self.c_degree)) / 2.0),axis=1)
+ self.c_degree*np.log(2.0)
Expand Down Expand Up @@ -908,19 +908,19 @@ def _calc_vl(self):
- np.sum(self.h0_w_mats_inv * self._e_lambda_mats,axis=(1,2))
) / 2.0

# E[ln q(Z|pi)]
# -E[ln q(Z|pi)]
self._vl_q_z = (-(self.gamma_vecs * self._ln_rho).sum()
-(self.ms * (self._ln_a_tilde_mat - self._ln_a_tilde_mat.max())).sum()
-(self.gamma_vecs[0] * (self._ln_pi_tilde_vec - self._ln_pi_tilde_vec.max())).sum()
+np.log(self._cs).sum())

# E[ln q(pi)]
# -E[ln q(pi)]
self._vl_q_pi = ss_dirichlet.entropy(self.hn_eta_vec)

# E[ln p(A)]
# -E[ln p(A)]
self._vl_q_a = -self._ln_c_hn_zeta_vecs_sum - ((self.hn_zeta_vecs - 1) * self._ln_a_tilde_mat).sum()

# E[ln q(mu,Lambda)]
# -E[ln q(mu,Lambda)]
self._vl_q_mu_lambda = np.sum(
+ self.c_degree * (1.0 + np.log(2.0*np.pi) - np.log(self.hn_kappas))
- self._ln_b_hn_w_nus * 2.0
Expand Down Expand Up @@ -968,7 +968,7 @@ def _init_subsampling(self,x):
/ _size * self.hn_nus[k]
+ np.eye(self.c_degree) * 1.0E-5) # avoid singular matrix
self.hn_w_mats[k] = np.linalg.inv(self.hn_w_mats_inv[k])
self._calc_q_lambda_char()
self._calc_q_lambda_features()

def _update_q_mu_lambda(self):
self.hn_kappas[:] = self.h0_kappas + self.ns
Expand All @@ -982,15 +982,15 @@ def _update_q_mu_lambda(self):
@ (self.x_bar_vecs - self.h0_m_vecs)[:,np.newaxis,:])
)
self.hn_w_mats[:] = np.linalg.inv(self.hn_w_mats_inv)
self._calc_q_lambda_char()
self._calc_q_lambda_features()

def _update_q_pi(self):
self.hn_eta_vec[:] = self.h0_eta_vec + self.ns
self._calc_q_pi_char()
self._calc_q_pi_features()

def _update_q_a(self):
self.hn_zeta_vecs[:] = self.h0_zeta_vecs + self.ms
self._calc_q_a_char()
self._calc_q_a_features()

def _calc_rho(self,x):
self._ln_rho[:] = ((self._e_ln_lambda_dets
Expand Down Expand Up @@ -1135,9 +1135,9 @@ def update_posterior(
self.hn_nus[:] = tmp_nus
self.hn_w_mats[:] = tmp_w_mats
self.hn_w_mats_inv[:] = tmp_w_mats_inv
self._calc_q_pi_char()
self._calc_q_a_char()
self._calc_q_lambda_char()
self._calc_q_pi_features()
self._calc_q_a_features()
self._calc_q_lambda_features()
self._update_q_z(x)
return self

Expand Down
6 changes: 3 additions & 3 deletions doc/devdoc/vb_method.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ $$
# E[ln p(eta)]
self._vl_p_eta =

# E[ln p(theta)]
self._vl_p_theta =
# -E[ln q(theta)]
self._vl_q_theta =

# E[ln q(eta)]
# -E[ln q(eta)]
self._vl_q_eta =

self.vl = (self._vl_p_x
Expand Down