{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ethical-frontier",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyerrors as pe\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from packaging import version\n",
    "if version.parse(pe.__version__) < version.parse(\"2.6.0\"):\n",
    "    raise Exception(f\"v2.6.0 or newer is required for this example, you are using {pe.__version__}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "incredible-posting",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_test = {'a':[0,1,2,3,4,5],'b':[0,1,2,3,4,5]}\n",
    "y_test = {'a':[pe.Obs([np.random.normal(i, i*1.5, 1000)],['ensemble1']) for i in range(1,7)],\n",
    "          'b':[pe.Obs([np.random.normal(val, val*1.5, 1000)],['ensemble1']) for val in [1.0,2.5,4.0,5.5,7.0,8.5]]}\n",
    "for key in y_test.keys():\n",
    "    [item.gamma_method() for item in y_test[key]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "subtle-malaysia",
   "metadata": {},
   "outputs": [],
   "source": [
    "def func_a(a, x):\n",
    "    return a[1] * x + a[0]\n",
    "\n",
    "def func_b(a, x):\n",
    "    return a[2] * x + a[0]\n",
    "\n",
    "funcs_test = {\"a\": func_a,\"b\": func_b}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "45f67973",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fit with 3 parameters\n",
      "Method: migrad\n",
      "Optimization terminated successfully.\n",
      "chisquare/d.o.f.: 0.8085703524653507\n",
      "fit parameters [0.97737577 1.01063624 1.47900852]\n",
      "chisquare/expected_chisquare: 0.8121288230401409\n"
     ]
    }
   ],
   "source": [
    "output_test = pe.fits.least_squares(x_test,y_test,funcs_test,method='migrad',expected_chisquare=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "technological-rolling",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_test.gamma_method()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "wooden-potential",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18284 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "colour= {'a':'red','b':'black'}\n",
    "plt.figure()\n",
    "for key in funcs_test.keys():\n",
    "    plt.errorbar(x_test[key],[o.value for o in y_test[key]],ls='none',marker='*',color=colour[key],yerr=[o.dvalue for o in y_test[key]],capsize=3,label=key)\n",
    "    plt.plot([x_val for x_val in x_test[key]],[funcs_test[key](output_test.fit_parameters,x_val) for x_val in x_test[key]],color=colour[key],label='func_'+key)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
