stockApproximation.py

#
# numpy : approximations de l'action coca-cola
# Vincent Legat - 2018
# Ecole Polytechnique de Louvain
#

from numpy  import *
from pandas import read_csv

import matplotlib
from matplotlib import pyplot as plt

matplotlib.rcParams['toolbar'] = 'None'
matplotlib.rcParams['lines.linewidth'] = 1
plt.rcParams['figure.facecolor'] = 'silver'


def computeApproximation(X,U,n):
  x  = linspace(X[0],X[-1]+100,100)
  uh = polyval(polyfit(X,U,n),x)
  plt.ylim((45,49))
  plt.xticks([])
  plt.plot(X,U,'-r')
  plt.plot(x,uh,'-b')


#
# -1- Les données
#

csvData = read_csv('stockCocaCola.csv')
X  = list(csvData["time"])
U  = list(csvData["value"])
plt.figure("Coca Cola stock")
plt.ylim((45,49))
plt.xticks([])
plt.plot(X,U,'-r')

#
# -2- Les prédictions financières
#

plt.figure("Predictions of Coca Cola stock value")
plt.subplot(2,2,1)
computeApproximation(X,U,1)
plt.subplot(2,2,2)
computeApproximation(X,U,4)
plt.subplot(2,2,3)
computeApproximation(X,U,6)
plt.subplot(2,2,4)
computeApproximation(X,U,8)

plt.show()