simulators_sample.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Simulators__\n",
"\n",
"These scripts simulates many 1D Gaussian datasets with a low signal to noise ratio, which are used to demonstrate\n",
"model-fitting."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"%matplotlib inline\n",
"from pyprojroot import here\n",
"workspace_path = str(here())\n",
"%cd $workspace_path\n",
"print(f\"Working Directory has been set to `{workspace_path}`\")\n",
"\n",
"import numpy as np\n",
"from os import path\n",
"\n",
"import autofit as af\n",
"import util"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Gaussian x1 low snr (centre fixed to 50.0)__\n",
"\n",
"This is used for demonstrating expectation propagation, whereby a shared `centre` parameter is inferred from a sample \n",
"of `total_datasets` 1D Gaussian datasets."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"total_datasets = 50\n",
"\n",
"for i in range(total_datasets):\n",
" dataset_path = path.join(\n",
" \"dataset\", \"example_1d\", f\"gaussian_x1__low_snr\", f\"dataset_{i}\"\n",
" )\n",
" gaussian = af.ex.Gaussian(centre=50.0, normalization=0.5, sigma=5.0)\n",
" util.simulate_dataset_1d_via_gaussian_from(\n",
" gaussian=gaussian, dataset_path=dataset_path\n",
" )"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Gaussian x1 low snr (centre drawn from parent Gaussian distribution to 50.0)__\n",
"\n",
"This is used for demonstrating expectation propagation and hierachical modeling, whereby a the `centre` parameters \n",
"of a sample of `total_datasets` 1D Gaussian datasets are drawn from a Gaussian distribution."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"\n",
"total_datasets = 10\n",
"\n",
"gaussian_parent_model = af.Model(\n",
" af.ex.Gaussian,\n",
" centre=af.GaussianPrior(mean=50.0, sigma=10.0, lower_limit=0.0, upper_limit=100.0),\n",
" normalization=0.5,\n",
" sigma=5.0,\n",
")\n",
"\n",
"for i in range(total_datasets):\n",
" dataset_path = path.join(\n",
" \"dataset\", \"example_1d\", f\"gaussian_x1__hierarchical\", f\"dataset_{i}\"\n",
" )\n",
"\n",
" gaussian = gaussian_parent_model.random_instance()\n",
"\n",
" util.simulate_dataset_1d_via_gaussian_from(\n",
" gaussian=gaussian, dataset_path=dataset_path\n",
" )\n"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Gaussian x2 offset centre__\n",
"\n",
"This is used for demonstrating the benefits of graphical models over fitting one-by-one, because it creates a \n",
"degeneracy in the offset of the centres of the two Gaussians."
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"total_datasets = 10\n",
"\n",
"for i in range(total_datasets):\n",
" dataset_path = path.join(\n",
" \"dataset\", \"example_1d\", f\"gaussian_x2__offset_centres\", f\"dataset_{i}\"\n",
" )\n",
"\n",
" sigma_0_prior = af.GaussianPrior(\n",
" lower_limit=0.0, upper_limit=20.0, mean=10.0, sigma=10.0\n",
" )\n",
" while True:\n",
" try:\n",
" sigma_0_value = sigma_0_prior.value_for(unit=np.random.random(1))\n",
" break\n",
" except af.exc.PriorLimitException:\n",
" continue\n",
"\n",
" sigma_1_prior = af.GaussianPrior(\n",
" lower_limit=0.0, upper_limit=20.0, mean=10.0, sigma=10.0\n",
" )\n",
" while True:\n",
" try:\n",
" sigma_1_value = sigma_1_prior.value_for(unit=np.random.random(1))\n",
" break\n",
" except af.exc.PriorLimitException:\n",
" continue\n",
"\n",
" gaussian_0 = af.ex.Gaussian(centre=40.0, normalization=1.0, sigma=sigma_0_value)\n",
" gaussian_1 = af.ex.Gaussian(centre=60.0, normalization=1.0, sigma=sigma_1_value)\n",
"\n",
" util.simulate_dataset_1d_via_profile_1d_list_from(\n",
" profile_1d_list=[gaussian_0, gaussian_1], dataset_path=dataset_path\n",
" )\n"
],
"outputs": [],
"execution_count": null
}
],
"metadata": {
"anaconda-cloud": {},
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}