Skip to content

Instantly share code, notes, and snippets.

@ducakar
Last active March 15, 2020 15:29
Show Gist options
  • Save ducakar/4fda4a57396ac2dde3e7a60d49346a0c to your computer and use it in GitHub Desktop.
Save ducakar/4fda4a57396ac2dde3e7a60d49346a0c to your computer and use it in GitHub Desktop.
Aproksimacija širjenja koronavirusa v Sloveniji z eksponentno funkcijo
import math
# Aproksimacija z eksponentno funkcijo po metodi najmanjših kvadratov.
# https://mathworld.wolfram.com/LeastSquaresFittingExponential.html
def exp_fit(values):
points = [(i, y) for (i, y) in enumerate(values)]
xx = [x for (x, _) in points]
yy = [y for (_, y) in points]
n = len(values)
a = (sum([math.log(y) for y in yy]) * sum([x**2 for x in xx]) - sum(xx) * sum([x * math.log(y) for (x, y) in points])) / \
(n * sum(x**2 for x in xx) - sum(xx)**2)
b = (n * sum(x * math.log(y) for (x, y) in points) - sum(xx) * sum(math.log(y) for y in yy)) / \
(n * sum(x**2 for x in xx) - sum(xx)**2)
return math.exp(a), math.exp(b)
data = [5, 7, 12, 16, 23, 31, 57, 89, 141, 181, 219]
a, b = exp_fit(data)
print("{} * {}^n".format(a, b))
print()
print("dejansko :: eksponentno")
for i in range(len(data)):
y = a * b**i
print("{:8d} :: {:.1f}".format(data[i], y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment