{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Fitting a light curve\n\nThis example shows how to fit the parameters of a SALT2 model to photometric\nlight curve data.\n\nFirst, we'll load an example of some photometric data.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import sncosmo\n\ndata = sncosmo.load_example_data()\n\nprint(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "An important additional note: a table of photometric data has a\n``band`` column and a ``zpsys`` column that use strings to identify\nthe bandpass (e.g., ``'sdssg'``) and zeropoint system (``'ab'``) of\neach observation. If the bandpass and zeropoint systems in your data\nare *not* built-ins known to sncosmo, you must register the\ncorresponding `~sncosmo.Bandpass` or `~sncosmo.MagSystem` to the\nright string identifier using the registry.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# create a model\nmodel = sncosmo.Model(source='salt2')\n\n# run the fit\nresult, fitted_model = sncosmo.fit_lc(\n    data, model,\n    ['z', 't0', 'x0', 'x1', 'c'],  # parameters of model to vary\n    bounds={'z':(0.3, 0.7)})  # bounds on parameters (if any)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The first object returned is a dictionary-like object where the keys\ncan be accessed as attributes in addition to the typical dictionary\nlookup like ``result['ncall']``:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(\"Number of chi^2 function calls:\", result.ncall)\nprint(\"Number of degrees of freedom in fit:\", result.ndof)\nprint(\"chi^2 value at minimum:\", result.chisq)\nprint(\"model parameters:\", result.param_names)\nprint(\"best-fit values:\", result.parameters)\nprint(\"The result contains the following attributes:\\n\", result.keys())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The second object returned is a shallow copy of the input model with\nthe parameters set to the best fit values. The input model is\nunchanged.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "sncosmo.plot_lc(data, model=fitted_model, errors=result.errors)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Suppose we already know the redshift of the supernova we're trying to\nfit.  We want to set the model's redshift to the known value, and then\nmake sure not to vary `z` in the fit.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model.set(z=0.5)  # set the model's redshift.\nresult, fitted_model = sncosmo.fit_lc(data, model,\n                                      ['t0', 'x0', 'x1', 'c'])\nsncosmo.plot_lc(data, model=fitted_model, errors=result.errors)"
      ]
    }
  ],
  "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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}