I am using Python 3.8
Basically, given a set of x and y values, I wrote something that gives me an approximation using linear regression but for exponential models. I am wondering because the expected output of 2 1
is 100% accurate which I made to be on purpose. I chose powers of 2. The first version of the program gives me a like 99.99999% accurate answer, but the second one gives me a 100% accurate answer. Could anyone tell me why this is? Also, I am wondering if log2 is generally more accurate to use than log10, or if this is just a "fluke" due to the specific values I had in points
Also, I looked at the "Similar questions" box and nothing came up that seemed similar to my question.
Version 1:
from math import log10, exp
from decimal import Decimal
points = {'1': '2', '2': '4', '5': '32', '6': '64'}
t = Decimal(str(len(points)))
p = Decimal('0')
q = Decimal('0')
r = Decimal('0')
s = Decimal('0')
for key, value in points.items():
p += Decimal(key)
q += Decimal(key) * Decimal(key)
r += Decimal(log10(Decimal(value)))
s += Decimal(key) * Decimal(log10(Decimal(value)))
a = (t * s - p * r) / (t * q - p * p)
b = (r - a * p) / t
a = Decimal(Decimal('10') ** a)
b = Decimal(Decimal('10') ** b)
print(a, b)
This is the result I got:
1.999999999999999960280230758 1.000000000000000060150187915
Version 2:
from math import log2
from decimal import Decimal
points = {'1': '2', '2': '4', '5': '32', '6': '64'}
t = Decimal(str(len(points)))
p = Decimal('0')
q = Decimal('0')
r = Decimal('0')
s = Decimal('0')
for key, value in points.items():
p += Decimal(key)
q += Decimal(key) * Decimal(key)
r += Decimal(log2(Decimal(value)))
s += Decimal(key) * Decimal(log2(Decimal(value)))
a = (t * s - p * r) / (t * q - p * p)
b = (r - a * p) / t
a = Decimal(Decimal('2') ** a)
b = Decimal(Decimal('2') ** b)
print(a, b)
This is the result I got:
2 1
I tried another test with values that are neither powers of 10 or 2, but I don't know which one is more accurate:
from math import log2, log10
from decimal import Decimal
points = {'1': '3', '2': '7', '5': '26', '6': '62'}
t = Decimal(str(len(points)))
p = Decimal('0')
q = Decimal('0')
r = Decimal('0')
s = Decimal('0')
for key, value in points.items():
p += Decimal(key)
q += Decimal(key) * Decimal(key)
r += Decimal(log2(Decimal(value)))
s += Decimal(key) * Decimal(log2(Decimal(value)))
a = (t * s - p * r) / (t * q - p * p)
b = (r - a * p) / t
a = Decimal(Decimal('2') ** a)
b = Decimal(Decimal('2') ** b)
print(a, b)
points = {'1': '3', '2': '7', '5': '26', '6': '62'}
t = Decimal(str(len(points)))
p = Decimal('0')
q = Decimal('0')
r = Decimal('0')
s = Decimal('0')
for key, value in points.items():
p += Decimal(key)
q += Decimal(key) * Decimal(key)
r += Decimal(log10(Decimal(value)))
s += Decimal(key) * Decimal(log10(Decimal(value)))
a = (t * s - p * r) / (t * q - p * p)
b = (r - a * p) / t
a = Decimal(Decimal('10') ** a)
b = Decimal(Decimal('10') ** b)
print(a, b)
My result is:
1.752690522119211309696792535 1.902951630457180834208048103
1.752690522119211322362489659 1.902951630457180751134992738