SHARE
TWEET

Untitled

a guest Oct 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "markdown",
  5.    "metadata": {},
  6.    "source": [
  7.     "### Session 4.1 Preclass"
  8.    ]
  9.   },
  10.   {
  11.    "cell_type": "code",
  12.    "execution_count": 11,
  13.    "metadata": {},
  14.    "outputs": [],
  15.    "source": [
  16.     "from scipy.optimize import minimize\n",
  17.     "import numpy as np\n",
  18.     "import matplotlib.pyplot as plt"
  19.    ]
  20.   },
  21.   {
  22.    "cell_type": "code",
  23.    "execution_count": 12,
  24.    "metadata": {},
  25.    "outputs": [],
  26.    "source": [
  27.     "# objective function\n",
  28.     "# x = [mu, lambda, alpha, beta]\n",
  29.     "mu_e = 2.3\n",
  30.     "mu_var = 0.5\n",
  31.     "sig_e = 2.75\n",
  32.     "sig_var = 1\n",
  33.     "def f(x):\n",
  34.     "    mu = (mu_e - x[0])**2\n",
  35.     "    sig = (sig_e - (x[3]/(x[2]-1)))**2\n",
  36.     "    mu_vr = (mu_var - (x[3]/((x[2]-1)*x[1])))**2\n",
  37.     "    sig_vr = (sig_var - (x[3]**2/((x[2]-1)**2*(x[2]-2))))**2\n",
  38.     "    return mu+sig+mu_vr+sig_vr\n",
  39.     "\n"
  40.    ]
  41.   },
  42.   {
  43.    "cell_type": "code",
  44.    "execution_count": 19,
  45.    "metadata": {},
  46.    "outputs": [
  47.     {
  48.      "name": "stdout",
  49.      "output_type": "stream",
  50.      "text": [
  51.       "Started at [1 1 3 3]\n",
  52.       "Ended at [ 2.29999808  5.50002766  9.562539   23.5469938 ]\n",
  53.       "f([ 2.29999808  5.50002766  9.562539   23.5469938 ]) = 0.000000\n"
  54.      ]
  55.     }
  56.    ],
  57.    "source": [
  58.     "initial = np.array([1, 1,3,3])\n",
  59.     "result = minimize(f, initial)\n",
  60.     "final = result.x\n",
  61.     "print('Started at', initial)\n",
  62.     "print('Ended at', final)\n",
  63.     "print('f(%s) = %.6f' % (final, f(final)))"
  64.    ]
  65.   },
  66.   {
  67.    "cell_type": "code",
  68.    "execution_count": 20,
  69.    "metadata": {},
  70.    "outputs": [
  71.     {
  72.      "name": "stdout",
  73.      "output_type": "stream",
  74.      "text": [
  75.       "[ 2.3       5.5       9.5625   23.546875]\n"
  76.      ]
  77.     }
  78.    ],
  79.    "source": [
  80.     "# solution 2: solve for system of equation\n",
  81.     "from scipy.optimize import fsolve\n",
  82.     "import math\n",
  83.     "\n",
  84.     "def equations(x):\n",
  85.     "    mu = x[0] - mu_e\n",
  86.     "    sig = (x[3]/(x[2]-1)) -sig_e\n",
  87.     "    mu_vr = (x[3]/((x[2]-1)*x[1])) - mu_var\n",
  88.     "    sig_vr =(x[3]**2/((x[2]-1)**2*(x[2]-2))) -  sig_var\n",
  89.     "    return (mu, sig, mu_vr, sig_vr)\n",
  90.     "\n",
  91.     "result =  fsolve(equations, (1, 1,3,3))\n",
  92.     "\n",
  93.     "print(result)\n"
  94.    ]
  95.   },
  96.   {
  97.    "cell_type": "code",
  98.    "execution_count": null,
  99.    "metadata": {},
  100.    "outputs": [],
  101.    "source": []
  102.   }
  103.  ],
  104.  "metadata": {
  105.   "kernelspec": {
  106.    "display_name": "Python 3",
  107.    "language": "python",
  108.    "name": "python3"
  109.   },
  110.   "language_info": {
  111.    "codemirror_mode": {
  112.     "name": "ipython",
  113.     "version": 3
  114.    },
  115.    "file_extension": ".py",
  116.    "mimetype": "text/x-python",
  117.    "name": "python",
  118.    "nbconvert_exporter": "python",
  119.    "pygments_lexer": "ipython3",
  120.    "version": "3.6.5"
  121.   }
  122.  },
  123.  "nbformat": 4,
  124.  "nbformat_minor": 2
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top