-
Notifications
You must be signed in to change notification settings - Fork 2
/
interpolation.py
171 lines (133 loc) · 5.52 KB
/
interpolation.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
import numpy as np
import scipy as sc
from scipy.special import erf
import random as rd
import matplotlib.pyplot as plt
import math
from collections import Counter
class LinearInterp:
def __init__(self, x_data, y_data, interval):
self.x_data = x_data
self.y_data = y_data
self.cumulative_data = self.cumulative_full_compute(interval[0], interval[1])
self.interval = interval
def do_interp(self, x):
for a in range(0, len(self.y_data)):
if x >= self.x_data[a] and x <= self.x_data[a+1]:
dy = self.y_data[a+1] - self.y_data[a]
dx = self.x_data[a+1] - self.x_data[a]
coef_b = self.y_data [a] - (self.x_data[a] * dy) / dx
coef_a = dy / dx
new_point = coef_b + x * coef_a
return [new_point, (coef_a, coef_b),(self.x_data[a], self.x_data[a+1])]
def do_cumulative(self, a, b):
cumulative = 0
for x in self.x_data:
if x>=a and x<=b:
result = self.do_interp(x)
cumulative += result[2][1] ** 2 * result[1][0] /2 + result[2][1]*result[1][1] - result[2][0] ** 2 * result[1][0] /2 - result[2][0]*result[1][1]
return cumulative
def cumulative_full_compute(self, interval1, interval2):
temporary_list =[]
for knot in range(0,len(self.x_data)):
temporary_list.append(self.do_cumulative(interval1, self.x_data[knot]))
return temporary_list
#This function generates the points for a given cumulative. I have to limit the possible cumulative because if I did't have that point to interpolate, I can't sample in this interval.
def generate_random_point(self):
c = rd.uniform(min(self.cumulative_data), max(self.cumulative_data))
#c = rd.uniform(0,1)
for knot in range(0,len(self.cumulative_data)):
cumulative_i = self.cumulative_data[knot]
if cumulative_i >= c:
cumulative_j = self.cumulative_data[knot+1]
dy = self.x_data[knot+1] - self.x_data[knot]
dx = cumulative_j - cumulative_i
coef_b = self.x_data[knot] - (cumulative_i * dy) / dx
coef_a = dy / dx
new_point = coef_b + c * coef_a
return [new_point, (coef_a, coef_b),(self.x_data[knot], self.x_data[knot+1]), c]
class PDF:
def __init__(self):
self.count = 1
def gaussian_oned_pdf(self, x, mean, sigma):
return np.exp(-np.power(x - mean, 2.) / (2 * np.power(sigma, 2.))) * (1/ (np.sqrt(2 * np.pi)*sigma))
def gaussian_oned_cdf(self,b, mean, sigma):
arg = (b - mean)/(np.sqrt(2) * sigma)
return 1/2 * (1 + erf(arg))
def coin_toss(self, points, probability):
i =0
j=0
for a in range(0, points):
p = rd.uniform(0,1)
if p <= probability:
i+=1
else:
j+=1
return [i,j]
def gaussian_twod(self, x, y, meanx, meany, sigmax, sigmay):
gaus_x = np.exp(-np.power(x - meanx, 2.) / (2 * np.power(sigmax, 2.))) * (1/ (np.sqrt(2 * np.pi)*sigmax))
gaus_y = np.exp(-np.power(y - meany, 2.) / (2 * np.power(sigmay, 2.))) * (1/ (np.sqrt(2 * np.pi)*sigmay))
return gaus_x * gaus_y
class Util:
def __init__(self):
self.true = 0
def mean_function(self,points):
den = len(points)
sum_p = [a/den for a in points]
return np.sum(sum_p)
def var_function(self, points, mean):
den = len(points)
var_p = [(a - mean)**2 / den for a in points]
return np.sum(var_p)
def skew(x, sigma, mean):
list_sum = []
for point in x:
list_sum.append((point - mean)**3)
return (np.sum(list_sum) / (len(x) * sigma**3))
def curtoise(x, sigma, mean):
list_sum = []
for point in x:
list_sum.append((point - mean)**4)
return ((np.sum(list_sum) / (len(x) * sigma**4)) - 3)
def covariance_func(self,x,y, x_mean, y_mean):
sum_list = []
for a in range(0, len(x)):
sum_list.append((x[a]-x_mean)*(y[a]-y_mean) / (len(x) - 1))
return np.sum(sum_list)
def correlation_func(self,cov, sigmax, sigmay):
return cov/(sigmax * sigmay)
class Estimators:
def __init__(self):
self.init = 0
def estimator_1(self, points):
soma = np.sum(points)
return soma/(len(points))
def estimator_2(self, points):
soma = 0
for a in range(0,10):
soma+=points[a]
return soma/10
def estimator_3(self, points):
soma = np.sum(points)
return soma/(len(points)-1)
def estimator_4(self, points):
return 1.8
def estimator_5(self, points):
product = np.prod(points)
coeficient =1/ len(points)
return product**coeficient
def estimator_6(self, points):
data = Counter(points)
return data.most_common(1)[0][0]
def estimator_7(self, points):
max_point = max(points)
min_point = min(points)
return (max_point + min_point)/2
def estimator_8(self, points):
length = len(points)
interval = math.floor(length/2)
soma = 0
for a in range(0,interval):
soma += points[2*a]
return soma/interval