from typing import List import math from data import from matplotlib i

  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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from typing import List
import math
from data import *
from matplotlib import pyplot
COSTS = [24, 36, 110]
def main():
a1, b1 = part1(False)
print("Alpha 1, beta 1:")
print(a1, b1)
print("______________________")
a2, b2 = part2(False)
print("Alpha 2, beta 2:")
print(a2, b2)
print("______________________")
a31, b31, a32, b32, a33, b33 = part3(False)
print("Alpha 31, 32, 33; beta 31, 32, 33:")
print(a31, b31)
print(a32, b32)
print(a33, b33)
print("______________________")
# Intermediate values
total = 857038
ksi1 = a1 * (total ** b1)
ksi3 = (ksi1 / a2) ** (1 / b2)
ksi4 = (ksi3 / a31) ** (1 / b31)
ksi5 = (ksi3 / a32) ** (1 / b32)
ksi6 = (ksi3 / a33) ** (1 / b33)
count_tickets = [260.111, 126.387, 129.43]
count_tickets_sum = 0
for count in count_tickets:
count_tickets_sum += count
# n
n = [ksi4, ksi5, ksi6]
# p
p = []
for count in count_tickets:
p.append(count / count_tickets_sum)
# Cost all tickets
cost_all_tickets = 0
for i in range(len(n)):
cost_all_tickets += n[i] * p[i] * COSTS[i]
# Average
n_sum = 0
for num in n:
n_sum += num
average = cost_all_tickets / n_sum
# Total revenue
total_revenue = total * 1000 * average
print("Average, revenue:")
print(average, total_revenue)
def part1(display=False) -> (float, float):
rows = len(data1)
cols = len(data1[0])
xs = []
ys = []
test_xs = []
test_ys = []
for i in range(0, cols, 2):
for j in range(rows):
if j % 2 == 0:
xs.append(data1[j][i] * 1000)
ys.append(data1[j][i + 1] * 1000)
else:
test_xs.append(data1[j][i] * 1000)
test_ys.append(data1[j][i + 1] * 1000)
alpha, beta = calc_dependency(xs, ys)
if display:
display_test(alpha, beta, test_xs, test_ys)
check(test_xs, test_ys, alpha, beta, 0.1)
return alpha, beta
def part2(display=False) -> (float, float):
rows = len(data2)
cols = len(data2[0])
xs = []
ys = []
test_xs = []
test_ys = []
for i in range(0, cols, 2):
for j in range(0, rows, 3):
x = data2[j][i] * 1000
y = data2[j][i + 1] * 1000
if x > 0 and y > 0:
if j % 6 == 0:
xs.append(x)
ys.append(y)
else:
test_xs.append(x)
test_ys.append(y)
alpha, beta = calc_dependency(xs, ys)
if display:
display_test(alpha, beta, test_xs, test_ys)
check(test_xs, test_ys, alpha, beta, 0.1)
return alpha, beta
def part3(display=False) -> (float, float, float, float, float, float):
rows = 72
cols = len(data3[0])
ts = []
xs = []
ys = []
zs = []
test_ts = []
test_xs = []
test_ys = []
test_zs = []
for i in range(0, cols, 2):
for j in range(0, rows, 3):
if i < cols - 4:
ts.append(data3[j][i])
xs.append(data3[j][i + 1])
ys.append(data3[j + 1][i + 1])
zs.append(data3[j + 2][i + 1])
else:
test_ts.append(data3[j][i])
test_xs.append(data3[j][i + 1])
test_ys.append(data3[j + 1][i + 1])
test_zs.append(data3[j + 2][i + 1])
alpha1, beta1 = calc_dependency(ts, xs)
alpha2, beta2 = calc_dependency(ts, ys)
alpha3, beta3 = calc_dependency(ts, zs)
if display:
display_test(alpha1, beta1, test_ts, test_xs)
display_test(alpha2, beta2, test_ts, test_ys)
display_test(alpha3, beta3, test_ts, test_zs)
check(test_ts, test_xs, alpha1, beta1, 0.1)
check(test_ts, test_ys, alpha2, beta2, 0.1)
check(test_ts, test_zs, alpha3, beta3, 0.1)
return alpha1, beta1, alpha2, beta2, alpha3, beta3
def display_test(alpha: float, beta: float, x1: List[float], x2: List[float]):
dep_x1 = []
for x in x2:
dep_x1.append(alpha * pow(x, beta))
pyplot.plot(x1, x2, "ro", dep_x1, x2, "bo")
pyplot.show()
def calc_dependency(x1: List[float], x2: List[float]) -> (float, float):
assert (len(x1) == len(x2))
ln_x1 = []
ln_x2 = []
for i in range(len(x1)):
ln_x1.append(math.log(x1[i]))
ln_x2.append(math.log(x2[i]))
m1 = calc_m(ln_x1)
m2 = calc_m(ln_x2)
d1 = calc_d(ln_x1)
d2 = calc_d(ln_x2)
beta = math.sqrt(d1 / d2)
alpha = math.exp(m1 - beta * m2)
return alpha, beta
def check(x1: List[float], x2: List[float], alpha: float, beta: float, eps: float, display=False) -> bool:
def f(x, alpha, beta):
#print("Test: " + str(x) + ", " + str(alpha) + ", " + str(beta) + " => " + str(1 - math.exp(-alpha * pow(x / 1000, beta))))
return 1 - math.exp(-alpha * pow(x / 1000, beta))
dep_x1 = []
for x in x2:
dep_x1.append(alpha * pow(x, beta))
f_x1 = []
f_dep_x1 = []
for i in range(len(x1)):
f_x1.append(f(x1[i] / 2, alpha, beta))
f_dep_x1.append(f(dep_x1[i] / 2, alpha, beta))
#min_value = abs(min(x1) - min(dep_x1))
#max_value = abs(max(x1) - max(dep_x1))
#
#error = max_value - min_value
error = 0
for i in range(len(x1)):
error = max(error, abs(f_x1[i] - f_dep_x1[i]))
satisfied = error < eps
if display:
print("Error: " + str(error))
if satisfied:
print("Satisfied")
else:
print("Not satisfied")
return satisfied
def calc_m(xs: List[float]) -> float:
acc = 0
for x in xs:
acc += x
return acc / len(xs)
def calc_d(xs: List[float]) -> float:
m = calc_m(xs)
ys = []
for x in xs:
ys.append(pow(x - m, 2))
return calc_m(ys)
main()