This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7b4ab5e
Showing
87 changed files
with
7,431 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Development files and python cache | ||
/*.pyc | ||
/*.egg | ||
/*.egg-info | ||
|
||
# Notebook checkpoints | ||
.ipynb_checkpoints | ||
|
||
# System | ||
.DS_Store | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*,cover | ||
.hypothesis/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
#Ipython Notebook | ||
.ipynb_checkpoints | ||
|
||
# Latex-log files: | ||
*.aux | ||
*.log | ||
*.out | ||
*.gz | ||
*.toc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
sudo: required | ||
dist: trusty | ||
language: python | ||
matrix: | ||
include: | ||
- python: 3.5 | ||
notifications: | ||
email: false | ||
addons: | ||
apt_packages: | ||
- pandoc | ||
before_install: | ||
- "export DISPLAY=:99.0" | ||
- "sh -e /etc/init.d/xvfb start" | ||
install: | ||
- sudo apt-get install libopenblas-dev | ||
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; | ||
- bash miniconda.sh -b -p $HOME/miniconda | ||
- export PATH="$HOME/miniconda/bin:$PATH" | ||
- hash -r | ||
- conda config --set always_yes yes --set changeps1 no | ||
- conda update -q conda | ||
- conda info -a | ||
- conda create -q -n testenv python=$TRAVIS_PYTHON_VERSION ipython numpy scipy pytest matplotlib mkl mkl-service sphinx | ||
- source activate testenv | ||
- pip install travis-sphinx nbsphinx | ||
- export PATH=$HOME/.local/bin:$PATH | ||
- pip install pep8 pytest-pep8 python-coveralls pytest-cov | ||
- pip install sphinxcontrib-napoleon sphinx_rtd_theme | ||
- pip install theano | ||
- echo -e "\n[blas]\nldflags = -lopenblas\n" >> ~/.theanorc | ||
- python setup.py install | ||
script: | ||
- PYTHONPATH=$PWD:$PYTHONPATH pytest --cov=delfi; | ||
- travis-sphinx build -n -s docs/ | ||
after_success: | ||
- coveralls | ||
- travis-sphinx deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Modified work Copyright (c) 2017, Jan-Matthis Lueckmann, Pedro J. Goncalves, Jakob H. Macke | ||
Original work Copyright (c) 2016, George Papamakarios | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
The views and conclusions contained in the software and documentation are those | ||
of the authors and should not be interpreted as representing official policies, | ||
either expressed or implied, of anybody else. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# delfi | ||
|
||
delfi is a Python package for density estimation likelihood-free inference. | ||
|
||
**Important: The code in this repository is still experimental, and APIs are subject to change without warning.** | ||
|
||
|
||
## Documentation | ||
|
||
For installation instructions and getting started, an early-stage documentation is available | ||
at [http://www.mackelab.org/delfi/](http://www.mackelab.org/delfi/) | ||
|
||
|
||
## Status | ||
|
||
[![Build Status](https://travis-ci.org/mackelab/delfi.svg?branch=master)](https://travis-ci.org/mackelab/delfi) [![Docs](https://img.shields.io/badge/docs-latest-brightgreen.svg?style=flat)](http://www.mackelab.org/delfi/) [![PyPI version](https://badge.fury.io/py/delfi.svg)](https://badge.fury.io/py/delfi) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from delfi.version import __version__, VERSION |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import abc | ||
import numpy as np | ||
|
||
from delfi.utils.meta import ABCMetaDoc | ||
|
||
|
||
class BaseDistribution(metaclass=ABCMetaDoc): | ||
"""Abstract base class for distributions | ||
Distributions must at least implement abstract properties and methods of | ||
this class. | ||
Parameters | ||
---------- | ||
ndim : int | ||
Number of ndimensions of the distribution | ||
seed : int or None | ||
If provided, random number generator will be seeded | ||
""" | ||
def __init__(self, ndim, seed=None): | ||
self.ndim = ndim | ||
|
||
self.seed = seed | ||
if seed is not None: | ||
self.rng = np.random.RandomState(seed=seed) | ||
else: | ||
self.rng = np.random.RandomState() | ||
|
||
@abc.abstractproperty | ||
def mean(self): | ||
"""Means""" | ||
pass | ||
|
||
@abc.abstractproperty | ||
def std(self): | ||
"""Standard deviations of marginals""" | ||
pass | ||
|
||
@abc.abstractmethod | ||
def eval(self, x, ii=None, log=True): | ||
"""Method to evaluate pdf | ||
Parameters | ||
---------- | ||
x : int or list or np.array | ||
Rows are inputs to evaluate at | ||
ii : list | ||
A list of indices specifying which marginal to evaluate. | ||
If None, the joint pdf is evaluated | ||
log : bool, defaulting to True | ||
If True, the log pdf is evaluated | ||
Returns | ||
------- | ||
scalar | ||
""" | ||
pass | ||
|
||
@abc.abstractmethod | ||
def gen(self, n_samples=1): | ||
"""Method to generate samples | ||
Parameters | ||
---------- | ||
n_samples : int | ||
Number of samples to generate | ||
Returns | ||
------- | ||
n_samples x self.ndim | ||
""" | ||
pass | ||
|
||
def gen_newseed(self): | ||
"""Generates a new random seed""" | ||
if self.seed is None: | ||
return None | ||
else: | ||
return self.rng.randint(0, 2**31) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import numpy as np | ||
|
||
from delfi.distribution.BaseDistribution import BaseDistribution | ||
|
||
|
||
class Discrete(BaseDistribution): | ||
def __init__(self, p, seed=None): | ||
"""Discrete distribution | ||
Parameters | ||
---------- | ||
p : list or np.array, 1d | ||
Probabilities of elements, must sum to 1 | ||
seed : int or None | ||
If provided, random number generator will be seeded | ||
""" | ||
super().__init__(ndim=1, seed=seed) | ||
|
||
p = np.asarray(p) | ||
assert p.ndim == 1, 'p must be a 1-d array' | ||
assert np.isclose(np.sum(p), 1), 'p must sum to 1' | ||
self.p = p | ||
|
||
@property | ||
def mean(self): | ||
"""Means""" | ||
pass | ||
|
||
@property | ||
def std(self): | ||
"""Standard deviations of marginals""" | ||
pass | ||
|
||
@copy_ancestor_docstring | ||
def eval(self, x, ii=None, log=True): | ||
raise NotImplementedError("To be implemented") | ||
|
||
@copy_ancestor_docstring | ||
def gen(self, n_samples=1, seed=None): | ||
# See BaseDistribution.py for docstring | ||
c = np.cumsum(self.p[:-1])[np.newaxis, :] # cdf | ||
r = self.rng.rand(n_samples, 1) | ||
return np.sum((r > c).astype(int), axis=1).reshape(-1, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import numpy as np | ||
from scipy.stats import gamma | ||
from delfi.distribution.BaseDistribution import BaseDistribution | ||
|
||
|
||
class Gamma(BaseDistribution): | ||
def __init__(self, alpha=1., beta=1., seed=None): | ||
"""Univariate (!) Gamma distribution | ||
Parameters | ||
---------- | ||
alpha : list, or np.array, 1d | ||
Shape parameters | ||
beta : list, or np.array, 1d | ||
inverse scale paramters | ||
seed : int or None | ||
If provided, random number generator will be seeded | ||
""" | ||
super().__init__(ndim=1, seed=seed) | ||
|
||
alpha, beta = np.atleast_1d(alpha), np.atleast_1d(beta) | ||
assert alpha.ndim == 1, 'alpha must be a 1-d array' | ||
assert alpha.size == beta.size, 'alpha and beta must match in size' | ||
assert np.all(alpha > 0.), 'Should be greater than zero.' | ||
assert np.all(beta > 0.), 'Should be greater than zero.' | ||
self.alpha = alpha | ||
self.beta = beta | ||
self._gamma = gamma(a=alpha, scale=1./beta) | ||
|
||
@property | ||
def mean(self): | ||
"""Means""" | ||
return self.alpha / self.beta | ||
|
||
@property | ||
def std(self): | ||
"""Standard deviations of marginals""" | ||
return np.sqrt( self.alpha ) / self.beta | ||
|
||
@copy_ancestor_docstring | ||
def eval(self, x, ii=None, log=True): | ||
# univariate distribution only, i.e. ii=[0] in any case | ||
return self._gamma.logpdf(x) if log else self._gamma.pdf(x) | ||
|
||
@copy_ancestor_docstring | ||
def gen(self, n_samples=1, seed=None): | ||
# See BaseDistribution.py for docstring | ||
|
||
x = self.rng.gamma(shape=self.alpha, | ||
scale=1./self.beta, | ||
size=(n_samples, self.ndim)) | ||
return x |
Oops, something went wrong.