|
| 1 | +# PyAutoFit: Classy Probabilistic Programming |
| 2 | + |
| 3 | +[](https://www.repostatus.org/#active) |
| 4 | +[](https://pypi.org/project/autofit/) |
| 5 | +[](https://pypi.org/project/autofit/) |
| 6 | +[](https://colab.research.google.com/github/PyAutoLabs/autofit_workspace/blob/2026.5.1.4/start_here.ipynb) |
| 7 | +[](https://github.com/rhayes777/PyAutoFit/actions) |
| 8 | +[](https://github.com/rhayes777/PyAutoBuild/actions) |
| 9 | +[](https://pyautofit.readthedocs.io/en/latest/?badge=latest) |
| 10 | +[](https://doi.org/10.21105/joss.02550) |
| 11 | + |
| 12 | +[Installation Guide](https://pyautofit.readthedocs.io/en/latest/installation/overview.html) | |
| 13 | +[readthedocs](https://pyautofit.readthedocs.io/en/latest/index.html) | |
| 14 | +[Introduction on Colab](https://colab.research.google.com/github/PyAutoLabs/autofit_workspace/blob/2026.5.1.4/notebooks/overview/overview_1_the_basics.ipynb) | |
| 15 | +[HowToFit](https://github.com/PyAutoLabs/HowToFit) |
| 16 | + |
| 17 | +**PyAutoFit** is a Python based probabilistic programming language for model fitting and Bayesian inference |
| 18 | +of large datasets. |
| 19 | + |
| 20 | +The basic **PyAutoFit** API allows us a user to quickly compose a probabilistic model and fit it to data via a |
| 21 | +log likelihood function, using a range of non-linear search algorithms (e.g. MCMC, nested sampling). |
| 22 | + |
| 23 | +Users can then set up **PyAutoFit** scientific workflow, which enables streamlined modeling of small |
| 24 | +datasets with tools to scale up to large datasets. |
| 25 | + |
| 26 | +**PyAutoFit** supports advanced statistical methods, most |
| 27 | +notably [a big data framework for Bayesian hierarchical analysis](https://pyautofit.readthedocs.io/en/latest/features/graphical.html). |
| 28 | + |
| 29 | +## Getting Started |
| 30 | + |
| 31 | +The following links are useful for new starters: |
| 32 | + |
| 33 | +- [The PyAutoFit readthedocs](https://pyautofit.readthedocs.io/en/latest), which includes an [installation guide](https://pyautofit.readthedocs.io/en/latest/installation/overview.html) and an overview of **PyAutoFit**'s core features. |
| 34 | +- [The introduction Jupyter Notebook on Colab](https://colab.research.google.com/github/PyAutoLabs/autofit_workspace/blob/2026.5.1.4/notebooks/overview/overview_1_the_basics.ipynb), where you can try **PyAutoFit** in a web browser (without installation). |
| 35 | +- [The autofit_workspace GitHub repository](https://github.com/Jammy2211/autofit_workspace), which includes example scripts demonstrating **PyAutoFit**'s features. |
| 36 | +- [The standalone HowToFit repository](https://github.com/PyAutoLabs/HowToFit), a series of Jupyter notebook lectures which give new users a step-by-step introduction to **PyAutoFit**. |
| 37 | + |
| 38 | +## Support |
| 39 | + |
| 40 | +Support for installation issues, help with Fit modeling and using **PyAutoFit** is available by |
| 41 | +[raising an issue on the GitHub issues page](https://github.com/rhayes777/PyAutoFit/issues). |
| 42 | + |
| 43 | +We also offer support on the **PyAutoFit** [Slack channel](https://pyautoFit.slack.com/), where we also provide the |
| 44 | +latest updates on **PyAutoFit**. Slack is invitation-only, so if you'd like to join send |
| 45 | +an [email](https://github.com/Jammy2211) requesting an invite. |
| 46 | + |
| 47 | +## HowToFit |
| 48 | + |
| 49 | +For users less familiar with Bayesian inference and scientific analysis you may wish to read through |
| 50 | +the **HowToFits** lectures. These teach you the basic principles of Bayesian inference, with the |
| 51 | +content pitched at undergraduate level and above. |
| 52 | + |
| 53 | +The lectures are available in the [standalone HowToFit repository](https://github.com/PyAutoLabs/HowToFit). |
| 54 | + |
| 55 | +## API Overview |
| 56 | + |
| 57 | +To illustrate the **PyAutoFit** API, we use an illustrative toy model of fitting a one-dimensional Gaussian to |
| 58 | +noisy 1D data. Here's the `data` (black) and the model (red) we'll fit: |
| 59 | + |
| 60 | +<img src="https://raw.githubusercontent.com/rhayes777/PyAutoFit/main/files/toy_model_fit.png" width="400" /> |
| 61 | + |
| 62 | +We define our model, a 1D Gaussian by writing a Python class using the format below: |
| 63 | + |
| 64 | +```python |
| 65 | +class Gaussian: |
| 66 | + |
| 67 | + def __init__( |
| 68 | + self, |
| 69 | + centre=0.0, # <- PyAutoFit recognises these |
| 70 | + normalization=0.1, # <- constructor arguments are |
| 71 | + sigma=0.01, # <- the Gaussian's parameters. |
| 72 | + ): |
| 73 | + self.centre = centre |
| 74 | + self.normalization = normalization |
| 75 | + self.sigma = sigma |
| 76 | + |
| 77 | + """ |
| 78 | + An instance of the Gaussian class will be available during model fitting. |
| 79 | +
|
| 80 | + This method will be used to fit the model to data and compute a likelihood. |
| 81 | + """ |
| 82 | + |
| 83 | + def model_data_from(self, xvalues): |
| 84 | + |
| 85 | + transformed_xvalues = xvalues - self.centre |
| 86 | + |
| 87 | + return (self.normalization / (self.sigma * (2.0 * np.pi) ** 0.5)) * \ |
| 88 | + np.exp(-0.5 * (transformed_xvalues / self.sigma) ** 2.0) |
| 89 | +``` |
| 90 | + |
| 91 | +**PyAutoFit** recognises that this Gaussian may be treated as a model component whose parameters can be fitted for via |
| 92 | +a non-linear search like [emcee](https://github.com/dfm/emcee). |
| 93 | + |
| 94 | +To fit this Gaussian to the `data` we create an Analysis object, which gives **PyAutoFit** the `data` and a |
| 95 | +`log_likelihood_function` describing how to fit the `data` with the model: |
| 96 | + |
| 97 | +```python |
| 98 | +class Analysis(af.Analysis): |
| 99 | + |
| 100 | + def __init__(self, data, noise_map): |
| 101 | + |
| 102 | + self.data = data |
| 103 | + self.noise_map = noise_map |
| 104 | + |
| 105 | + def log_likelihood_function(self, instance): |
| 106 | + |
| 107 | + """ |
| 108 | + The 'instance' that comes into this method is an instance of the Gaussian class |
| 109 | + above, with the parameters set to values chosen by the non-linear search. |
| 110 | + """ |
| 111 | + |
| 112 | + print("Gaussian Instance:") |
| 113 | + print("Centre = ", instance.centre) |
| 114 | + print("normalization = ", instance.normalization) |
| 115 | + print("Sigma = ", instance.sigma) |
| 116 | + |
| 117 | + """ |
| 118 | + We fit the ``data`` with the Gaussian instance, using its |
| 119 | + "model_data_from" function to create the model data. |
| 120 | + """ |
| 121 | + |
| 122 | + xvalues = np.arange(self.data.shape[0]) |
| 123 | + |
| 124 | + model_data = instance.model_data_from(xvalues=xvalues) |
| 125 | + residual_map = self.data - model_data |
| 126 | + chi_squared_map = (residual_map / self.noise_map) ** 2.0 |
| 127 | + log_likelihood = -0.5 * sum(chi_squared_map) |
| 128 | + |
| 129 | + return log_likelihood |
| 130 | +``` |
| 131 | + |
| 132 | +We can now fit our model to the `data` using a non-linear search: |
| 133 | + |
| 134 | +```python |
| 135 | +model = af.Model(Gaussian) |
| 136 | + |
| 137 | +analysis = Analysis(data=data, noise_map=noise_map) |
| 138 | + |
| 139 | +emcee = af.Emcee(nwalkers=50, nsteps=2000) |
| 140 | + |
| 141 | +result = emcee.fit(model=model, analysis=analysis) |
| 142 | +``` |
| 143 | + |
| 144 | +The `result` contains information on the model-fit, for example the parameter samples, maximum log likelihood |
| 145 | +model and marginalized probability density functions. |
0 commit comments