Search code examples
pythonpython-2.7regressioncurve-fittingexponential

Am I correctly writing code for curve fitting models? The line is way off for every model except for linear regression


I'm very new to coding in python and could use some support in answering this question. I wrote the code for the problem, but my models do not look correct.

Below is the question:

Optimization - Curve Fit Given 16 pairs of prices (as dependent variable) and corresponding demands (as independent variable), use the curve fitting tool to estimate the best fitting linear, exponential, logarithmic, and power curves.

Price Demand 127 3420 134 3400 136 3250 139 3410 140 3190 141 3250 148 2860 149 2830 151 3160 154 2820 155 2780 157 2900 159 2810 167 2580 168 2520 171 2430

Below is my code:

from pylab import *
from numpy import *
from numpy.random import *
from scipy.optimize import *
# linear regression
#called in curve fitting model
def linreg(x,a,b):
    return a*x+b

# data

x = [3420, 3400, 3250, 3410, 3190, 3250, 2860, 2830, 3160, 2820, 2780, 2900, 2810, 2580, 2520, 2430]
x = np.asarray(x, dtype=np.float64)
y = [127, 134, 136 ,139, 140, 141, 148, 149, 151, 154, 155, 157, 159, 167, 168, 171]
y = np.asarray(y, dtype=np.float64)

#liner regression
# curve fitting
attributes,variances = curve_fit(linreg,x,y)
# estimated y
y_modeled = x*attributes[0]+attributes[1]
# plot true and modeled results
plot(x,y,'ob',markersize=2)
plot(x,y_modeled,'-r',linewidth=1)
show()

# exponential regression
#called in curve fitting model
def expon(x, a, b, c):
    return a * np.exp(-b * x) + c

#exponential
# curve fitting
attributes,variances = curve_fit(expon,x,y)
# estimated y
y_modeled = x*attributes[0]+attributes[1]
# plot true and modeled results
plot(x,y,'ob',markersize=2)
plot(x,y_modeled,'-r',linewidth=1)
show()


# logarithmic function
def logar(x, p1,p2):
  return p1*np.log(x)+p2
#logarithmic
# curve fitting
attributes,variances = curve_fit(logar,x,y)
# estimated y
y_modeled = x*attributes[0]+attributes[1]
# plot true and modeled results
plot(x,y,'ob',markersize=2)
plot(x,y_modeled,'-r',linewidth=1)
show()

#power curve function? MAybe? 
def powerlaw(x,a,b):
    return a*(x**b)

#power curves
# curve fitting
attributes,variances = curve_fit(powerlaw,x,y)
# estimated y
y_modeled = x*attributes[0]+attributes[1]
# plot true and modeled results
plot(x,y,'ob',markersize=2)
plot(x,y_modeled,'-r',linewidth=1)
show()

When I run the linear regression model the line fits the data. However, whenever I run the other curve fitting options to line is WAY above the data points. It doesn't fit the data at all.

Thank you! Any help would be greatly appreciated. Our TAs are on strike, so I have no one that is available to help.


Solution

  • You are not calling the models correctly. Try these, which have the form "function(x, pointer_to_parameters)"

    y_modeled = linreg(x, *attributes)
    y_modeled = expon(x, *attributes)
    y_modeled = logar(x, *attributes)
    y_modeled = powerlaw(x, *attributes)
    

    With these, I get excellent plots from your code.