Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-matthis committed Nov 29, 2017
0 parents commit 7b4ab5e
Show file tree
Hide file tree
Showing 87 changed files with 7,431 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .gitignore
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
38 changes: 38 additions & 0 deletions .travis.yml
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
28 changes: 28 additions & 0 deletions LICENSE.txt
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.
16 changes: 16 additions & 0 deletions README.md
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)
1 change: 1 addition & 0 deletions delfi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from delfi.version import __version__, VERSION
79 changes: 79 additions & 0 deletions delfi/distribution/BaseDistribution.py
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)
43 changes: 43 additions & 0 deletions delfi/distribution/Discrete.py
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)
52 changes: 52 additions & 0 deletions delfi/distribution/Gamma.py
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
Loading

0 comments on commit 7b4ab5e

Please sign in to comment.