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()