Created
May 12, 2017 02:50
-
-
Save baruch-grs/3a69110d9470301def5858ed43c02dd3 to your computer and use it in GitHub Desktop.
Método para calcular los mínimos cuadrados
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class LeastSquares(object): | |
"""docstring for LeastSquares""" | |
def __init__(self, x:list, y:list): | |
super(LeastSquares, self).__init__() | |
if len(x) != len(y): | |
raise NameError('Las listas deben tener misma longitud.') | |
self.__x = x | |
self.__y = y | |
self.__periodic_list = [] | |
self.__n = len(self.__x) | |
self.set_periodic_list() | |
def get_sum_x(self): | |
return sum(self.__x) | |
def get_sum_y(self): | |
return sum(self.__y) | |
def get_x_average(self): | |
return math.ceil(self.get_sum_x() / len(self.__x)) | |
def get_y_average(self): | |
return math.ceil(self.get_sum_y() / len(self.__y)) | |
def get_sum_x_pow(self): | |
auxiliar_list = [] | |
count = 0 | |
for day in self.__x: | |
auxiliar_list.append(self.__x[count]**2) | |
count += 1 | |
return sum(auxiliar_list) | |
def get_sum_y_pow(self): | |
auxiliar_list = [] | |
count = 0 | |
for element in self.__y: | |
auxiliar_list.append(self.__y[count]**2) | |
count += 1 | |
return sum(auxiliar_list) | |
def get_sum_x_y_prod(self): | |
count = 0 | |
auxiliar_list = [] | |
for item in self.__x: | |
auxiliar_list.append(self.__x[count]*self.__y[count]) | |
count += 1 | |
return sum(auxiliar_list) | |
def set_periodic_list(self): | |
difference_list = [] | |
count = 0 | |
is_periodic = True | |
for _ in self.__x: | |
if count != 0: | |
difference_list.append(self.__x[count] - self.__x[count - 1]) | |
count += 1 | |
count = 0 | |
for item in difference_list: | |
if count != 0: | |
if difference_list[count] != difference_list[count-1]: | |
is_periodic = False | |
break | |
count += 1 | |
if is_periodic: | |
count = 0 | |
periodic_value = difference_list[0] | |
for _ in self.__x: | |
self.__periodic_list.append(self.__x[len(self.__x) - 1] + periodic_value * (count + 1)) | |
count += 1 | |
else: | |
raise NameError('Tu lista de Periodo no es continua') | |
def get_A(self): | |
return math.ceil(self.get_y_average() - self.get_B() * self.get_x_average()) | |
def get_B(self): | |
return math.ceil((self.get_sum_x_y_prod() - (self.get_sum_x() * self.get_sum_y() / self.__n) ) / (self.get_sum_x_pow() - (self.get_sum_x() ** 2) / self.__n )) | |
def get_forecast(self): | |
forecast_list = [] | |
count = 0 | |
for item in self.__x: | |
forecast_list.append(self.get_A() + self.get_B() * self.__periodic_list[count]) | |
count += 1 | |
return forecast_list | |
def main(): | |
lista_x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | |
lista_y = [90, 106, 152, 244, 302, 274, 162, 194, 312, 359, 215, 126] | |
xd = LeastSquares(lista_x, lista_y) | |
print('Suma de x:\t\t', xd.get_sum_x()) | |
print('Suma de y:\t\t', xd.get_sum_y()) | |
print('Suma de x al cuadrado:\t', xd.get_sum_x_pow()) | |
print('Promedio de x:\t\t', xd.get_x_average()) | |
print('Promedio de y:\t\t', xd.get_y_average()) | |
print('Suma de y al cuadrado:\t', xd.get_sum_y_pow()) | |
print('Suma del producto del X y Y:\t', xd.get_sum_x_y_prod()) | |
print('*'*50) | |
print('A:\t\t', xd.get_A()) | |
print('B:\t\t', xd.get_B()) | |
print('Pronostico:\t', xd.get_forecast()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment