{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Week 7 Lab: How to Construct a Model\n",
    "\n",
    "This week is about building a quantitative model of a phenomenon from scratch:\n",
    "map the variables, write the governing equation, express it as recursive,\n",
    "difference, and differential forms, fit it to data, and run simulations to probe\n",
    "its behavior.\n",
    "\n",
    "This notebook works two complete examples end to end. Use them as templates for\n",
    "the **two environment-behavior relations of your own choosing** that the\n",
    "assignment asks you to model.\n",
    "\n",
    "- **Part A** - Lever pressing under a random-ratio (RR) schedule, using the\n",
    "  discrete recursion model $n_{t+1} = (\\alpha p - \\beta)\\, n_t$.\n",
    "- **Part B** - Salivary responding to a conditioned stimulus (CS), using the\n",
    "  acquisition model $R_{t+1} = R_t + \\gamma I\\,(1 - R_t)$.\n",
    "\n",
    "For each, we fit the model to data and/or simulate how the parameters shape the\n",
    "predicted behavior."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "Import `numpy`, `matplotlib`, and `seaborn`, and define the shared simulation parameters (time grid and saturation strength) used later."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Imports and shared simulation parameters\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part A: Lever Pressing under a Random-Ratio Schedule\n",
    "\n",
    "The discrete recursion model is $n_{t+1} = (\\alpha p - \\beta)\\, n_t$, where $\\alpha$ amplifies responding per reinforcer, $\\beta$ is baseline decay, and $p$ is reinforcement probability. The single multiplier $(\\alpha p - \\beta)$ governs growth vs. decay.\n",
    "\n",
    "### Task A1: Simulate animal data\n",
    "\n",
    "Generate a noisy \"observed\" response trajectory from known parameters so we have something to fit."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Simulate a noisy response trajectory from known alpha, beta, p\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task A2: Fit by least squares\n",
    "\n",
    "Define the model and a sum-of-squared-error objective, then minimize it with `scipy.optimize.minimize` (bounded). Recover $\\alpha, \\beta, p$."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Define the model + SSE objective, then minimize() to fit\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task A3: Fit by maximum likelihood (Poisson)\n",
    "\n",
    "For count data, assume a Poisson error distribution and minimize the negative log-likelihood instead."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Poisson negative log-likelihood, then minimize() to fit\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task A4: Evaluate goodness of fit\n",
    "\n",
    "Compare the least-squares and MLE fits using R-squared, RMSE, MAE, and AIC."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Compute R2/RMSE/MAE/AIC and compare the two fits\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task A5: Simulate the dynamics\n",
    "\n",
    "Now explore the model itself. Plot the discrete recursion for several values of $p$, then compare the continuous-time and saturated (logistic-like) versions of the same relation."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Discrete recursion trajectories for several p values\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Continuous-time** linear form $dn/dt = (\\alpha p - \\beta)\\, n$ (analytical solution):"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Continuous-time linear model\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Saturated** form $dn/dt = (\\alpha p - \\beta)\\, n - \\gamma n^2$ (finite equilibrium):"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Saturated (logistic-like) model via Euler integration\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part B: Salivary Responding to a Conditioned Stimulus\n",
    "\n",
    "Respondent acquisition follows $R_{t+1} = R_t + \\gamma I\\,(1 - R_t)$, where $I$ is CS intensity and $\\gamma$ is the learning rate. We show the same relation as a recursion, a difference equation (Euler with $\\Delta t \\neq 1$), and a differential equation (closed form), each while varying $I$ and $\\gamma$.\n",
    "\n",
    "### Common settings"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Shared settings for the salivary-response simulations\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task B1: Recursion\n",
    "\n",
    "$R_{t+1} = R_t + \\gamma I\\,(1 - R_t)$"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Recursion: vary I (gamma fixed) and vary gamma (I fixed)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task B2: Difference equation (Euler)\n",
    "\n",
    "$\\Delta R_t = \\gamma I\\,(1 - R_t)\\,\\Delta t$"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Difference equation via Euler updates\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task B3: Differential equation (closed form)\n",
    "\n",
    "$dR/dt = \\gamma I\\,(1 - R) \\;\\Rightarrow\\; R(t) = 1 - (1 - R_0)\\,e^{-\\gamma I t}$"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Differential equation closed-form solution\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Task B4: Interactive 3D surfaces (Plotly)\n",
    "\n",
    "Visualize $R(I, \\gamma, t)$ as an animated surface over the $(I, \\gamma)$ grid for each of the three formulations."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Plotly 3D animated surface: recursion\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Wrap-up\n",
    "\n",
    "You have taken two relations from a verbal description to a fitted, simulatable model. For your assignment, repeat this lifecycle for two relations of your own: draw the life-cycle diagram, write the basic equation and its recursive/difference/differential forms, fit it to a fake dataset, and simulate how the parameters change the predictions. Watch for predictions that are biologically impossible (negative or runaway response rates) - those are signals to revise the model."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}