-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram_old.py
More file actions
143 lines (114 loc) · 2.94 KB
/
histogram_old.py
File metadata and controls
143 lines (114 loc) · 2.94 KB
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
135
136
137
138
139
140
141
142
143
import scipy.stats as st
import math
from copy import copy
import numpy
d = {}
for i in range (0,10000):
k = float(i)/10000.
d[k] = st.norm.ppf(k)
d[1] = numpy.Inf
def ppf(x):
x = int(x*10000)/10000.0
try:
return d[x]
except KeyError:
return numpy.NAN
class Histograma:
def __init__(self, valores=[], bins=0):
self.total = 0
self.createHistograma(valores, bins)
def createHistograma(self, valores=[], bins=0):
#print "valores", valores
if bins == 0:
self.bins = math.sqrt(len(valores))
if len(valores) == 0:
self.valores = []
else:
self.valores = copy(valores)
self.valores.sort()
min = float(self.valores[0])
#print "min", min
max = float(self.valores[-1])
#print "max", max
if min == max:
self.bins = 1
pivo = 1
else:
pivo = (max-min)/(self.bins-1)
self.pivo = pivo
#print self.pivo, max, min
self.hist = []
tmp = min
i = 0
while len(self.hist) < self.bins:
self.hist.append([tmp,tmp+pivo,0])
for i in range(i, len(valores)):
if self.valores[i] >= tmp and self.valores[i] < tmp+pivo:
self.hist[-1][2] += 1
self.total += 1
else:
break
tmp+= pivo
def updateHistograma(self, valores):
self.valores = copy(valores)
self.valores.sort()
if self.valores[0] < self.hist[0][0]:
min = self.valores[0]
else:
min = self.hist[0][0]
if self.valores[-1] > self.hist[-1][1]:
max = self.valores[-1]
else:
max = self.hist[-1][1]
self.bins = math.sqrt(self.total)
if min == max:
self.bins = 1
pivo = 1
else:
pivo = float(max-min)/float(self.bins-1)
self.pivo = pivo
while min < self.hist[0][0]:
self.hist = [[self.hist[0][0]-self.pivo,self.hist[0][0],0]]+self.hist
while max >= self.hist[-1][1]:
self.hist = self.hist + [[self.hist[-1][1], self.hist[-1][1]+self.pivo, 0]]
i = 0
b = 0
while b < len(self.hist):
tmp = self.hist[b]
for i in range(i, len(self.valores)):
if self.valores[i] >= tmp[0]:
if self.valores[i] < tmp[1]:
tmp[2] += 1
self.total += 1
else:
break
b +=1
def normalizedHistograma (self):
normalizedHist = []
k = 0
for i in range(len(self.hist)):
k += self.hist[i][-1]
normalizedHist.append(copy(self.hist[i]))
normalizedHist[-1][-1] = float(k)
for j in normalizedHist:
#j[-1] = st.norm.ppf(float(j[-1])/float(self.total))
j[-1] = ppf(float(j[-1])/float(self.total))
return normalizedHist
def getNormalizedValues(self, valores):
normalizedHist = self.normalizedHistograma()
#print "hist", self.hist
#print "norm hist", normalizedHist
if type(valores) != type([]):
valores = [valores]
results = []
for i in valores:
for j in normalizedHist:
if i >= j[0]:
if i < j[1]:
if j[2] < -2.5: final = -3.09
elif j[2] > 2.5: final = 3.09
else: final = j[2]
results.append(final)
break
#print results, normalizedHist
return results