Skip to content

Commit 1bfde55

Browse files
committed
Modification for web pages
1 parent d7e9638 commit 1bfde55

28 files changed

+170
-114
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ For more details, see our [website](https://yuta-nakahara.github.io/BayesML/ "Ba
2323

2424
Please use the following commands to install BayesML.
2525

26-
``` shell
26+
``` bash
2727
pip install bayesml
2828
```
2929

README_jp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ BayesMLは以下の特徴を持っています.
2222

2323
以下のコマンドによりインストール可能です.
2424

25-
``` shell
25+
``` bash
2626
pip install bayesml
2727
```
2828

bayesml/_test.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
from bayesml import bernoulli
2-
3-
gen_model = bernoulli.GenModel(theta=0.7)
4-
5-
gen_model.visualize_model()
6-
7-
x = gen_model.gen_sample(sample_size=20)
8-
9-
learn_model = bernoulli.LearnModel()
10-
11-
learn_model.visualize_posterior()
12-
13-
learn_model.update_posterior(x)
14-
learn_model.visualize_posterior()
15-
16-
print(learn_model.estimate_params(loss='squared'))
17-
print(learn_model.estimate_params(loss='abs'))
18-
print(learn_model.estimate_params(loss='0-1'))
1+
from bayesml import hiddenmarkovnormal
2+
import numpy as np
3+
model = hiddenmarkovnormal.GenModel(
4+
c_num_classes=2,
5+
c_degree=1,
6+
mu_vecs=np.array([[5],[-5]]),
7+
a_mat=np.array([[0.95,0.05],[0.1,0.9]]))
8+
model.visualize_model()

bayesml/contexttree/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
where the updating rule of the hyperparameter is as follows:
5555
5656
.. math::
57-
\beta_n(k|s) = \beta_0(k|s) + \sum_{i=1}^n I \left\{ \text{:math:`s` is the ancestor of :math:`s_{T_\mathrm{max}}(x^{i-1})` and :math:`x_i=k` } \right\}.
57+
\beta_n(k|s) = \beta_0(k|s) + \sum_{i=1}^n I \left\{ s \ \mathrm{is \ the \ ancestor \ of} \ s_{T_\mathrm{max}}(x^{i-1}) \ \mathrm{and} \ x_i=k \right\}.
5858
5959
For :math:`T \in \mathcal{T}`,
6060
@@ -66,18 +66,18 @@
6666
.. math::
6767
g_{n,s} =
6868
\begin{cases}
69-
g_{0,s} & \text{if :math:`n=0`}, \\
69+
g_{0,s}, & n=0, \\
7070
\frac{ g_{n-1,s} \tilde{q}_{s_{\mathrm{child}}} (x_n|x^{n-1}) }
71-
{ \tilde{q}_s(x_n|x^{n-1}) } & \text{otherwise},
71+
{ \tilde{q}_s(x_n|x^{n-1}) } & \mathrm{otherwise},
7272
\end{cases}
7373
7474
where :math:`s_{\mathrm{child}}` is the child node of :math:`s` on the path from :math:`s_\lambda` to :math:`s_{T_\mathrm{max}}(x^n)` and
7575
7676
.. math::
7777
\tilde{q}_s(x_n|x^{n-1}) =
7878
\begin{cases}
79-
q_s(x_n|x^{n-1}) & \text{if :math:`s\in\mathcal{L}(T_\mathrm{max})`}, \\
80-
(1-g_{n-1,s}) q_s(x_n|x^{n-1}) + g_{n-1,s} \tilde{q}_{s_{\mathrm{child}}}(x_n|x^{n-1}) & \text{otherwise}.
79+
q_s(x_n|x^{n-1}) & s\in\mathcal{L}(T_\mathrm{max}), \\
80+
(1-g_{n-1,s}) q_s(x_n|x^{n-1}) + g_{n-1,s} \tilde{q}_{s_{\mathrm{child}}}(x_n|x^{n-1}) & \mathrm{otherwise}.
8181
\end{cases}
8282
8383
Here,

bayesml/contexttree/_contexttree.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _visualize_model_recursion(self,tree_graph,node,node_id,parent_id,sibling_nu
191191
tmp_p_v = p_v
192192

193193
# add node information
194-
label_string = f'h_g={node.h_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec='
194+
label_string = f'h_g={node.h_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec\\l='
195195
if node.leaf:
196196
label_string += '['
197197
for i in range(self.c_k):
@@ -388,16 +388,16 @@ def visualize_model(self,filename=None,format=None,sample_length=10):
388388
Examples
389389
--------
390390
>>> from bayesml import contexttree
391-
>>> model = contexttree.GenModel(c_k=2,c_d_max=3,h_g=0.75)
391+
>>> gen_model = contexttree.GenModel(c_k=2,c_d_max=3,h_g=0.75)
392392
>>> gen_model.gen_params()
393-
>>> model.visualize_model()
394-
[1 1 1 1 1 1 0 0 0 1]
393+
>>> gen_model.visualize_model()
394+
[1 0 1 0 0 0 1 0 0 0]
395395
396396
.. image:: ./images/contexttree_example.png
397397
398398
See Also
399399
--------
400-
graphbiz.Digraph
400+
graphviz.Digraph
401401
"""
402402
#例外処理
403403
_check.pos_int(sample_length,'sample_length',DataFormatError)
@@ -781,7 +781,7 @@ def estimate_params(self,loss="0-1",visualize=True,filename=None,format=None):
781781
782782
See Also
783783
--------
784-
graphbiz.Digraph
784+
graphviz.Digraph
785785
"""
786786

787787
if loss == "0-1":
@@ -820,7 +820,7 @@ def _visualize_model_recursion(self,tree_graph,node:_LearnNode,node_id,parent_id
820820
tmp_p_v = p_v
821821

822822
# add node information
823-
label_string = f'hn_g={node.hn_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec='
823+
label_string = f'hn_g={node.hn_g:.2f}\\lp_v={tmp_p_v:.2f}\\ltheta_vec\\l='
824824
label_string += '['
825825
for i in range(self.c_k):
826826
theta_vec_hat = node.hn_beta_vec / node.hn_beta_vec.sum()
@@ -869,7 +869,7 @@ def visualize_posterior(self,filename=None,format=None):
869869
870870
See Also
871871
--------
872-
graphbiz.Digraph
872+
graphviz.Digraph
873873
"""
874874
try:
875875
import graphviz

bayesml/gaussianmixture/_gaussianmixture.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,7 @@ def visualize_model(self,sample_size=100):
298298
[ 2. ]]
299299
lambda_mats:
300300
[[[ 6.25]]
301-
302301
[[ 6.25]]
303-
304302
[[100. ]]]
305303
306304
.. image:: ./images/gaussianmixture_example.png
@@ -535,7 +533,7 @@ def set_h0_params(
535533
self.h0_w_mats[:] = h0_w_mats
536534
self.h0_w_mats_inv[:] = np.linalg.inv(self.h0_w_mats)
537535

538-
self.calc_prior_char()
536+
self._calc_prior_char()
539537
self.reset_hn_params()
540538

541539
def get_h0_params(self):
@@ -635,7 +633,7 @@ def get_hn_params(self):
635633
"hn_nus":self.hn_nus,
636634
"hn_w_mats":self.hn_w_mats}
637635

638-
def calc_prior_char(self):
636+
def _calc_prior_char(self):
639637
self._ln_c_h0_alpha = gammaln(self.h0_alpha_vec.sum()) - gammaln(self.h0_alpha_vec).sum()
640638
self._ln_b_h0_w_nus = (
641639
- self.h0_nus*np.linalg.slogdet(self.h0_w_mats)[1]
@@ -645,7 +643,7 @@ def calc_prior_char(self):
645643
axis=1) * 2.0
646644
) / 2.0
647645

648-
def calc_vl(self):
646+
def _calc_vl(self):
649647
# E[ln p(X|Z,mu,Lambda)]
650648
self._vl_p_x = np.sum(
651649
self.ns
@@ -797,10 +795,10 @@ def update_posterior(
797795
tolerance : float, optional
798796
convergence croterion of variational lower bound, by default 1.0E-8
799797
init_type : str, optional
800-
type of initialization, by default 'subsampling'
801-
* 'subsampling': for each latent class, extract a subsample whose size is int(np.sqrt(x.shape[0])).
802-
and use its mean and covariance matrix as an initial values of hn_m_vecs and hn_lambda_mats.
803-
* 'random_responsibility': randomly assign responsibility to r_vecs
798+
* ``'subsampling'``: for each latent class, extract a subsample whose size is ``int(np.sqrt(x.shape[0]))``.
799+
and use its mean and covariance matrix as an initial values of ``hn_m_vecs`` and ``hn_lambda_mats``.
800+
* ``'random_responsibility'``: randomly assign responsibility to ``r_vecs``
801+
Type of initialization, by default ``'subsampling'``
804802
"""
805803
_check.float_vecs(x,'x',DataFormatError)
806804
if x.shape[-1] != self.c_degree:
@@ -833,14 +831,14 @@ def update_posterior(
833831
f'init_type={init_type} is unsupported. '
834832
+ 'This function supports only '
835833
+ '"subsampling" and "random_responsibility"'))
836-
self.calc_vl()
834+
self._calc_vl()
837835
print(f'\r{i}. VL: {self.vl}',end='')
838836
for t in range(max_itr):
839837
vl_before = self.vl
840838
self._update_q_mu_lambda()
841839
self._update_q_pi()
842840
self._update_q_z(x)
843-
self.calc_vl()
841+
self._calc_vl()
844842
print(f'\r{i}. VL: {self.vl} t={t} ',end='')
845843
if np.abs((self.vl-vl_before)/vl_before) < tolerance:
846844
convergence_flag = False
@@ -959,11 +957,9 @@ def visualize_posterior(self):
959957
[47.68878373 54.31121627]
960958
hn_w_mats:
961959
[[[0.02226992]]
962-
963960
[[0.01575793]]]
964961
E[lambda_mats]=
965962
[[[1.06202546]]
966-
967963
[[0.85583258]]]
968964
969965
.. image:: ./images/gaussianmixture_posterior.png
@@ -1056,7 +1052,7 @@ def make_prediction(self,loss="squared"):
10561052
10571053
Returns
10581054
-------
1059-
Predicted_value : {float, numpy.ndarray}
1055+
predicted_value : {float, numpy.ndarray}
10601056
The predicted value under the given loss function.
10611057
"""
10621058
if loss == "squared":
@@ -1105,14 +1101,14 @@ def pred_and_update(
11051101
tolerance : float, optional
11061102
convergence croterion of variational lower bound, by default 1.0E-8
11071103
init_type : str, optional
1108-
type of initialization, by default 'random_responsibility'
1109-
* 'random_responsibility': randomly assign responsibility to r_vecs
1110-
* 'subsampling': for each latent class, extract a subsample whose size is int(np.sqrt(x.shape[0])).
1111-
and use its mean and covariance matrix as an initial values of hn_m_vecs and hn_lambda_mats.
1104+
* ``'random_responsibility'``: randomly assign responsibility to ``r_vecs``
1105+
* ``'subsampling'``: for each latent class, extract a subsample whose size is ``int(np.sqrt(x.shape[0]))``.
1106+
and use its mean and covariance matrix as an initial values of ``hn_m_vecs`` and ``hn_lambda_mats``.
1107+
Type of initialization, by default ``'random_responsibility'``
11121108
11131109
Returns
11141110
-------
1115-
Predicted_value : {float, numpy.ndarray}
1111+
predicted_value : {float, numpy.ndarray}
11161112
The predicted value under the given loss function.
11171113
"""
11181114
_check.float_vec(x,'x',DataFormatError)
@@ -1143,7 +1139,7 @@ def estimate_latent_vars(self,x,loss="0-1"):
11431139
11441140
Returns
11451141
-------
1146-
Estimates : numpy.ndarray
1142+
estimates : numpy.ndarray
11471143
The estimated values under the given loss function.
11481144
If the loss function is \"KL\", the posterior distribution will be returned
11491145
as a numpy.ndarray whose elements consist of occurence probabilities.
@@ -1196,14 +1192,14 @@ def estimate_latent_vars_and_update(
11961192
tolerance : float, optional
11971193
convergence croterion of variational lower bound, by default 1.0E-8
11981194
init_type : str, optional
1199-
type of initialization, by default 'subsampling'
1200-
* 'subsampling': for each latent class, extract a subsample whose size is int(np.sqrt(x.shape[0])).
1201-
and use its mean and covariance matrix as an initial values of hn_m_vecs and hn_lambda_mats.
1202-
* 'random_responsibility': randomly assign responsibility to r_vecs
1195+
* ``'subsampling'``: for each latent class, extract a subsample whose size is ``int(np.sqrt(x.shape[0]))``.
1196+
and use its mean and covariance matrix as an initial values of ``hn_m_vecs`` and ``hn_lambda_mats``.
1197+
* ``'random_responsibility'``: randomly assign responsibility to ``r_vecs``
1198+
Type of initialization, by default ``'subsampling'``
12031199
12041200
Returns
12051201
-------
1206-
Predicted_value : numpy.ndarray
1202+
predicted_value : numpy.ndarray
12071203
The estimated values under the given loss function.
12081204
"""
12091205
z_hat = self.estimate_latent_vars(x,loss=loss)

bayesml/hiddenmarkovnormal/_hiddenmarkovnormal.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ def visualize_model(self,sample_length=200):
391391
[-5.]]
392392
lambda_mats:
393393
[[[1.]]
394-
395394
[[1.]]]
396395
397396
.. image:: ./images/hiddenmarkovnormal_example.png
@@ -843,7 +842,7 @@ def _calc_q_lambda_char(self):
843842
axis=1) * 2.0
844843
) / 2.0
845844

846-
def calc_vl(self):
845+
def _calc_vl(self):
847846
# E[ln p(X|Z,mu,Lambda)]
848847
self._vl_p_x = np.sum(
849848
self.ns
@@ -1025,10 +1024,10 @@ def update_posterior(
10251024
tolerance : float, optional
10261025
convergence criterion of variational lower bound, by default 1.0E-8
10271026
init_type : str, optional
1028-
type of initialization, by default 'subsampling'
1029-
* 'subsampling': for each latent class, extract a subsample whose size is int(np.sqrt(x.shape[0])).
1027+
* ``'subsampling'``: for each latent class, extract a subsample whose size is ``int(np.sqrt(x.shape[0]))``,
10301028
and use its mean and covariance matrix as an initial values of hn_m_vecs and hn_lambda_mats.
1031-
* 'random_responsibility': randomly assign responsibility to gamma_vecs
1029+
* ``'random_responsibility'``: randomly assign responsibility to gamma_vecs
1030+
Type of initialization, by default 'subsampling'
10321031
"""
10331032
_check.float_vecs(x,'x',DataFormatError)
10341033
_check.shape_consistency(
@@ -1069,15 +1068,15 @@ def update_posterior(
10691068
f'init_type={init_type} is unsupported. '
10701069
+ 'This function supports only '
10711070
+ '"subsampling" and "random_responsibility"'))
1072-
self.calc_vl()
1071+
self._calc_vl()
10731072
print(f'\r{i}. VL: {self.vl}',end='')
10741073
for t in range(max_itr):
10751074
vl_before = self.vl
10761075
self._update_q_mu_lambda()
10771076
self._update_q_pi()
10781077
self._update_q_a()
10791078
self._update_q_z(x)
1080-
self.calc_vl()
1079+
self._calc_vl()
10811080
print(f'\r{i}. VL: {self.vl} t={t} ',end='')
10821081
if np.abs((self.vl-vl_before)/vl_before) < tolerance:
10831082
convergence_flag = False
@@ -1221,11 +1220,9 @@ def visualize_posterior(self):
12211220
[154.15657765 47.84342235]
12221221
hn_w_mats:
12231222
[[[0.00525177]]
1224-
12251223
[[0.02569298]]]
12261224
E[lambda_mats]=
12271225
[[[0.8095951 ]]
1228-
12291226
[[1.22924015]]]
12301227
12311228
.. image:: ./images/hiddenmarkovnormal_posterior.png
@@ -1375,10 +1372,10 @@ def pred_and_update(
13751372
tolerance : float, optional
13761373
convergence croterion of variational lower bound, by default 1.0E-8
13771374
init_type : str, optional
1378-
type of initialization, by default 'random_responsibility'
1379-
* 'random_responsibility': randomly assign responsibility to r_vecs
1380-
* 'subsampling': for each latent class, extract a subsample whose size is int(np.sqrt(x.shape[0])).
1375+
* ``'random_responsibility'``: randomly assign responsibility to ``xi_mats`` and ``gamma_vecs``
1376+
* ``'subsampling'``: for each latent class, extract a subsample whose size is ``int(np.sqrt(x.shape[0]))``.
13811377
and use its mean and covariance matrix as an initial values of hn_m_vecs and hn_lambda_mats.
1378+
Type of initialization, by default 'random_responsibility'
13821379
13831380
Returns
13841381
-------
@@ -1508,10 +1505,10 @@ def estimate_latent_vars_and_update(
15081505
tolerance : float, optional
15091506
convergence croterion of variational lower bound, by default 1.0E-8
15101507
init_type : str, optional
1511-
type of initialization, by default 'random_responsibility'
1512-
* 'random_responsibility': randomly assign responsibility to r_vecs
1513-
* 'subsampling': for each latent class, extract a subsample whose size is int(np.sqrt(x.shape[0])).
1508+
* ``'random_responsibility'``: randomly assign responsibility to ``xi_mats`` and ``gamma_vecs``
1509+
* ``'subsampling'``: for each latent class, extract a subsample whose size is ``int(np.sqrt(x.shape[0]))``.
15141510
and use its mean and covariance matrix as an initial values of hn_m_vecs and hn_lambda_mats.
1511+
Type of initialization, by default ``'random_responsibility'``
15151512
15161513
Returns
15171514
-------

bayesml/metatree/_metatree_x_discrete.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ def visualize_model(self,filename=None,format=None,sample_size=10):
596596
597597
See Also
598598
--------
599-
graphbiz.Digraph
599+
graphviz.Digraph
600600
"""
601601
#例外処理
602602
_check.pos_int(sample_size,'sample_size',DataFormatError)
@@ -1208,7 +1208,7 @@ def estimate_params(self,loss="0-1",visualize=True,filename=None,format=None):
12081208
12091209
See Also
12101210
--------
1211-
graphbiz.Digraph
1211+
graphviz.Digraph
12121212
"""
12131213

12141214
if loss == "0-1":
@@ -1293,7 +1293,7 @@ def visualize_posterior(self,filename=None,format=None):
12931293
12941294
See Also
12951295
--------
1296-
graphbiz.Digraph
1296+
graphviz.Digraph
12971297
"""
12981298
MAP_index = np.argmax(self.hn_metatree_prob_vec)
12991299
print(f'MAP probability of metatree:{self.hn_metatree_prob_vec[MAP_index]}')

doc/bayesml.autoregressive.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
bayesml.autoregressive package
22
==============================
33

4+
.. image:: ./images/autoregressive_example.png
5+
46
Module contents
57
---------------
68

0 commit comments

Comments
 (0)