{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy as scp\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regresion and sum of squares optimisation\n",
    "This exercise will underline how to use the sum of squares to find the optimal parameters to fit a set of datapoints, and then compare to the expected values that can be computed for a linear curve."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load \"data.csv\" and extract the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot the data and try to find what type of function would best fit the points. Create such a fit by guessing the parameters and plot it on top of your data points."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot the residual and the squared residual for these given parameters. Find the sum for both of these quantities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, by keeping b constant and iterating on possible values of a, plot the sum of squared residuals with respect to a. What is the minimum value ?\n",
    "Repeat this for b by keeping a constant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(Optionnal) An interesting way of visualizing the optimal points and making sure the points are not local minimas is to do a 2D plot of the sum of residuals squared with respect to a and b. This can be done using the `plt.contour(a, b, RSS)` to plot the lines of equal sum, or `plt.contourf(a, b, RSS)` to represent the sum values as a gradient.\n",
    "\n",
    "Find the optimal values of a and b using this technique."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the formulas for the expected values of a and b seen in the lectures, compare your results obtained by iteration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Compute the Fisher statistic for this fit. Can you say with a confidence level of $\\alpha=0.05$ that this curve fits the points correctly ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The scipy function `scipy.optimize.curve` is a great tool for fitting your data with specific functions.\n",
    "Use what you have learned about functions, and use this function to fit the data with a sine curve.\n",
    "\n",
    "Note : Once defined using `def function_name(params)`, you can pass `function_name` as a parameter to certain functions !"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
