-
Notifications
You must be signed in to change notification settings - Fork 0
/
series_cac40.py
executable file
·134 lines (104 loc) · 2.79 KB
/
series_cac40.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Un petit script pour distinguer des motifs
# dans des séries.
#
# le fichier doit être un ensemble de valeurs,
# avec une valeur par ligne.
import os, sys
import math
def read_file(fname):
try:
fd = open(fname)
except IOError:
print "unable to open requested file."
sys.exit(-1)
ret = []
lines = fd.readlines()
for line in lines:
ret.append(int(float(line))) # moche mais de toutes façons, avec
# toutes les moyennes que l'on fait
# la précision est balancée de toutes
# façons.
fd.close()
return ret
def mediane(l):
# Calcule la médiane d'une liste.
div = len(l)
acc = 0
for e in l:
acc += e
return acc / div
def variance(l):
div = len(l)
acc = 0
med = mediane(l)
for e in l:
acc += (e - med)**2
return acc / div
def ecart_type(l):
return int(math.sqrt(variance(l)))
def pente(l):
return (l[-1] - l[0]) / (len(l))
def pente_reg_lin(l):
# calcul de la pente en utilisant la méthode de régression
# linéaire d'excel. voir :
# http://office.microsoft.com/fr-ca/excel-help/pente-HP005209264.aspx
_x = int(len(l) / 2) # car les valeurs sont linéaires
_y = mediane(l)
num = 0
for i, y in enumerate(l):
num += (i - _x) * (y - _y)
den = 0
for j in range(len(l)):
den += (j - _x) ** 2
return num / den
def pente_davinson(l):
# l'équation de calcul de la pente qu'a utilisée davinson
# je passe n = len(l)
def critere(prev_e, e):
# Ceci est le critère, qui permet de classer des élements
# selon qu'ils appartiennent à la liste ou pas.
return (abs(mediane(prev_e) - mediane(e)) <= 500 and abs(ecart_type(prev_e) - ecart_type(e)) <= 50)
def process_input(l):
l1 = []
ret = []
# D'abord, casser la liste en plusieurs parties dont on calcule la variance et l'espérance
for i in range(len(l) / 2):
t = [l.pop(0), l.pop(0)]
l1.append(t)
# Ensuite calcule les médianes et variances de chaque élements et fusionne les élements contigus
# qui sont proches.
prev_e = l1[0] # l'élement précedent
ret.append(prev_e)
for e in l1[1:]:
# TODO: définir un seuil correct - avec de vrais critères.
if critere(prev_e, e):
prev_e += e
prev_e = e
else:
# rajoute les caractéristiques finales de la section
# à la fin.
d = {}
d["vmoyenne"] = mediane(e)
d["vecart"] = ecart_type(e)
d["vpente"] = pente_reg_lin(e)
if d["vmoyenne"] > 4000:
d["moyenne"] = "elevé"
elif d["vmoyenne"] > 2000:
d["moyenne"] = "moyenne"
else:
d["moyenne"] = "faible"
if d["vecart"] > 300:
d["ecart"] = "elevé"
elif d["vecart"] > 100:
d["ecart"] = "moyen"
else:
d["ecart"] = "faible"
ret.append([e, d])
prev_e = e
return ret
if __name__ == "__main__":
for e in process_input(read_file(sys.argv[1])):
print e
print ""