-
Notifications
You must be signed in to change notification settings - Fork 1
/
iel-martin-linea-recta.py
81 lines (52 loc) · 1.05 KB
/
iel-martin-linea-recta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 11 20:13:53 2020
@author: José-Manuel Martin Coronado (IEL)
"""
"1) Ecuación:Y = a+b*X para n y para N"
n = 30
N = 100
a = 10
b = 4
import numpy as np
x1 = np.array(list(range(1,n+1,1)))
y = a+b*x1
'2) Gráfico'
import matplotlib.pyplot as plt
plt.plot(x1,y)
plt.ylabel('Variable Y')
plt.xlabel('Variable X')
#plt.show()
'3) Perturbación aleatoria'
ey = sum(y)/n
sdy = (sum((y-ey)**2)/(n-1))**0.5
e = np.random.randint(-sdy/2,sdy/2,n)
y2 = y+e
plt.plot(x1,y2)
plt.ylabel('Variable Y2')
plt.xlabel('Variable X')
plt.plot(x1,y)
'3) Regresión lineal (matricial)'
x1.shape = (n,1)
y.shape = (n,1)
y2.shape = (n,1)
i = np.full((1,n),1)
type(i)
i.shape = (n,1)
i
x = np.column_stack([i,x1])
type(x)
x.shape
print(x)
xt = np.transpose(x)
xt
xx = xt@x
xx
invxx = np.linalg.inv(xx)
betas = invxx@xt@y2
y3 = betas[0]+betas[1]*x1
plt.plot(x1,y2)
plt.ylabel('Variable Y2')
plt.xlabel('Variable X')
plt.plot(x1,y)
plt.plot(x1,y3)