Market Prediction using Python Fitting Tools (Numpy, Scipy)

[]
In [1]:
import numpy

# Generate artificial data = straight line with a=0 and b=1
# plus some noise.
# xdata = numpy.array([0.0,1.0,2.0,3.0,4.0,5.0])
xdata = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0])

# we will repace the original data into real data
# ydata = numpy.array([0.1,0.9,2.2,2.8,3.9,7.1])
ydata = numpy.array([10.4, 10.5, 11.5, 13.0, 15.1])

# Initial guess.
x0    = numpy.array([0.0, 0.0, 0.0])
In [2]:
plot( xdata, ydata, 'o-')
Out[2]:
[<matplotlib.lines.Line2D at 0xaf4fa72c>]
In [3]:
sigma = numpy.ones(5)
print sigma
[ 1.  1.  1.  1.  1.]

In [4]:
# The objective is defined by 2D polymer.
def func(x, a, b, c):
    return a + b*x + c*x*x
In [5]:
import scipy.optimize as optimization

results = optimization.curve_fit(func, xdata, ydata, x0, sigma)
abc = results[0]
print 'results = ', results
print 'abc =', abc
results =  (array([ 10.36285714,  -0.09571429,   0.32142857]), array([[ 0.00556735, -0.00484898,  0.00089796],
       [-0.00484898,  0.00781224, -0.00179592],
       [ 0.00089796, -0.00179592,  0.00044898]]))
abc = [ 10.36285714  -0.09571429   0.32142857]

In [16]:
# In order to see the future, more data is appended.
fit_xdata = numpy.append( xdata, [5.0, 6.0])
fit_y = func( fit_xdata, abc[0], abc[1], abc[2])
print fit_y
[ 10.36285714  10.58857143  11.45714286  12.96857143  15.12285714  17.92
  21.36      ]

In [17]:
plot( xdata, ydata, 'o')
plot( fit_xdata, fit_y, '-')
title( 'Market Prediction based on Curve-Fitting')
xlabel( 'Quater')
ylabel( 'Grwoth(%)')
show()

Comments

Popular posts from this blog

Missing Lester Young~~

Jeff Koons and Ciciolina as Kitsch artists

This is my Google blog.