Package: autofit
Installation Guide|readthedocs|Introduction on Binder|HowToFitPyAutoFitis a Python based probabilistic programming language for model fitting and Bayesian inference
of large datasets.The basicPyAutoFitAPI allows us a user to quickly compose a probabilistic model and fit it to data via a
log likelihood function, using a range of non-linear search algorithms (e.g. MCMC, nested sampling).Users can then set upPyAutoFitscientific workflow, which enables streamlined modeling of small
datasets with tools to scale up to large datasets.PyAutoFitsupports advanced statistical methods, most
notablya big data framework for Bayesian hierarchical analysis.Getting StartedThe following links are useful for new starters:The PyAutoFit readthedocs, which includes aninstallation guideand an overview ofPyAutoFit’s core features.The introduction Jupyter Notebook on Binder, where you can tryPyAutoFitin a web browser (without installation).The autofit_workspace GitHub repository, which includes example scripts and theHowToFit Jupyter notebook lectureswhich give new users a step-by-step introduction toPyAutoFit.SupportSupport for installation issues, help with Fit modeling and usingPyAutoFitis available byraising an issue on the GitHub issues page.We also offer support on thePyAutoFitSlack channel, where we also provide the
latest updates onPyAutoFit. Slack is invitation-only, so if you’d like to join send
anemailrequesting an invite.HowToFitFor users less familiar with Bayesian inference and scientific analysis you may wish to read through
theHowToFitslectures. These teach you the basic principles of Bayesian inference, with the
content pitched at undergraduate level and above.A complete overview of the lecturesis provided on the HowToFit readthedocs pageAPI OverviewTo illustrate thePyAutoFitAPI, we use an illustrative toy model of fitting a one-dimensional Gaussian to
noisy 1D data. Here’s thedata(black) and the model (red) we’ll fit:We define our model, a 1D Gaussian by writing a Python class using the format below:classGaussian:def__init__(self,centre=0.0,# <- PyAutoFit recognises thesenormalization=0.1,# <- constructor arguments aresigma=0.01,# <- the Gaussian's parameters.):self.centre=centreself.normalization=normalizationself.sigma=sigma"""
    An instance of the Gaussian class will be available during model fitting.

    This method will be used to fit the model to data and compute a likelihood.
    """defmodel_data_1d_via_xvalues_from(self,xvalues):transformed_xvalues=xvalues-self.centrereturn(self.normalization/(self.sigma*(2.0*np.pi)**0.5))*\np.exp(-0.5*(transformed_xvalues/self.sigma)**2.0)PyAutoFitrecognises that this Gaussian may be treated as a model component whose parameters can be fitted for via
a non-linear search likeemcee.To fit this Gaussian to thedatawe create an Analysis object, which givesPyAutoFitthedataand alog_likelihood_functiondescribing how to fit thedatawith the model:classAnalysis(af.Analysis):def__init__(self,data,noise_map):self.data=dataself.noise_map=noise_mapdeflog_likelihood_function(self,instance):"""
        The 'instance' that comes into this method is an instance of the Gaussian class
        above, with the parameters set to values chosen by the non-linear search.
        """print("Gaussian Instance:")print("Centre = ",instance.centre)print("normalization = ",instance.normalization)print("Sigma = ",instance.sigma)"""
        We fit the ``data`` with the Gaussian instance, using its
        "model_data_1d_via_xvalues_from" function to create the model data.
        """xvalues=np.arange(self.data.shape[0])model_data=instance.model_data_1d_via_xvalues_from(xvalues=xvalues)residual_map=self.data-model_datachi_squared_map=(residual_map/self.noise_map)**2.0log_likelihood=-0.5*sum(chi_squared_map)returnlog_likelihoodWe can now fit our model to thedatausing a non-linear search:model=af.Model(Gaussian)analysis=Analysis(data=data,noise_map=noise_map)emcee=af.Emcee(nwalkers=50,nsteps=2000)result=emcee.fit(model=model,analysis=analysis)Theresultcontains information on the model-fit, for example the parameter samples, maximum log likelihood
model and marginalized probability density functions.