In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pylab as plt

In [2]:
import scipy.optimize
import pandas as pd

What does the following code do?


In [ ]:
d = pd.read_csv("data/dataset_0.csv")
fig, ax = plt.subplots()
ax.plot(d.x,d.y,'o')

What does the following code do?


In [ ]:
def linear(x,a,b):
    return a + b*x

What does the following code do?


In [ ]:
def linear(x,a,b):
    return a + b*x

def linear_r(param,x,y):
    return linear(x,param[0],param[1]) - y

What does the following code do?


In [ ]:
def linear_r(param,x,y):                     # copied from previous cell
    return linear(x,param[0],param[1]) - y   # copied from previous cell

param_guesses = [1,1]
fit = scipy.optimize.least_squares(linear_r,param_guesses,
                                   args=(d.x,d.y))
fit_a = fit.x[0]
fit_b = fit.x[1]
sum_of_square_residuals = fit.cost

What does the following code do?

  • What the heck is linspace?
  • What are we plotting?

In [ ]:
x_range = np.linspace(np.min(d.x),np.max(d.x),100)

fig, ax = plt.subplots()
ax.plot(d.x,d.y,"o")
ax.plot(x_range,linear(x_range,fit_a,fit_b))

Put together


In [7]:
def linear(x,a,b):
    """Linear model of x using a (slope) and b (intercept)"""
    return a + b*x

def linear_r(param,x,y):
    """Residuals function for linear"""
    return linear(x,param[0],param[1]) - y

fig, ax = plt.subplots()

# Read data
d = pd.read_csv("data/dataset_0.csv")
ax.plot(d.x,d.y,'o')

# Perform regression
param_guesses = [1,1]
fit = scipy.optimize.least_squares(linear_r,param_guesses,args=(d.x,d.y))
fit_a = fit.x[0]
fit_b = fit.x[1]
sum_of_square_residuals = fit.cost

# Plot result
x_range = np.linspace(np.min(d.x),np.max(d.x),100)
ax.plot(x_range,linear(x_range,fit_a,fit_b))

fit


Out[7]:
 active_mask: array([0., 0.])
        cost: 4.261679630541931
         fun: array([ 1.10967611,  0.37884357,  0.7262888 ,  0.19610326, -0.07363268,
        0.87402953, -0.04205402,  0.34759648, -0.25938691,  0.03667523,
       -0.17507308, -0.68958901, -0.08836881, -0.34453537, -0.55445953,
       -0.28176203, -0.30205479, -0.53989842,  0.02746354, -0.86197952,
       -0.2592627 , -0.24275086, -0.42436478, -0.32125392,  0.05872358,
       -0.29706174, -0.14169496, -0.89286187,  0.2943461 , -0.30268024,
       -0.46564262,  0.41435537,  0.40923643, -0.0323235 ,  0.25418016,
        0.81366676,  0.28895853,  0.0650563 ,  0.60869764,  0.33681213,
        0.35198181])
        grad: array([ 1.17124844e-08, -2.48689958e-14])
         jac: array([[ 1.        ,  0.        ],
       [ 1.        ,  0.25      ],
       [ 1.        ,  0.5       ],
       [ 1.        ,  0.75      ],
       [ 1.00000001,  1.        ],
       [ 1.        ,  1.25      ],
       [ 1.        ,  1.5       ],
       [ 0.99999999,  1.75      ],
       [ 1.        ,  2.        ],
       [ 1.00000001,  2.25      ],
       [ 1.        ,  2.5       ],
       [ 1.00000001,  2.75      ],
       [ 0.99999999,  3.        ],
       [ 0.99999999,  3.25      ],
       [ 0.99999999,  3.5       ],
       [ 0.99999999,  3.75      ],
       [ 0.99999999,  4.        ],
       [ 0.99999999,  4.25      ],
       [ 1.00000001,  4.5       ],
       [ 1.00000001,  4.75      ],
       [ 1.00000001,  5.        ],
       [ 1.00000001,  5.25      ],
       [ 1.00000001,  5.5       ],
       [ 0.99999999,  5.75      ],
       [ 0.99999999,  6.        ],
       [ 0.99999999,  6.25      ],
       [ 0.99999999,  6.5       ],
       [ 0.99999999,  6.75      ],
       [ 0.99999999,  7.        ],
       [ 0.99999999,  7.25      ],
       [ 1.00000001,  7.5       ],
       [ 1.00000001,  7.75      ],
       [ 1.00000001,  8.        ],
       [ 1.00000001,  8.25      ],
       [ 1.00000001,  8.5       ],
       [ 0.99999999,  8.75      ],
       [ 1.00000001,  9.        ],
       [ 1.00000001,  9.25      ],
       [ 1.00000001,  9.5       ],
       [ 1.00000001,  9.75      ],
       [ 1.00000001, 10.        ]])
     message: 'Both `ftol` and `xtol` termination conditions are satisfied.'
        nfev: 3
        njev: 3
  optimality: 1.1712484371262377e-08
      status: 4
     success: True
           x: array([1.13006323, 0.31994236])

For your assigned model:

  • Write a function and residuals function
  • Estimate the parameters of the model
  • Plot the data and the model on the same graph
  • Write the SSR and number of parameters on the board
  • If you finish early: plot the residuals and decide if you like your model
  • If you're still waiting: try to figure out which model best fits dataset_1.csv

$y = a \Big ( \frac{bx}{1 + bx} \Big )$
$y = a \Big ( \frac{bx^{c}}{1 + bx^{c}} \Big )$
$y = a(1 - e^{-bx})$
$y = a + bx^{2} + cx^{3}$
$y = a + bx^{2} + cx^{3} + dx^{4}$
$y = asin(bx + c)$
$y = aln(x + b)$
$y = aln(bx + c)$

In [ ]:


In [ ]: