Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:arm61/uravu.git
Browse files Browse the repository at this point in the history
  • Loading branch information
arm61 committed Feb 24, 2020
2 parents 512e30f + b6dc0e7 commit f05ae32
Show file tree
Hide file tree
Showing 20 changed files with 1,069 additions and 98 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ build
dist
htmlcov
uravu.egg-info
**/.DS_Store
**/.DS_Store
**/Untitled.ipynb
coverage_html_report
paper
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: python
python:
- '3.6'
- 3.6-dev
- '3.7'
- 3.7-dev
- '3.8'
- 3.8-dev
install:
- pip install -r requirements.txt
script:
- coverage run --source=uravu setup.py test
after_success:
- coveralls
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
# uravu

உறவு
**making Bayesian modelling easy(er)**


[![Documentation Status](https://readthedocs.org/projects/uravu/badge/?version=latest)](https://uravu.readthedocs.io/en/latest/?badge=latest)
[![Coverage Status](https://coveralls.io/repos/github/arm61/uravu/badge.svg?branch=master)](https://coveralls.io/github/arm61/uravu?branch=master)
[![Build Status](https://travis-ci.org/arm61/uravu.svg?branch=master)](https://travis-ci.org/arm61/uravu)
[![Build status](https://ci.appveyor.com/api/projects/status/eo426m99lmkbh5rx?svg=true)](https://ci.appveyor.com/project/arm61/uravu)
[![Gitter](https://badges.gitter.im/uravu/community.svg)](https://gitter.im/uravu/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

u-rav-u (from the Tamil for relationship) is a library for fitting, assessing, and optimising the relationship between a model and some data, using Bayesian methods.
``uravu`` (from the Tamil for relationship) is about the relationship between some data and a function that may be used to describe the data.

The world of Bayesian modelling can be a confusing place, but uravu makes it easier.
All you need is a function to describe the analytical relationship and some data that you want to model.
Other fun things like prior probabilities for your model parameters can be included.
The aim of ``uravu`` is to make using the **amazing** Bayesian inference libraries that are available in Python as easy as [scipy.optimize.curve_fit](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html).
Therefore enabling many more to make use of these exciting tools and powerful libraries.
Plus, we have some nice plotting functionalities available in the `plotting` module, capable of generating publication quality figures.

From just a single function and your data, uravu can help you perform:
![An example of the type of figures that uravu can produce. Showing straight line distribution with increasing uncertainty.](./docs/source/sample_fig.png)

- Maximum likelihood fitting
- MCMC modelling for posterior estimation (using [`emcee`](https://emcee.readthedocs.io/))
- Evidence evaluation from nested sampling (using [`dynesty`](https://dynesty.readthedocs.io/))
- A plotting library to present the results (using [`matplotlib`](https://matplotlib.org/) and [`corner`](https://corner.readthedocs.io/))
In an effort to make the ``uravu`` API friendly to those new to Bayesian inference, ``uravu`` is *opinionated*, making assumptions about priors amoung other things.
However, we have endevoured to make it straightforward to ignore these opinions.

With this library, we hope to lower the barrier to entry to Bayesian modelling methods, while improving understanding and appreciation for the power of these methods.
In addition to the library and API, we also have some [basic tutorials](https://uravu.readthedocs.io/en/latest/tutorials.html) discussing how Bayesian inference methods can be used in the analysis of data.

## Bayesian inference in Python

There are a couple of fantastic Bayesian inference libraries available in Python that `uravu` makes use of:

- [emcee](https://emcee.readthedocs.io/): enables the use of the [Goodman & Weare’s Affine Invariant Markov chain Monte Carlo (MCMC) Ensemble sampler](https://doi.org/10.2140/camcos.2010.5.65) to evaluate the structure of the model parameter posterior distributions,
- [dynesty](https://dynesty.readthedocs.io/): implements the [nested sampling](https://doi.org/10.1063/1.1835238) algorithm for evidence estimation.

## Problems

Expand Down
33 changes: 33 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
environment:
matrix:

# For Python versions available on Appveyor, see
# http://www.appveyor.com/docs/installed-software#python
# The list here is complete (excluding Python 2.6, which
# isn't covered by this document) at the time of writing.

- PYTHON: "C:\\Python36-x64"
MINICONDA: "C:\\Miniconda"
- PYTHON: "C:\\Python37-x64"
MINICONDA: "C:\\Miniconda"
- PYTHON: "C:\\Python38-x64"
MINICONDA: "C:\\Miniconda"

install:
# We need wheel installed to build wheels
- "SET PATH=%MINICONDA%;%MINICONDA%\\Scripts;%PATH%"
- "%PYTHON%\\python.exe -m pip install -U twine wheel setuptools"
- "%PYTHON%\\python.exe -m pip install -r requirements.txt"
- "set PATH=%PATH%;C:\"%PYTHON%\\Scripts"

build: off

test_script:
# Put your test command here.
# If you don't need to build C extensions on 64-bit Python 3.3 or 3.4,
# you can remove "build.cmd" from the front of the command, as it's
# only needed to support those cases.
# Note that you must use the environment variable %PYTHON% to refer to
# the interpreter you're using - Appveyor does not do anything special
# to put the Python version you want to use on PATH.
- "%PYTHON%\\python.exe setup.py test"
7 changes: 3 additions & 4 deletions docs/source/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plotting.plot_relationship(modeller)\n",
"ax.set_xlim((-0.5, 9.5))\n",
"ax.set_ylim((0, 22))\n",
"fig, ax = plt.subplots(figsize=(10, 6))\n",
"ax = plotting.plot_relationship(modeller, axes=ax)\n",
"plt.show()"
]
},
Expand All @@ -177,7 +176,7 @@
"metadata": {},
"outputs": [],
"source": [
"#print(modeller.citations)"
"print(modeller.citations)"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

The aim of ``uravu`` is to make using the **amazing** Bayesian inference libraries that are available in Python as easy as `scipy.optimize.curve_fit`_.
Therefore enabling many more to make use of these exciting tools and powerful libraries.
Plus, we have some nice plotting functionalities available in the :mod:`plotting` module, capable of generating publication quality figures.
Plus, we have some nice plotting functionalities available in the :mod:`plotting` module, capable of generating publication quality figures.

.. image:: sample_fig.png
:alt: An example of the type of figures that uravu can produce. Showing straight line distribution with increasing uncertainty.

In an effort to make the ``uravu`` API friendly to those new to Bayesian inference, ``uravu`` is *opinionated*, making assumptions about priors amoung other things.
However, we have endevoured to make it straightforward to ignore these opinions.
Expand All @@ -25,7 +28,7 @@ Bayesian inference in Python
There are a couple of fantastic Bayesian inference libraries available in Python that `uravu` makes use of:

- `emcee`_: enables the use of the `Goodman & Weare’s Affine Invariant Markov chain Monte Carlo (MCMC) Ensemble sampler`_ to evaluate the structure of the model parameter posterior distributions,
- `dynesty`_: implements `nested sampling`_ and `dynamic nested sampling`_ algorithms for evidence estimation.
- `dynesty`_: implements the `nested sampling`_ algorithm for evidence estimation.

To see which publications you should reference in your work, the :class:`Relationship` has a property that will return the relevant citations based on what analyses have been performed.

Expand All @@ -46,7 +49,6 @@ To see which publications you should reference in your work, the :class:`Relatio
.. _dynesty: https://dynesty.readthedocs.io/
.. _Goodman & Weare’s Affine Invariant Markov chain Monte Carlo (MCMC) Ensemble sampler: https://doi.org/10.2140/camcos.2010.5.65
.. _nested sampling: https://doi.org/10.1063/1.1835238
.. _dynamic nested sampling: https://doi.org/10.1007/s11222-018-9844-0

.. toctree::
:hidden:
Expand Down
219 changes: 219 additions & 0 deletions docs/source/max_likelihood.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Maximum likelihood\n",
"\n",
"In Bayesian modelling, the **likelihood**, $L$, is the name given to the measure of the goodness of fit between the model, with some given parameters, and the data. \n",
"When the maximum likelihood, $\\hat{L}$, is found, the most likely statistical model has been found for the given data. \n",
"\n",
"In this tutorial we will see how `uravu` can be used to maximize the likelihood of a model for some dataset.\n",
"\n",
"In `uravu`, the likelihood is calculated as follows, \n",
"\n",
"$$ \\ln L = -0.5 \\sum_{i=1}^n \\bigg[ \\frac{(y_i - m_i) ^2}{s_i^2} + \\ln(2 \\pi s_i^2) \\bigg], $$\n",
"\n",
"where, $y$ is the data ordinate, $m$ is the model ordinate, and $s$ is, \n",
"\n",
"$$ s_i^2 = \\delta y_i^2 + f^2 m_i^2 $$,\n",
"\n",
"where $\\delta y$ is the uncertainty in $y$ and $f$ is the fraction of unaccounted for uncertainty (more about this can be found in the [Unaccounted uncertainty](./unaccounted_uncertainty.html) tutorial.)\n",
"`uravu` is able to maximize this function with the help of the [`scipy.optimize.minimize()`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) function (we minimize the negative of the likelihood).\n",
"\n",
"Before we maximise the likelihood, is it necessary to create some *synthetic* data to analyse. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.linspace(0, 10, 20)\n",
"y = np.exp(0.5 * x) * 4\n",
"y += y * np.random.randn(20) * 0.1\n",
"dy = y * 0.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.errorbar(x, y, dy, marker='o', ls='')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data plotted above may be modelled with the following relationship, \n",
"\n",
"$$ y = a\\exp(bx), $$\n",
"\n",
"where $a$ and $b$ are the variables of interest in the modelling process. \n",
"We want to find the values of these variables, which maximises the likelihood. \n",
"\n",
"First, we must write a function do describe the model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def my_model(x, a, b):\n",
" \"\"\"\n",
" A function to describe the model under investgation.\n",
" \n",
" Args:\n",
" x (array_like): Abscissa data.\n",
" a (float): The pre-exponential factor.\n",
" b (float): The x-multiplicative factor.\n",
" \n",
" Returns\n",
" y (array_like): Ordinate data.\n",
" \"\"\"\n",
" return a * np.exp(b * x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With our model defined, we can construct a `Relationship` object. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from uravu.relationship import Relationship"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"modeller = Relationship(my_model, x, y, dy)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(modeller)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `Relationship` object gives us access to a few exciting Bayesian modelling methods.\n",
"However, this tutorial is focused on maximising the likelihood, this is achieved with the `max_likelihood()` class method. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"modeller.max_likelihood()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(modeller.variables)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see that the variables have changed to be close to the values into in the data synthesis. \n",
"\n",
"Let's inspect the model visually. \n",
"This can be achieved easily with the `plotting` module in `uravu`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from uravu import plotting"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax = plotting.plot_relationship(modeller)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Above, we can see that the orange line of maximum likelihood agrees well with the data. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
2 changes: 0 additions & 2 deletions docs/source/max_likelihood.rst

This file was deleted.

Binary file added docs/source/sample_fig.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ pint
uncertainties
nbsphinx
jupyter-sphinx
sphinx_rtd_theme
sphinx_rtd_theme
coverage
coveralls
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Chemistry',
'Topic :: Scientific/Engineering :: Physics']
Expand Down
Loading

0 comments on commit f05ae32

Please sign in to comment.