A mathematical model is a description of a system which uses abstractions and mathematical language. The development of of one of these is know as mathematical modelling. The mathematical models usally are made of relations and variables, such relations can be described by operators (algebraical and/or differential), functions, etc. The variables are abstractions of the studied system's parameters which can be quantified.
Nowdays in almost every field of the human knowledge is necessary to deal with data. Use a programming language becomes an essential tool when we want to automatize the implementation of our models.
1) implement a function to read data from a directory taking in count the file extention.
In [2]:
import os
import numpy as np
path = "/data/"
def read_dir(path, ext):
l = []
for f in os.listdir(os.getcwd()+path):
if f.endswith(ext):
r = open(os.getcwd()+path+f).read()
r = np.array(r[:-1].split())
l.append({f:r})
return l
2) Use the funtion to load the data on memory
In [9]:
input_data = read_dir(path,'.in')
ans_data = read_dir(path,'.ans')
3) Create a set of functions that implements a model to treat our data.
In [10]:
def price(l):
p = int(l[0])
a = int(l[1])
b = int(l[2])
c = int(l[3])
d = int(l[4])
n = int(l[5])
P = []
for k in range(int(n)):
P.append(p*(np.sin(a*(k+1)+b)+np.cos(c*(k+1)+d)+2))
return P
In [11]:
def max_decline(prices):
max_dif = 0.0
dif = max(prices)-min(prices)
if dif > 0.0:
max_dif = dif
return max_dif
In [12]:
def max_dec_list(data):
declines = []
for d in data:
key = d.keys()[0]
value = d.values()[0]
stock_prices = price(value)
declines.append({key:[max_decline(stock_prices)]})
return declines
comp_data = max_dec_list(input_data)
4) Compute the error between the computed data and the teorical.
In [19]:
for l1 in ans_data:
for l2 in comp_data:
if l1.keys()[0][0:2] == l2.keys()[0][0:2]:
print l1.keys()[0], l2.keys()[0]
print 'error in file', l1.keys()[0][0:2], 'is ', '%.6f' % abs(float(l1.get(l1.keys()[0])[0])- float(l2.get(l2.keys()[0])[0]))
In [ ]: