pberkes / big_O

Python module to estimate big-O time complexity from execution time
BSD 3-Clause "New" or "Revised" License
322 stars 50 forks source link

Polynomial.compute() and Exponential.compute() Return Incorrect Results #43

Closed TheMatt2 closed 2 years ago

TheMatt2 commented 2 years ago

Description

big_o.complexities.Polynomial.compute() and big_o.complexities.Exponential.compute() consistently return unreasonable values.

This issue is present both when the class is used by itself, and when it is returned by big_o.big_o()

Steps to reproduce:

Run the following example code to test the Polynomial class:

import big_o
import numpy as np

polynomial_complexity = big_o.complexities.Polynomial()
n = np.array(range(1,11)) # array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

t = 3.7*n**4.5 
# array([3.70000000e+00, 8.37214429e+01, 5.19095627e+02, 1.89440000e+03,
#        5.17090720e+03, 1.17457932e+04, 2.35040609e+04, 4.28653788e+04,
#        7.28271000e+04, 1.17004273e+05])

polynomial_complexity.fit(n, t) # 2.753078336902666e-29

t_computed = polynomial_complexity.compute(n)
# array([ 1.30833282,  4.42749513,  6.25208812,  7.54665744,  8.55080343,
#         9.37125043, 10.06492849, 10.66581976, 11.19584342, 11.66996574])

Observe t_computed is very different from the values t

The issue can be plotted as follows:

import matplotlib.pyplot as plt
plt.plot(n, t, "-", n, t_computed, "--")
plt.show()

polynomial

In blue is t, the original function. In orange is t_computed. Observe that, while t is consistent with a polynomial function, the t_computed line is negative and roughly linear/constant. The t_computed line is not consistent with a polynomial.

Expected Behavior:

Result of big_o.complexities.Polynomial() and big_o.complexities.Exponential() should resemble a polynomial/exponential function that matches the fitted data.