-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git@github.com:arm61/uravu.git
- Loading branch information
Showing
20 changed files
with
1,069 additions
and
98 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 |
---|---|---|
|
@@ -5,4 +5,7 @@ build | |
dist | ||
htmlcov | ||
uravu.egg-info | ||
**/.DS_Store | ||
**/.DS_Store | ||
**/Untitled.ipynb | ||
coverage_html_report | ||
paper |
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,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 |
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
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,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" |
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
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
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,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 | ||
} |
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -8,4 +8,6 @@ pint | |
uncertainties | ||
nbsphinx | ||
jupyter-sphinx | ||
sphinx_rtd_theme | ||
sphinx_rtd_theme | ||
coverage | ||
coveralls |
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
Oops, something went wrong.