Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
166 views
ubuntu2004
Kernel: Python 3 (system-wide)
import numpy as np import matplotlib.pyplot as plt import math from poly import *

Regresión

Por minimos cuadrados

Se=āˆ‘i=0n[P(xi)āˆ’yi]2=ei2S_e = \sum^{n}_{i=0} [P(x_i) - y_i]^2 = e_i^2

Regresión Lineal

y=P(x)=a0+a1x⇒Se=āˆ‘i=0n[a0+a1xiāˆ’yi]2=ei2y = P(x) = a_0 + a_1x \\ \Rightarrow S_e = \sum^{n}_{i=0} [a_0 + a_1x_i - y_i]^2 = e_i^2

Obteniendo las siguientes derivadas parciales e igualandolas a cero:

āˆ‚Seāˆ‚a0=2āˆ‘[(a0+a1xi)āˆ’yi]=0\frac{\partial S_e}{\partial a_0} = 2 \sum [(a_0 +a_1x_i) - y_i] = 0āˆ‚Seāˆ‚a1=2āˆ‘[(a0+a1xi)āˆ’yi](xi)=0\frac{\partial S_e}{\partial a_1} = 2 \sum [(a_0 +a_1x_i) - y_i] (x_i) = 0ā‡’āˆ‘[(a0+a1xi)āˆ’yi]=0āˆ‘[(a0+a1xi)āˆ’yi](xi)=0\Rightarrow \sum [(a_0 +a_1x_i) - y_i] = 0 \\ \sum [(a_0 +a_1x_i) - y_i] (x_i) = 0
#Calcular los coeficientes de la recta de Regresión def CoefsLineal(x,y,n): a = np.zeros(2) p = np.sum(x) q = np.sum(y) r = np.sum(x*x) s = np.sum(x*y) det = (n+1)*r - p*p a[0] = (q*r - p*s) / det a[1] = ((n+1)*s - p*q) / det return a
#Evaluar un punto en la recta def RegLineal(a,t): pt = a[0] + a[1]*t return pt
#ejemplo 1 x = np.arange(1,11, dtype=float) #genera puntos del 1 al 10 y = np.array([1.3,3.5,4.2,5.0,7.0,8.8,10.1,12.5,13.0,15.6], dtype=float) n = len(x)-1
#Calcular los coeficientes a = CoefsLineal(x,y,n) #Evaluar un punto t = 11 pt = RegLineal(a,t) pt
16.56
#Evaluar un conjunto de puntos t = x pt = RegLineal(a,t) pt
array([ 1.17818182, 2.71636364, 4.25454545, 5.79272727, 7.33090909, 8.86909091, 10.40727273, 11.94545455, 13.48363636, 15.02181818])
#Graficar los nodos plt.plot(x,y,"or") #Graficar recta de regresion ts = np.linspace(np.min(x), np.max(x), 100) Pts = RegLineal(a,ts) plt.plot(ts,Pts, "b")
[<matplotlib.lines.Line2D at 0x7fdcca5a5850>]
Image in a Jupyter notebook
#Predecir t = 11 t = 11 pt = RegLineal(a,t) pt
16.56
tabla = np.loadtxt('temp_puebla.txt') x = tabla[:,0] y = tabla[:,1] n = len(x) - 1 a = CoefsLineal(x,y,n) print(a) #Graficar los nodos plt.plot(x,y,"or") #Graficar recta de regresion ts = np.linspace(np.min(x), np.max(x), 100) Pts = RegLineal(a,ts) plt.plot(ts,Pts, "b")
[-1.11113369e+02 6.37614170e-02]
[<matplotlib.lines.Line2D at 0x7fdcca4f7f70>]
Image in a Jupyter notebook
#Predecir t = 11 t = 2060 pt = RegLineal(a,t) pt
20.235150192960518
#grafica del 2015-2023 x = np.arange(2015,2023, dtype=float) y = np.array([9,9,11,8,6,4,6,6], dtype=float) n = len(x) -1 a = CoefsLineal(x,y,n) print(a) #Graficar los nodos plt.plot(x,y,"or") #Graficar recta de regresion ts = np.linspace(np.min(x), np.max(x), 100) Pts = RegLineal(a,ts) plt.plot(ts,Pts, "b")
[ 1.42513095e+03 -7.02380952e-01]
[<matplotlib.lines.Line2D at 0x7fdcca47ffa0>]
Image in a Jupyter notebook
t = 2030 pt = RegLineal(a,t) pt
-0.7023809523809632

Regresion cuadratica

#Algoritmo para crear la matriz C def matrizC(x,n): c=np.ones((n+1,3)) for i in range(1,3): c[::,i]=x*c[::,i-1] return c
#ejemplo para obtener la matriz c x=np.array([0,2,3,5],dtype=float) y=np.array([-1,0,2,1],dtype=float) n=len(x)-1 c=matrizC(x,n) c
array([[ 1., 0., 0.], [ 1., 2., 4.], [ 1., 3., 9.], [ 1., 5., 25.]])
#algoritmo para calcular los coeficientes de P2(x) de regresion def coefsCuad(x,y,n): c=matrizC(x,n) ct=np.transpose(c) B=np.dot(ct, c) #crear sistema z=np.dot(ct, y) a=np.linalg.solve(B,z) #resolver el sistema return a
#obtener vector de coeficientes a=coefsCuad(x,y,n) a
array([-1.15384615, 1.29487179, -0.16666667])
#escribir polinomio n=2 escPol("p", a, n) #evaluar un punto en el polinomio de regresion lineal cuadratico con ruffini t=2 Ruffini(a, n, t) #graficar el polinimio ts=np.linspace(np.min(x), np.max(x),1000) pts=Ruffini(a, n, ts) plt.plot(ts,pts) plt.plot(x,y,"o")
p(x) = -1.15+1.29 x -0.17 x^2
[<matplotlib.lines.Line2D at 0x7fdcc82b5580>]
Image in a Jupyter notebook