Tengo las ventas de artículos desde enero de 2013 hasta octubre de 2015. Sólo quiero predecir las ventas totales para el próximo mes. Sólo por el bien de aprender, me gustaría transformarlo en un modelo de regresión múltiple codificado desde cero, sin ninguna biblioteca. Hasta ahora, he sido capaz de obtener las betas pero no sé cómo obtener la predicción para el próximo mes.
Aquí están los datos históricos de las ventas mensuales desde enero de 2013 hasta octubre de 2015, ts
:
date_block_num
0 131479.0
1 128090.0
2 147142.0
3 107190.0
4 106970.0
5 125381.0
6 116966.0
7 125291.0
8 133332.0
9 127541.0
10 130009.0
11 183342.0
12 116899.0
13 109687.0
14 115297.0
15 96556.0
16 97790.0
17 97429.0
18 91280.0
19 102721.0
20 99208.0
21 107422.0
22 117845.0
23 168755.0
24 110971.0
25 84198.0
26 82014.0
27 77827.0
28 72295.0
29 64114.0
30 63187.0
31 66079.0
32 72843.0
33 71056.0
Intenté hacer una simple regresión lineal:
Primero traté de estimar alpha y beta y luego usar predict(alpha,beta,34)
. Así que lo hice:
import random
def predict(alpha, beta, x_i):
return alpha+ beta * x_i
def error(alpha, beta, x_i, y_i):
"""the error from predicting beta * x_i + alpha
when the actual value is y_i"""
return y_i - predict(alpha, beta, x_i)
def sum_of_squarred_errors(alpha, beta, x, y):
return sum(errors(alpha, beta, x_i, y_i)**2
for x_i, y_i in zip(x,y))
def correlation(x,y):
stdev_x = standard_deviation(x)
stdev_y = standard_deviation(y)
if stdev_x > 0 and stdev_y >0:
return covariance(x,y)/ stdev_x/ stdev_y
else:
return 0
def squared_error(x_i, y_i, theta):
alpha, beta = theta
return error(alpha, beta, x_i, y_i) ** 2
def squared_error_gradient(x_i, y_i, theta):
alpha, beta = theta
return [-2 * error(alpha, beta, x_i, y_i),
-2 * error(alpha, beta, x_i, y_i) * x_i]
def in_random_order(data):
"""generator that returns the elements if data in random order"""
indexes = [i for i, _ in enumerate(data)] # create a list of indexes
random.shuffle(indexes) # suffle them
for i in indexes:
yield data[i]
def scalar_multiply(c, v):
"""c is a number, v is a vector"""
return [c*v_i for v_i in v]
def minimize_stochastic(target_fn, gradient_fn, x,y, theta_0, alpha_0=0.01):
print("x: ", x, "\ny: ",y.tolist())
data = zip(x,y)
theta = theta_0 #initial guess
alpha = alpha_0 # initial step size
min_theta, min_value = None, float('inf') # the minimum so far
iterations_with_no_improvment = 0
# if we ever go 100 iterations with no improvment, stop
while iterations_with_no_improvment < 100:
value = sum(target_fn(x_i, y_i, theta) for x_i, y_i in data)
# print("value: ", value)
if value < min_value:
# if we've found a new minimum, remember it
# and go back to the original step size
min_theta, min_value = theta, value
iterations_with_no_improvment = 0
alpha = alpha_0
else:
# otherwise we're not improving, so try shrinking the step size
iterations_with_no_improvment +=1
alpha *=0.9
# and take a gradient step for each of the data points
for x_i, y_i in in_random_order(data):
gradient_i = gradient_fn(x_i, y_i, theta)
theta = vector_substract(theta, scalar_multiply(alpha, gradient_i))
return min_theta
# choose random value to start
random.seed(0)
theta = [random.random(), random.random()]
alpha, beta = minimize_stochastic(squared_error,
squared_error_gradient, ts.index.values,
ts.values,
theta,
0.001)
print("alpha: ", alpha, "beta: ", beta)
Pero tiene alfas y betas súper bajas:
alpha: 0.8444218515250481 beta: 0.7579544029403025
Así que el total de ventas para 34 (noviembre de 2015) son: 26.614871551495334 que parece imposible comparado con 33 (octubre 2015): 71056.0
Entonces, ¿me equivoqué con el algoritmo de regresión lineal? Mi suposición es que mis valores aleatorios para empezar son tal vez demasiado bajos:
theta = [random.random(), random.random()]
Sin embargo, deberían aumentar de todos modos hasta que no haya más entradas, ¿no es así?
Entonces, ¿cómo elegir los thetas iniciales para una simple regresión lineal?