bayesml.gaussianmixture package#
Module contents#
The Gaussian mixture model with the Gauss-Wishart prior distribution and the Dirichlet prior distribution.
The stochastic data generative model is as follows:
\(K \in \mathbb{N}\): number of latent classes
\(\boldsymbol{z} \in \{ 0, 1 \}^K\): a one-hot vector representing the latent class (latent variable)
\(\boldsymbol{\pi} \in [0, 1]^K\): a parameter for latent classes, (\(\sum_{k=1}^K \pi_k=1\))
\(D \in \mathbb{N}\): a dimension of data
\(\boldsymbol{x} \in \mathbb{R}^D\): a data point
\(\boldsymbol{\mu}_k \in \mathbb{R}^D\): a parameter
\(\boldsymbol{\mu} = \{ \boldsymbol{\mu}_k \}_{k=1}^K\)
\(\boldsymbol{\Lambda}_k \in \mathbb{R}^{D\times D}\) : a parameter (a positive definite matrix)
\(\boldsymbol{\Lambda} = \{ \boldsymbol{\Lambda}_k \}_{k=1}^K\)
\(| \boldsymbol{\Lambda}_k | \in \mathbb{R}\): the determinant of \(\boldsymbol{\Lambda}_k\)
The prior distribution is as follows:
\(\boldsymbol{m}_0 \in \mathbb{R}^{D}\): a hyperparameter
\(\kappa_0 \in \mathbb{R}_{>0}\): a hyperparameter
\(\nu_0 \in \mathbb{R}\): a hyperparameter (\(\nu_0 > D-1\))
\(\boldsymbol{W}_0 \in \mathbb{R}^{D\times D}\): a hyperparameter (a positive definite matrix)
\(\boldsymbol{\alpha}_0 \in \mathbb{R}_{> 0}^K\): a hyperparameter
\(\mathrm{Tr} \{ \cdot \}\): a trace of a matrix
\(\Gamma (\cdot)\): the gamma function
where \(B(\boldsymbol{W}_0, \nu_0)\) and \(C(\boldsymbol{\alpha}_0)\) are defined as follows:
The apporoximate posterior distribution in the \(t\)-th iteration of a variational Bayesian method is as follows:
\(\boldsymbol{x}^n = (\boldsymbol{x}_1, \boldsymbol{x}_2, \dots , \boldsymbol{x}_n) \in \mathbb{R}^{D \times n}\): given data
\(\boldsymbol{z}^n = (\boldsymbol{z}_1, \boldsymbol{z}_2, \dots , \boldsymbol{z}_n) \in \{ 0, 1 \}^{K \times n}\): latent classes of given data
\(\boldsymbol{r}_i^{(t)} = (r_{i,1}^{(t)}, r_{i,2}^{(t)}, \dots , r_{i,K}^{(t)}) \in [0, 1]^K\): a parameter for the \(i\)-th latent class. (\(\sum_{k=1}^K r_{i, k}^{(t)} = 1\))
\(\boldsymbol{m}_{n,k}^{(t)} \in \mathbb{R}^{D}\): a hyperparameter
\(\kappa_{n,k}^{(t)} \in \mathbb{R}_{>0}\): a hyperparameter
\(\nu_{n,k}^{(t)} \in \mathbb{R}\): a hyperparameter \((\nu_n > D-1)\)
\(\boldsymbol{W}_{n,k}^{(t)} \in \mathbb{R}^{D\times D}\): a hyperparameter (a positive definite matrix)
\(\boldsymbol{\alpha}_n^{(t)} \in \mathbb{R}_{> 0}^K\): a hyperparameter
where the updating rule of the hyperparameters is as follows.
The approximate predictive distribution is as follows:
\(\boldsymbol{x}_{n+1} \in \mathbb{R}^D\): a new data point
\(\boldsymbol{\mu}_{\mathrm{p},k} \in \mathbb{R}^D\): the parameter of the predictive distribution
\(\boldsymbol{\Lambda}_{\mathrm{p},k} \in \mathbb{R}^{D \times D}\): the parameter of the predictive distribution (a positive definite matrix)
\(\nu_{\mathrm{p},k} \in \mathbb{R}_{>0}\): the parameter of the predictive distribution
where the parameters are obtained from the hyperparameters of the posterior distribution as follows:
- class bayesml.gaussianmixture.GenModel(c_num_classes, c_degree, pi_vec=None, mu_vecs=None, lambda_mats=None, h_alpha_vec=None, h_m_vecs=None, h_kappas=None, h_nus=None, h_w_mats=None, seed=None)#
Bases:
Generative
The stochastic data generative model and the prior distribution
- Parameters:
- c_num_classesint
a positive integer
- c_degreeint
a positive integer
- pi_vecnumpy.ndarray, optional
a real vector in \([0, 1]^K\), by default [1/K, 1/K, … , 1/K]
- mu_vecsnumpy.ndarray, optional
vectors of real numbers, by default zero vectors.
- lambda_matsnumpy.ndarray, optional
positive definite symetric matrices, by default the identity matrices
- h_alpha_vecnumpy.ndarray, optional
a vector of positive real numbers, by default [1/2, 1/2, … , 1/2]
- h_m_vecsnumpy.ndarray, optional
vectors of real numbers, by default zero vectors
- h_kappasfloat, optional
positive real numbers, by default [1.0, 1.0, … , 1.0]
- h_nusfloat, optional
real numbers greater than c_degree-1, by default the value of
c_degree
- h_w_matsnumpy.ndarray, optional
positive definite symetric matrices, by default the identity matrices
- seed{None, int}, optional
A seed to initialize numpy.random.default_rng(), by default None
Methods
Generate the parameter from the prior distribution.
gen_sample
(sample_size)Generate a sample from the stochastic data generative model.
Get constants of GenModel.
Get the hyperparameters of the prior distribution.
Get the parameter of the sthocastic data generative model.
load_h_params
(filename)Load the hyperparameters to h_params.
load_params
(filename)Load the parameters saved by
save_params
.save_h_params
(filename)Save the hyperparameters using python
pickle
module.save_params
(filename)Save the parameters using python
pickle
module.save_sample
(filename, sample_size)Save the generated sample as NumPy
.npz
format.set_h_params
([h_alpha_vec, h_m_vecs, ...])Set the hyperparameters of the prior distribution.
set_params
([pi_vec, mu_vecs, lambda_mats])Set the parameter of the sthocastic data generative model.
visualize_model
([sample_size])Visualize the stochastic data generative model and generated samples.
- get_constants()#
Get constants of GenModel.
- Returns:
- constantsdict of {str: int, numpy.ndarray}
"c_num_classes"
: the value ofself.c_num_classes
"c_degree"
: the value ofself.c_degree
- set_h_params(h_alpha_vec=None, h_m_vecs=None, h_kappas=None, h_nus=None, h_w_mats=None)#
Set the hyperparameters of the prior distribution.
- Parameters:
- h_alpha_vecnumpy.ndarray, optional
a vector of positive real numbers, by default None
- h_m_vecsnumpy.ndarray, optional
vectors of real numbers, by default None
- h_kappasfloat, optional
positive real numbers, by default None
- h_nusfloat, optional
real numbers greater than c_degree-1, by default None
- h_w_matsnumpy.ndarray, optional
positive definite symetric matrices, by default None
- get_h_params()#
Get the hyperparameters of the prior distribution.
- Returns:
- h_params{str:float, np.ndarray}
"h_alpha_vec"
: The value ofself.h_alpha_vec
"h_m_vecs"
: The value ofself.h_m_vecs
"h_kappas"
: The value ofself.h_kappas
"h_nus"
: The value ofself.h_nus
"h_w_mats"
: The value ofself.h_w_mats
- gen_params()#
Generate the parameter from the prior distribution.
The generated vaule is set at
self.pi_vec
,self.mu_vecs
andself.lambda_mats
.
- set_params(pi_vec=None, mu_vecs=None, lambda_mats=None)#
Set the parameter of the sthocastic data generative model.
- Parameters:
- pi_vecnumpy.ndarray
a real vector in \([0, 1]^K\). The sum of its elements must be 1.
- mu_vecsnumpy.ndarray
vectors of real numbers
- lambda_matsnumpy.ndarray
positive definite symetric matrices
- get_params()#
Get the parameter of the sthocastic data generative model.
- Returns:
- params{str:float, numpy.ndarray}
"pi_vec"
: The value ofself.pi_vec
"mu_vecs"
: The value ofself.mu_vecs
"lambda_mats"
: The value ofself.lambda_mats
- gen_sample(sample_size)#
Generate a sample from the stochastic data generative model.
- Parameters:
- sample_sizeint
A positive integer
- Returns:
- xnumpy ndarray
2-dimensional array whose shape is
(sammple_size,c_degree)
and its elements are real numbers.- znumpy ndarray
2-dimensional array whose shape is
(sample_size,c_num_classes)
whose rows are one-hot vectors.
- save_sample(filename, sample_size)#
Save the generated sample as NumPy
.npz
format.It is saved as a NpzFile with keyword: “x”, “z”.
- Parameters:
- filenamestr
The filename to which the sample is saved.
.npz
will be appended if it isn’t there.- sample_sizeint
A positive integer
See also
- visualize_model(sample_size=100)#
Visualize the stochastic data generative model and generated samples.
- Parameters:
- sample_sizeint, optional
A positive integer, by default 100
Examples
>>> from bayesml import gaussianmixture >>> import numpy as np >>> model = gaussianmixture.GenModel( >>> c_num_classes=3, >>> c_degree=1 >>> pi_vec=np.array([0.444,0.444,0.112]), >>> mu_vecs=np.array([[-2.8],[-0.8],[2]]), >>> lambda_mats=np.array([[[6.25]],[[6.25]],[[100]]]) >>> ) >>> model.visualize_model() pi_vec: [0.444 0.444 0.112] mu_vecs: [[-2.8] [-0.8] [ 2. ]] lambda_mats: [[[ 6.25]] [[ 6.25]] [[100. ]]]
- class bayesml.gaussianmixture.LearnModel(c_num_classes, c_degree, h0_alpha_vec=None, h0_m_vecs=None, h0_kappas=None, h0_nus=None, h0_w_mats=None, seed=None)#
Bases:
Posterior
,PredictiveMixin
The posterior distribution and the predictive distribution.
- Parameters:
- c_num_classesint
a positive integer
- c_degreeint
a positive integer
- h0_alpha_vecnumpy.ndarray, optional
a vector of positive real numbers, by default [1/2, 1/2, … , 1/2]
- h0_m_vecsnumpy.ndarray, optional
vectors of real numbers, by default zero vectors
- h0_kappas{float, numpy.ndarray}, optional
positive real numbers, by default [1.0, 1.0, … , 1.0]
- h0_nus{float, numpy.ndarray}, optional
real numbers greater than c_degree-1, by default the value of
c_degree
- h0_w_matsnumpy.ndarray, optional
positive definite symetric matrices, by default the identity matrices
- seed{None, int}, optional
A seed to initialize numpy.random.default_rng(), by default None
- Attributes:
- h0_w_mats_invnumpy.ndarray
the inverse matrices of h0_w_mats
- hn_alpha_vecnumpy.ndarray
a vector of positive real numbers
- hn_m_vecsnumpy.ndarray
vectors of real numbers
- hn_kappasnumpy.ndarray
positive real numbers
- hn_nusnumpy.ndarray
real numbers greater than c_degree-1
- hn_w_matsnumpy.ndarray
positive definite symetric matrices
- hn_w_mats_invnumpy.ndarray
the inverse matrices of hn_w_mats
- r_vecsnumpy.ndarray
vectors of real numbers. The sum of its elenemts is 1.
- nsnumpy.ndarray
positive real numbers
- s_matsnumpy.ndarray
positive difinite symmetric matrices
- p_mu_vecsnumpy.ndarray
vectors of real numbers
- p_nusnumpy.ndarray
positive real numbers
- p_lambda_matsnumpy.ndarray
positive definite symetric matrices
Methods
Calculate the parameters of the predictive distribution.
estimate_latent_vars
(x[, loss])Estimate latent variables corresponding to x under the given criterion.
estimate_latent_vars_and_update
(x[, loss, ...])Estimate latent variables and update the posterior sequentially.
estimate_params
([loss])Estimate the parameter of the stochastic data generative model under the given criterion.
Get constants of LearnModel.
Get the initial values of the hyperparameters of the posterior distribution.
Get the hyperparameters of the posterior distribution.
Get the parameters of the predictive distribution.
load_h0_params
(filename)Load the hyperparameters to h0_params.
load_hn_params
(filename)Load the hyperparameters to hn_params.
make_prediction
([loss])Predict a new data point under the given criterion.
overwrite_h0_params
()Overwrite the initial values of the hyperparameters of the posterior distribution by the learned values.
pred_and_update
(x[, loss, max_itr, ...])Predict a new data point and update the posterior sequentially.
reset_hn_params
()Reset the hyperparameters of the posterior distribution to their initial values.
save_h0_params
(filename)Save the hyperparameters using python
pickle
module.save_hn_params
(filename)Save the hyperparameters using python
pickle
module.set_h0_params
([h0_alpha_vec, h0_m_vecs, ...])Set the hyperparameters of the prior distribution.
set_hn_params
([hn_alpha_vec, hn_m_vecs, ...])Set updated values of the hyperparameter of the posterior distribution.
update_posterior
(x[, max_itr, num_init, ...])Update the hyperparameters of the posterior distribution using traning data.
Visualize the posterior distribution for the parameter.
- get_constants()#
Get constants of LearnModel.
- Returns:
- constantsdict of {str: int, numpy.ndarray}
"c_num_classes"
: the value ofself.c_num_classes
"c_degree"
: the value ofself.c_degree
- set_h0_params(h0_alpha_vec=None, h0_m_vecs=None, h0_kappas=None, h0_nus=None, h0_w_mats=None)#
Set the hyperparameters of the prior distribution.
- Parameters:
- h0_alpha_vecnumpy.ndarray, optional
a vector of positive real numbers, by default None
- h0_m_vecsnumpy.ndarray, optional
vectors of real numbers, by default None
- h0_kappas{float, numpy.ndarray}, optional
positive real numbers, by default None
- h0_nus{float, numpy.ndarray}, optional
real numbers greater than c_degree-1, by default None
- h0_w_matsnumpy.ndarray, optional
positive definite symetric matrices, by default None
- get_h0_params()#
Get the initial values of the hyperparameters of the posterior distribution.
- Returns:
- h0_paramsdict of {str: numpy.ndarray}
"h0_alpha_vec"
: The value ofself.h0_alpha_vec
"h0_m_vecs"
: The value ofself.h0_m_vecs
"h0_kappas"
: The value ofself.h0_kappas
"h0_nus"
: The value ofself.h0_nus
"h0_w_mats"
: The value ofself.h0_w_mats
- set_hn_params(hn_alpha_vec=None, hn_m_vecs=None, hn_kappas=None, hn_nus=None, hn_w_mats=None)#
Set updated values of the hyperparameter of the posterior distribution.
- Parameters:
- hn_alpha_vecnumpy.ndarray, optional
a vector of positive real numbers, by default None
- hn_m_vecsnumpy.ndarray, optional
vectors of real numbers, by default None
- hn_kappas{float, numpy.ndarray}, optional
positive real numbers, by default None
- hn_nus{float, numpy.ndarray}, optional
real numbers greater than c_degree-1, by default None
- hn_w_matsnumpy.ndarray, optional
positive definite symetric matrices, by default None
- get_hn_params()#
Get the hyperparameters of the posterior distribution.
- Returns:
- hn_paramsdict of {str: numpy.ndarray}
"hn_alpha_vec"
: The value ofself.hn_alpha_vec
"hn_m_vecs"
: The value ofself.hn_m_vecs
"hn_kappas"
: The value ofself.hn_kappas
"hn_nus"
: The value ofself.hn_nus
"hn_w_mats"
: The value ofself.hn_w_mats
- update_posterior(x, max_itr=100, num_init=10, tolerance=1e-08, init_type='subsampling')#
Update the hyperparameters of the posterior distribution using traning data.
- Parameters:
- xnumpy.ndarray
(sample_size,c_degree)-dimensional ndarray. All the elements must be real number.
- max_itrint, optional
maximum number of iterations, by default 100
- num_initint, optional
number of initializations, by default 10
- tolerancefloat, optional
convergence croterion of variational lower bound, by default 1.0E-8
- init_typestr, optional
'subsampling'
: for each latent class, extract a subsample whose size isint(np.sqrt(x.shape[0]))
. and use its mean and covariance matrix as an initial values ofhn_m_vecs
andhn_lambda_mats
.'random_responsibility'
: randomly assign responsibility tor_vecs
Type of initialization, by default
'subsampling'
- estimate_params(loss='squared')#
Estimate the parameter of the stochastic data generative model under the given criterion.
Note that the criterion is applied to estimating
pi_vec
,mu_vecs
andlambda_mats
independently. Therefore, a tuple of the dirichlet distribution, the student’s t-distributions and the wishart distributions will be returned when loss=”KL”- Parameters:
- lossstr, optional
Loss function underlying the Bayes risk function, by default “squared”. This function supports “squared”, “0-1”, and “KL”.
- Returns:
- Estimatesa tuple of {numpy ndarray, float, None, or rv_frozen}
pi_vec_hat
: the estimate for pi_vecmu_vecs_hat
: the estimate for mu_vecslambda_mats_hat
: the estimate for lambda_mats
The estimated values under the given loss function. If it is not exist, np.nan will be returned. If the loss function is “KL”, the posterior distribution itself will be returned as rv_frozen object of scipy.stats.
- visualize_posterior()#
Visualize the posterior distribution for the parameter.
Examples
>>> from bayesml import gaussianmixture >>> gen_model = gaussianmixture.GenModel( >>> c_num_classes=2, >>> c_degree=1, >>> mu_vecs=np.array([[-2],[2]]), >>> ) >>> x,z = gen_model.gen_sample(100) >>> learn_model = gaussianmixture.LearnModel(c_num_classes=2, c_degree=1) >>> learn_model.update_posterior(x) >>> learn_model.visualize_posterior() hn_m_vecs: [[ 2.09365933] [-1.97862429]] hn_kappas: [47.68878373 54.31121627] hn_nus: [47.68878373 54.31121627] hn_w_mats: [[[0.02226992]] [[0.01575793]]] E[lambda_mats]= [[[1.06202546]] [[0.85583258]]]
- get_p_params()#
Get the parameters of the predictive distribution.
- Returns:
- p_paramsdict of {str: numpy.ndarray}
"p_mu_vecs"
: The value ofself.p_mu_vecs
"p_nus"
: The value ofself.p_nus
"p_lambda_mats"
: The value ofself.p_lambda_mats
- calc_pred_dist()#
Calculate the parameters of the predictive distribution.
- make_prediction(loss='squared')#
Predict a new data point under the given criterion.
- Parameters:
- lossstr, optional
Loss function underlying the Bayes risk function, by default “squared”. This function supports “squared” and “0-1”.
- Returns:
- predicted_value{float, numpy.ndarray}
The predicted value under the given loss function.
- pred_and_update(x, loss='squared', max_itr=100, num_init=10, tolerance=1e-08, init_type='random_responsibility')#
Predict a new data point and update the posterior sequentially.
h0_params will be overwritten by current hn_params before updating hn_params by x
- Parameters:
- xnumpy.ndarray
It must be a c_degree-dimensional vector
- lossstr, optional
Loss function underlying the Bayes risk function, by default “squared”. This function supports “squared” and “0-1”.
- max_itrint, optional
maximum number of iterations, by default 100
- num_initint, optional
number of initializations, by default 10
- tolerancefloat, optional
convergence croterion of variational lower bound, by default 1.0E-8
- init_typestr, optional
'random_responsibility'
: randomly assign responsibility tor_vecs
'subsampling'
: for each latent class, extract a subsample whose size isint(np.sqrt(x.shape[0]))
. and use its mean and covariance matrix as an initial values ofhn_m_vecs
andhn_lambda_mats
.
Type of initialization, by default
'random_responsibility'
- Returns:
- predicted_value{float, numpy.ndarray}
The predicted value under the given loss function.
- estimate_latent_vars(x, loss='0-1')#
Estimate latent variables corresponding to x under the given criterion.
Note that the criterion is independently applied to each data point.
- Parameters:
- xnumpy.ndarray
(sample_size,c_degree)-dimensional ndarray. All the elements must be real number.
- lossstr, optional
Loss function underlying the Bayes risk function, by default “0-1”. This function supports “squared”, “0-1”, and “KL”.
- Returns:
- estimatesnumpy.ndarray
The estimated values under the given loss function. If the loss function is “KL”, the posterior distribution will be returned as a numpy.ndarray whose elements consist of occurence probabilities.
- estimate_latent_vars_and_update(x, loss='0-1', max_itr=100, num_init=10, tolerance=1e-08, init_type='subsampling')#
Estimate latent variables and update the posterior sequentially.
h0_params will be overwritten by current hn_params before updating hn_params by x
- Parameters:
- xnumpy.ndarray
It must be a c_degree-dimensional vector
- lossstr, optional
Loss function underlying the Bayes risk function, by default “0-1”. This function supports “squared” and “0-1”.
- max_itrint, optional
maximum number of iterations, by default 100
- num_initint, optional
number of initializations, by default 10
- tolerancefloat, optional
convergence croterion of variational lower bound, by default 1.0E-8
- init_typestr, optional
'subsampling'
: for each latent class, extract a subsample whose size isint(np.sqrt(x.shape[0]))
. and use its mean and covariance matrix as an initial values ofhn_m_vecs
andhn_lambda_mats
.'random_responsibility'
: randomly assign responsibility tor_vecs
Type of initialization, by default
'subsampling'
- Returns:
- predicted_valuenumpy.ndarray
The estimated values under the given loss function.