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

"""
Explanation: What does the following code do?
End of explanation
"""


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

"""
Explanation: What does the following code do?
End of explanation
"""


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

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

"""
Explanation: Defines a linear function of x with the slope a and the intercept b.
What does the following code do?

What is the param business?
What is the y bit?
End of explanation
"""


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

param_guesses = [0,0]
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
print(fit.x)

"""
Explanation: Defines a residuals function for linear that compares the output of linear to whatever's in y.  
params holds a and b in a list.

What does the following code do?
End of explanation
"""


x_range = np.linspace(np.min(d.x),np.max(d.x),100)

plt.plot(d.x,d.y,"o")
plt.plot(x_range,linear(x_range,fit_a,fit_b))
plt.plot(x_range,linear(x_range,0,0))


"""
Explanation: Uses least_squares regression to find values of a and b that minimize linear_r given d.x and d.y.  
The fit parameters end up in fit_a and fit_b
The $SSR$ is stored in sum_of_square_residuals

What does the following code do?

What the heck is linspace?
What are we plotting?
End of explanation
"""


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

# Read data
d = pd.read_csv("data/dataset_0.csv")
plt.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)
plt.plot(x_range,linear(x_range,fit_a,fit_b))
print(fit.cost)

"""
Explanation: Put together
End of explanation
"""


def binding(x,a,b):
    """binding equation with b"""
    
    return a*(b*x/(1 + b*x))
    

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

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

# Perform regression
param_guesses = [5,0.3]
fit = scipy.optimize.least_squares(binding_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)
plt.plot(x_range,binding(x_range,fit_a,fit_b))

print(fit)


def model(a,b,x):
    return a*(b*x/(1 + b*x))

#Residuals function
def model_r(param,x,y):
    return model(param[0],param[1],x) - y

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

# Perform regression
param_guesses = [5,0.3]
fit = scipy.optimize.least_squares(model_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)
plt.plot(x_range,model(x_range,fit_a,fit_b))

print(fit.cost)

"""
Explanation: <h4>For your assigned model:</h4>
<ul>
    <li>Write a function and residuals function</li>
    <li>Estimate the parameters of the model</li>
    <li>Plot the data and the model on the same graph</li>
    <li>Write the SSR and number of parameters on the board</li>
    <li>If you finish early: plot the residuals and decide if you like your model</li>
    <li>If you're still waiting: try to figure out which model best fits dataset_1.csv</li>
</ul>
<br/>
<div style="font-size:30px;" >
<div class="row">
    <div class="col-xs-6">$y = a \Big ( \frac{bx}{1 + bx} \Big )$</div>
    <div class="col-xs-6">$y = a \Big ( \frac{bx^{c}}{1 + bx^{c}} \Big )$</div>
</div>

<div class="row">
    <div class="col-xs-6">$y = a(1 - e^{-bx})$</div>
    <div class="col-xs-6">$y = a + bx^{2} + cx^{3}$</div>
</div>
<div class="row">
    <div class="col-xs-6">$y = a + bx^{2} + cx^{3} + dx^{4}$</div>
    <div class="col-xs-6">$y = asin(bx + c)$</div>
</div>
<div class="row">
    <div class="col-xs-6">$y = aln(x + b)$</div>
    <div class="col-xs-6">$y = aln(bx + c)$</div>
</div>
End of explanation
"""


