Skip to content

Commit

Permalink
Merge branch 'nestedsample_it_upgrade'
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeaw committed Mar 15, 2020
2 parents 923fedf + 8184ece commit c2a5cb4
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 49 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

![Python version badge](https://img.shields.io/badge/python-3.6-blue.svg)
[![license](https://img.shields.io/github/license/LoLab-VU/Gleipnir.svg)](LICENSE)
![version](https://img.shields.io/badge/version-0.25.0-orange.svg)
[![release](https://img.shields.io/github/release-pre/LoLab-VU/Gleipnir.svg)](https://github.com/LoLab-VU/Gleipnir/releases/tag/v0.25.0)
![version](https://img.shields.io/badge/version-0.26.0-orange.svg)
[![release](https://img.shields.io/github/release-pre/LoLab-VU/Gleipnir.svg)](https://github.com/LoLab-VU/Gleipnir/releases/tag/v0.26.0)
[![anaconda cloud](https://anaconda.org/blakeaw/gleipnir/badges/version.svg)](https://anaconda.org/blakeaw/gleipnir)
[![DOI](https://zenodo.org/badge/173688080.svg)](https://zenodo.org/badge/latestdoi/173688080)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/e117a46ae8b241539742ab00f8cd1b38)](https://www.codacy.com/app/blakeaw1102/Gleipnir?utm_source=github.com&utm_medium=referral&utm_content=LoLab-VU/Gleipnir&utm_campaign=Badge_Grade)
Expand Down Expand Up @@ -83,7 +83,7 @@ pip install gleipnir-ns
```
or it can be directly sourced from the GitHub repo:
```
pip install -e git+https://github.com/LoLab-VU/Gleipnir@v0.25.0#egg=gleipnir
pip install -e git+https://github.com/LoLab-VU/Gleipnir@v0.26.0#egg=gleipnir
```
However, sourcing from the GitHub repo will not automatically install the core dependencies. You would have to do that separately:
```
Expand Down Expand Up @@ -266,24 +266,25 @@ observable_data['my_observable'] = (data, data_sd, time_idxs)
sample_it = NestedSampleIt(my_model, observable_data, timespan)
# Now build the NestedSampling object. -- All inputs are
# optional keyword arguments.
nested_sampler = sample_it(ns_version='gleipnir-classic',
nested_sampler = sample_it(ns_version='built-in',
ns_population_size=100,
ns_kwargs=dict(),
log_likelihood_type='logpdf')
log_likelihood_type='snlpdf')
# Then you can run the nested sampler.
log_evidence, log_evidence_error = nested_sampler.run()
```

NestedSampleIt constructs the NestedSampling object to sample all of a model's kinetic rate parameters. It assumes that the priors are uniform with size 4 orders of magnitude and centered on the values defined in the model.

In addition, NestedSampleIt crrently has three pre-defined loglikelihood functions with different estimators. They can be specified with the keyword parameter log_likelihood_type:
In addition, NestedSampleIt currently has three pre-defined loglikelihood functions with different estimators. They can be specified with the keyword parameter log_likelihood_type:
```python
# Now build the NestedSampling object.
nested_sampler = sample_it(log_likelihood_type='logpdf')
nested_sampler = sample_it(log_likelihood_type='snlpdf')
```
The options are
* 'logpdf'=>Compute the loglikelihood using the
normal distribution estimator
* 'snlpdf'=>Compute the loglikelihood using the
sum of logpdfs for normal distribution estimators centered
on data points with error sigma.
* 'mse'=>Compute the loglikelihood using the
negative mean squared error estimator
* 'sse'=>Compute the loglikelihood using
Expand All @@ -293,20 +294,20 @@ Each of these functions computes the loglikelihood estimate using the timecourse
If you want to use a different or more complicated likelihood function with NestedSampleIt then you'll need to subclass it and override one of the existing loglikelihood functions.

#### NestIt
The nestedsample_it module has a built-in helper class, NestIt, which can be used in conjunction of with NestedSampleIt class. NestIt can be used at the level of PySB model definition to log which parameters to include in
a Nested Sampling run. It can be imported from the pysb_utilities module:
The nestedsample_it module has a built-in helper class, NestIt, which can be used either in conjunction with NestedSampleIt class or on its own to log parameters for sampling. NestIt can be used at the level of PySB model definition to log which parameters to include in
a Nested Sampling run. It can also be used outside of the model definition. It can be imported from the pysb_utilities module:
```python
from gleipnir.pysb_utilities import NestIt
```
It is passed at instantiation to the NestedSampleIt class, which uses it
If passed at instantiation to the NestedSampleIt class, the NestedSampleIt object will use it
to build the sampled parameters list and parameter mask for the likelihood
function.
See the following example files:

* [dimerization_model_nestit](./examples/pysb_dimerization_model/dimerization_model_nestit.py) - example model definition using NestIt to flag parameters.
* [run_NS_NestedSampleIt_NestIt_dimerization_model](./examples/pysb_dimerization_model/run_NS_NestedSampleIt_NestIt_dimerization_model.py) - example use of NestIt with NestedSampleIt.

Note that if you flag a parameter for sampling without setting a prior, NestIt will by default assign the parameter a uniform prior centered on the parameter's value with a width of 4 orders of magnitude.
Note that if you flag a parameter for sampling without setting a prior, NestIt will by default assign the parameter a uniform prior centered on the parameter's value with a width of 4 orders of magnitude. You can alter this behavior by calling the `default_to_norm_prior` function before adding parameters to the NestIt instance which will set the default priors to a norm distribution centered on the nominal parameter value with a sigma of 2 orders of magnitude.

#### Builder class from pysb.builder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@
selector.append_to_models(obs_line)

# Now let's construct the Nested Samplers for the models.
# ns_version='gleipnir-classic' will use Gleipnir's built-in implementation
# ns_version='built-in' will use Gleipnir's built-in implementation
# of the classic Nested Sampling algorithm.
# ns_population_size=100 will set the active population size for the Nested
# Sampling runs to 100.
# log_likelihood_type='mse' will use the minus of the Mean Squared Error (mse)
# as the log_likelihood estimator.
selector.gen_nested_samplers(tspan, observable_data, solver=solver,
ns_version='gleipnir-classic',
ns_version='built-in',
ns_population_size=100,
log_likelihood_type='mse')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
sample_it = NestedSampleIt(builder.model, observable_data, tspan, builder=builder)
# Now build the NestedSampling object. -- All inputs are
# optional keyword arguments.
nested_sampler = sample_it(ns_version='gleipnir-classic',
nested_sampler = sample_it(ns_version='built-in',
ns_population_size=population_size,
ns_kwargs=dict(),
log_likelihood_type='logpdf')
log_likelihood_type='snlpdf')
print(len(nested_sampler.sampled_parameters))
# Then you can run the nested sampler.
log_evidence, log_evidence_error = nested_sampler.run(verbose=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
sample_it = NestedSampleIt(model, observable_data, tspan, nest_it=nest_it)
# Now build the NestedSampling object. -- All inputs are
# optional keyword arguments.
nested_sampler = sample_it(ns_version='gleipnir-classic',
nested_sampler = sample_it(ns_version='built-in',
ns_population_size=population_size,
ns_kwargs=dict(),
log_likelihood_type='logpdf')
log_likelihood_type='snlpdf')
print(len(nested_sampler.sampled_parameters))
# Then you can run the nested sampler.
log_evidence, log_evidence_error = nested_sampler.run(verbose=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
sample_it = NestedSampleIt(model, observable_data, tspan)
# Now build the NestedSampling object. -- All inputs are
# optional keyword arguments.
nested_sampler = sample_it(ns_version='gleipnir-classic',
nested_sampler = sample_it(ns_version='built-in',
ns_population_size=population_size,
ns_kwargs=dict(),
log_likelihood_type='logpdf')
log_likelihood_type='snlpdf')
# Then you can run the nested sampler.
log_evidence, log_evidence_error = nested_sampler.run(verbose=True)
# Retrieve the evidence
Expand Down
12 changes: 6 additions & 6 deletions gleipnir/pysb_utilities/hyp_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def append_to_models(self, line):

def gen_nested_samplers(self, timespan, observable_data,
solver=pysb.simulator.ScipyOdeSimulator,
solver_kwargs=None, ns_version='gleipnir-classic',
solver_kwargs=None, ns_version='built-in',
ns_population_size=1000, ns_kwargs=None,
log_likelihood_type='logpdf'):
log_likelihood_type='snlpdf'):
"""Generate the Nested Sampling objects for each model.
The Nested Sampling object instances are stored in a list as the
Expand All @@ -154,23 +154,23 @@ def gen_nested_samplers(self, timespan, observable_data,
solver_kwargs (dict): Dictionary of optional keyword arguments to
pass to the solver when it is initialized. Defaults to dict().
ns_version (str): Defines which version of Nested Sampling to use.
Options are 'gleipnir-classic'=>Gleipnir's built-in implementation
Options are 'built-in'=>Gleipnir's built-in implementation
of the classic Nested Sampling algorithm, 'multinest'=>Use the
MultiNest code via Gleipnir, 'polychord'=>Use the PolyChord code
via Gleipnir, or 'dnest4'=>Use the DNest4 program via Gleipnir.
Defaults to 'gleipnir-classic'.
Defaults to 'built-in'.
ns_population_size (int): Set the size of the active population
of sample points to use during Nested Sampling runs.
Defaults to 1000.
ns_kwargs (dict): Dictionary of any additional optional keyword
arguments to pass to NestedSampling object constructor.
Defaults to dict().
log_likelihood_type (str): Define the type of loglikelihood estimator
to use. Options are 'logpdf'=>Compute the loglikelihood using
to use. Options are 'snlpdf'=>Compute the loglikelihood using
the normal distribution estimator, 'mse'=>Compute the
loglikelihood using the negative mean squared error estimator,
'sse'=>Compute the loglikelihood using the negative sum of
squared errors estimator. Defaults to 'logpdf'.
squared errors estimator. Defaults to 'snlpdf'.
Returns:
None
Expand Down
Loading

0 comments on commit c2a5cb4

Please sign in to comment.