Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b7ee9de
First python mixing implementation
EnricoPaglia Jun 23, 2022
a88cd81
Change DP.py name
EnricoPaglia Jun 24, 2022
31489e0
Namespace for mixing changed
EnricoPaglia Jun 25, 2022
d1a7855
Removing pass and removing unnecessary python code from mixing.py
EnricoPaglia Jun 26, 2022
a56b255
Merged py_hier_pr
EnricoPaglia Jun 30, 2022
768eef0
change convert proto to conda prefix
EnricoPaglia Jun 30, 2022
9796b11
merged py_hier_pr
taguhiM Jul 2, 2022
a35bf4f
added notebook
taguhiM Jul 2, 2022
ac21c38
updated the notebook
taguhiM Jul 3, 2022
5602d0f
Merge branch 'py_hier_pr' into py_mix_pr
taguhiM Jul 4, 2022
8bf626a
removed comments
taguhiM Jul 4, 2022
cd7e55b
Merge branch 'py_hier_pr' into py_mix_pr
taguhiM Jul 4, 2022
0ec52aa
updates readme
EnricoPaglia Jul 4, 2022
ef83e9d
Merge branch 'py_hier_pr' into py_mix_pr
EnricoPaglia Jul 5, 2022
f801e2f
path in notebooks
EnricoPaglia Jul 5, 2022
c36d6ec
Merge branch 'py_hier_pr' into py_mix_pr
EnricoPaglia Jul 6, 2022
54e9b65
Merge branch 'py_hier_pr' into py_mix_pr
EnricoPaglia Jul 6, 2022
ed987de
Merge branch 'py_hier_pr' into py_mix_pr
EnricoPaglia Jul 6, 2022
0614ad5
Merge branch 'bayesmix-dev:master' into py_mix_pr
taguhiM Jul 7, 2022
d44d1a4
style
EnricoPaglia Jul 7, 2022
371e000
added notebook for py and cpp comparison
taguhiM Jul 9, 2022
c8c86bb
Documented DP_mixing.py
gbpollam Jul 9, 2022
1486877
added exception messages for non-existent implementation files
taguhiM Jul 10, 2022
e0be0a9
added logging.error, added docs and rst
taguhiM Nov 12, 2022
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
119 changes: 119 additions & 0 deletions docs/examples/DP_mixing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
"""
This file contains the implementation of the EPPF induced by the Dirithclet process (DP)
introduced in Ferguson (1973), see also Sethuraman (1994).
The EPPF induced by the DP depends on a `totalmass` parameter M.
Given a clustering of n elements into k clusters, each with cardinality
n_j (j = 1, ..., k) the EPPF of the DP gives the following
probabilities for the cluster membership of the (n+1)-th observation:

p(j-th cluster | ...) = n_j / (n + M)
p(new cluster | ...) = M / (n + M)

The state is solely composed of M, but we also store log(M) for efficiency
reasons. For more information about the class, please refer instead to base
classes, `AbstractMixing` and `BaseMixing`.
"""

import numpy as np


def initialize_state():
""" Initializing the state (total mass parameter M)

Returns
-------
float
initial value of the total mass parameter M
"""
total_mass = 5
return [total_mass]


def update_state(state, prior_values, allocation_size, unique_values):
""" Updating the state (total mass parameter M)

Returns
-------
float
updated value of the total mass parameter M
"""
return state


def mass_existing_cluster(n, n_clust, log, propto, hier_card, state):
""" Returns probability mass for an old cluster (for marginal mixings only)

Parameters
----------
n : int
Total dataset size
n_clust : int
Number of clusters
log : bool
Whether to return logarithm-scale values or not
propto : bool
Whether to include normalizing constants or not
hier_card : int
Number of data points assigned to the `Hierarchy` object representing the cluster
state : :obj:`list` of :obj:`float`
State (total mass parameter M) values for each cluster

Returns
-------
float
probability mass
"""
total_mass = state[0]
if log:
out = np.log(hier_card)
if not propto:
out -= np.log(n + total_mass)
else:
out = hier_card
if not propto:
out /= (n + total_mass)
return out


def mass_new_cluster(n, n_clust, log, propto, state):
""" Returns probability mass for a new cluster (for marginal mixings only)

Parameters
----------
n : int
Total dataset size
n_clust : int
Number of clusters
log : bool
Whether to return logarithm-scale values or not
propto : bool
Whether to include normalizing constants or not
state : :obj:`list` of :obj:`float`
State (total mass parameter M) values for each cluster

Returns
-------
float
probability mass
"""
total_mass = state[0]
if log:
out = np.log(total_mass)
if not propto:
out -= np.log(n + total_mass)
else:
out = total_mass
if not propto:
out /= (n + total_mass)
return out


def is_conditional():
"""

Returns
-------
:bool:
True for conditional, False for marginal mixings
"""
return False
11 changes: 10 additions & 1 deletion docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ you can find examples in ```docs/examples```. Specifically:
initialize_hypers, update_hypers, draw, compute_posterior_hypers,
update_summary_statistics```. Please refer to the ```NNIG_Hierarchy_NGG.py``` examples for details.

For an example of how to run please refer to ```test_run.py```, ```estimate_pyhier_desnity.ipynb```.
## To implement a mixing in Python
Create a ```.py``` file in ```pybmix/docs/examples``` implementing all the necessary methods of the mixing,
you can find an example in ```docs/examples```. Specifically:
- to implement a non-conditional mixing you need to define the methods:
```is_conditional, update_state, initialize_state, mass_existing_cluster, mass_new_cluster```.
Please refer to the ```DP_mixing.py``` example for details.
- to implement a conditional mixing you need to define the methods:
```is_conditional, update_state, initialize_state, mixing_weights```.

For working examples please refer to ```test_run.py``` and ```estimate_py_density.ipynb```.
28 changes: 28 additions & 0 deletions docs/examples/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
To implement a hierarchy in Python
----------------------------------

Create a ``.py`` file implementing all the necessary methods of the
hierarchy, you can find examples in ``docs/examples``. Specifically:

- to implement a non-conjugate hierarchy you need to define the
methods:
``is_conjugate, like_lpdf, initialize_state, initialize_hypers, update_hypers, draw, update_summary_statistics, sample_full_cond``.
Please refer to the ``LapNIG_Hierarchy.py`` example for details.
- to implement a conjugate hierarchy you need to define the methods:
``is_conjugate, like_lpdf, marg_lpdf, initialize_state, initialize_hypers, update_hypers, draw, compute_posterior_hypers, update_summary_statistics``.
Please refer to the ``NNIG_Hierarchy_NGG.py`` examples for details.

To implement a mixing in Python
-------------------------------

Create a ``.py`` file in ``pybmix/docs/examples`` implementing all the
necessary methods of the mixing, you can find an example in
``docs/examples``. Specifically: - to implement a non-conditional mixing
you need to define the methods:
``is_conditional, update_state, initialize_state, mass_existing_cluster, mass_new_cluster``.
Please refer to the ``DP_mixing.py`` example for details. - to implement
a conditional mixing you need to define the methods:
``is_conditional, update_state, initialize_state, mixing_weights``.

For working examples please refer to ``test_run.py`` and
``estimate_pyhier_desnity.ipynb``.
403 changes: 403 additions & 0 deletions docs/examples/compare_cpp_and_py.ipynb

Large diffs are not rendered by default.

Loading