In [1]:
%matplotlib inline
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd
In [2]:
pd.read_csv("data/test-dataset.csv")
Out[2]:
In [10]:
d = pd.read_csv("data/test-dataset.csv")
In [14]:
fig, ax = plt.subplots()
ax.plot(d.time,d.obs,"o")
ax.set_xlabel("time")
_ = ax.set_ylabel("observable")
In [12]:
import scipy.stats
In [13]:
m, b, r_value, p_value, std_err = scipy.stats.linregress(d.time,
d.obs)
print("slope:",m)
print("intercept:",b)
print("R squared:",r_value**2)
In [16]:
fig, ax = plt.subplots()
ax.plot(d.time,d.obs,"o")
ax.set_xlabel("time")
ax.set_ylabel("observable")
x_range = np.array([0,10])
ax.plot(x_range,x_range*m + b,"--")
ax.text(8,0.30,"m: {:.2f}".format(m))
ax.text(8,0.25,"b: {:.2f}".format(b))
ax.text(8,0.20,"$R^{2}$: " + "{:.2f}".format(r_value**2))
None
In [17]:
def y_calc(x,m,b):
return m*x + b
In [18]:
fig, ax = plt.subplots()
ax.plot(d.time,d.obs,"ko")
ax.plot(d.time,y_calc(d.time,m=0.1,b=0))
ax.plot(d.time,y_calc(d.time,m=0.1,b=0.2))
ax.plot(d.time,y_calc(d.time,m=0.08,b=0.2))
Out[18]:
In [19]:
def y_residuals(x_obs,y_obs,m,b):
return (m*x_obs + b) - y_obs
In [20]:
def plot_figs(x_obs,y_obs,m,b,ax):
"""Convenience plotting function"""
ax[0].plot(x_obs,y_obs,"ko")
ax[0].plot(x_obs,y_calc(x_obs,m,b))
ax[0].set_ylabel("y")
ax[1].plot(x_obs,np.zeros(len(x_obs)),"--",color="gray")
ax[1].plot(x_obs,y_residuals(x_obs,y_obs,m,b))
ax[1].set_ylabel("y_model - y_obs")
ax[1].set_xlabel("x")
In [21]:
fig, ax = plt.subplots(2,1,figsize=(5,10),sharex=True)
plot_figs(d.time,d.obs,m=0.1,b=0,ax=ax)
plot_figs(d.time,d.obs,m=0.1,b=0.2,ax=ax)
plot_figs(d.time,d.obs,m=0.08,b=0.2,ax=ax)
In [23]:
sum_squared_residuals = [np.sum(y_residuals(d.time,d.obs,m=0.10,b=0.0)**2),
np.sum(y_residuals(d.time,d.obs,m=0.10,b=0.2)**2),
np.sum(y_residuals(d.time,d.obs,m=0.08,b=0.2)**2),
np.sum(y_residuals(d.time,d.obs,m=0.0696240601504,b=0.186085714286)**2)]
fig, ax = plt.subplots()
ax.plot([1],sum_squared_residuals[0],"o",color="orange")
ax.plot([2],sum_squared_residuals[1],"o",color="blue")
ax.plot([3],sum_squared_residuals[2],"o",color="green")
ax.plot([4],sum_squared_residuals[3],"o",color="black")
ax.plot([1,2,3,4],sum_squared_residuals,"-",color='gray',zorder=-10)
ax.set_xlabel("round")
ax.set_ylabel("SSR")
None
In [24]:
%%HTML
<video width="650" height="600" controls="controls">
<source src="https://raw.githubusercontent.com/harmsm/pythonic-science/master/chapters/02_regression/data/simplex.mp4" type="video/mp4">
</video>
In [30]:
def some_function(x):
a = x*x
return a, x
return x + x
y = some_function(5)
print(y)
In [ ]: