Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Trabajo_grupal/WG1/Grupo_6.py
2714 views
1
####EJERCICIO 1
2
3
#Importamos la librería numpy
4
import numpy as np
5
6
#Como se utilizará una función utilizacom def
7
def fdiscontinuous():
8
9
#Creamos el vector, donde queremos entre 0 y 500 observaciones con rango 1
10
x=np.arange(0,500,1)
11
l=len(x)
12
y=l
13
#Creamos i para que el programa itere los valores del vector y asi los evalue segun cada condicion
14
for i in range(l):
15
if (x[i]>0 and x[i]<100):
16
print(y[i]==x[i]**1/2)
17
elif (x[i]>100 and x[i]<300):
18
print(y[i]==x[i]-5)
19
elif (x[i]>300 and x[i]<500):
20
print(y[i]==50)
21
#El vector aleatorio se desarrollará a continuación, donde se pide 20 observaciones aleatorias
22
vector=np.random.randint(0,500,20)
23
print (vector)
24
for range(vector) in fdiscontinuous():
25
print(range(vector))
26
27
28
29
30
######EJERCICIO 2
31
32
#Importamos los numpys, al igual que random para la realización del ejercicio
33
import numpy as np
34
import random
35
random.seed(7)
36
37
38
# In[170]:
39
40
41
#Creación del vector y matriz aleatorios*
42
#especificando un límite de 100 observaciones y de 2 cifras cada número.
43
44
45
# In[171]:
46
47
48
vector_2=np.random.randint(0, 100, size=(1, 100))
49
print(vector_2)
50
51
52
# In[172]:
53
54
55
matriz_2=np.random.randint(0, 100, size=(100, 50))
56
print(matriz_2)
57
58
59
# In[173]:
60
61
62
#Una vez creados, podremos hallar los valores máximo y mínimo de cada uno para la elaboración de la fórmula solicitada.
63
64
#Esto se creará a raíz de np.max(vector_2, axis = 1) y np.min(vector_2, axis = 1) para max y min, respectivamente.
65
66
67
# In[174]:
68
69
70
#Ahora podremos plantear la fórmula y reescribir los valores de la matriz solicitada. Esto servirá para Reescalar Cada elemento.
71
72
73
# In[189]:
74
75
76
for f in range(0, 100):
77
for g in range(100, 50):
78
escalar = (matriz_2[f][g]-np.max(vector_2, axis = 1))/((np.max(vector_2, axis = 1)-np.min(vector_2, axis = 1)))
79
if matriz_2[f][g] >= 101:
80
matriz_2[f][g] == 0
81
else:
82
matriz_2[f][g] = escalar
83
print(matriz_2)
84
85
86
87
88
####EJERCICIO 3
89
90
#Crear un proceso generador de datos con 5 variables y un tamaño de población de 10 mil observaciones.
91
#Crear un Loop que estime los coeficientes de un modelo de regresión lineal omitiendo una variable explicativa
92
#del proceso generador de datos. Asimismo, debe hallar el error estándar en cada iteración.
93
#Usar los siguientes tamaños de muestra en el Loop n : 10, 50, 80 , 120 , 200, 500,800,100, 5000.
94
#Comente sus resultados. Los resultados deben presentarse en una Dataframe.
95
#El nombre de las columnas serán (tamaño de muestra, coeficiente y error estándar ).
96
#Nótese que debe construir las columnas del coeficiente y error estándar para cada regresor x .
97
98
99
### para cuando el número de observaciones es 10000: n=10000
100
101
#Importamos las librerias necesarias para el programa
102
import random
103
import numpy as np
104
from scipy.stats import t #para t - student
105
import pandas as pd
106
107
#Generamos un proceso generador de datos con 5 variables y un error estandar, que posee 10 000 observaciones
108
x1 = np.random.rand(10000)
109
x2 = np.random.rand(10000)
110
x3 = np.random.rand(10000)
111
x4 = np.random.rand(10000)
112
x5 = np.random.rand(10000)
113
e = np.random.normal(0,1,10000) #El error estandar tiene distribucion normal
114
115
#funcion con las 5 variables y su término de error
116
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
117
118
#matriz de la funcion Y
119
X = np.column_stack((np.ones(10000),x1,x2,x3,x4,x5))
120
121
#coeficiientes estimados de la matriz X
122
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
123
124
#indexing aleatorios para la matriz X
125
random.sample( range(10000) , 10000 )
126
127
128
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
129
130
if standar and Pvalue and (instrumento is None) and (index is None) :
131
132
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
133
134
y_est = X @ beta
135
n = X.shape[0]
136
k = X.shape[1] - 1
137
nk = n - k
138
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
139
Var = sigma*np.linalg.inv(X.T @ X)
140
sd = np.sqrt( np.diag(Var) )
141
t_est = np.absolute(beta/sd)
142
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
143
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
144
145
146
elif (not instrumento is None) and (not index is None) :
147
148
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
149
150
index = index - 1
151
Z = X
152
Z[:,index] = z
153
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
154
x_est = Z @ beta_x
155
X[:,index] = x_est
156
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
157
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
158
159
return df
160
161
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
162
ols(X,Y)
163
164
165
166
### para cuando el número de observaciones es 10: n=10
167
168
import random
169
import numpy as np
170
from scipy.stats import t #para t - student
171
import pandas as pd
172
173
#se requiere una funcion con 5 variables aleatorias
174
x1 = np.random.rand(10)
175
x2 = np.random.rand(10)
176
x3 = np.random.rand(10)
177
x4 = np.random.rand(10)
178
x5 = np.random.rand(10)
179
e = np.random.normal(0,1,10)
180
181
#funcion con las 5 variables y su término de error
182
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
183
184
#matriz de la funcion Y
185
X = np.column_stack((np.ones(10),x1,x2,x3,x4,x5))
186
187
#coeficiientes estimados de la matriz X
188
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
189
190
#indexing aleatorios para la matriz X
191
random.sample( range(10) , 10 )
192
193
194
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
195
196
if standar and Pvalue and (instrumento is None) and (index is None) :
197
198
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
199
200
y_est = X @ beta
201
n = X.shape[0]
202
k = X.shape[1] - 1
203
nk = n - k
204
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
205
Var = sigma*np.linalg.inv(X.T @ X)
206
sd = np.sqrt( np.diag(Var) )
207
t_est = np.absolute(beta/sd)
208
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
209
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
210
211
212
elif (not instrumento is None) and (not index is None) :
213
214
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
215
216
index = index - 1
217
Z = X
218
Z[:,index] = z
219
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
220
x_est = Z @ beta_x
221
X[:,index] = x_est
222
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
223
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
224
225
return df
226
227
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
228
ols(X,Y)
229
230
231
232
### para cuando el número de observaciones es 50: n=50
233
234
import random
235
import numpy as np
236
from scipy.stats import t #para t - student
237
import pandas as pd
238
239
#se requiere una funcion con 5 variables aleatorias
240
x1 = np.random.rand(50)
241
x2 = np.random.rand(50)
242
x3 = np.random.rand(50)
243
x4 = np.random.rand(50)
244
x5 = np.random.rand(50)
245
e = np.random.normal(0,1,50)
246
247
#funcion con las 5 variables y su término de error
248
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
249
250
#matriz de la funcion Y
251
X = np.column_stack((np.ones(50),x1,x2,x3,x4,x5))
252
253
#coeficiientes estimados de la matriz X
254
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
255
256
#indexing aleatorios para la matriz X
257
random.sample( range(50) , 50 )
258
259
260
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
261
262
if standar and Pvalue and (instrumento is None) and (index is None) :
263
264
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
265
266
y_est = X @ beta
267
n = X.shape[0]
268
k = X.shape[1] - 1
269
nk = n - k
270
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
271
Var = sigma*np.linalg.inv(X.T @ X)
272
sd = np.sqrt( np.diag(Var) )
273
t_est = np.absolute(beta/sd)
274
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
275
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
276
277
278
elif (not instrumento is None) and (not index is None) :
279
280
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
281
282
index = index - 1
283
Z = X
284
Z[:,index] = z
285
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
286
x_est = Z @ beta_x
287
X[:,index] = x_est
288
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
289
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
290
291
return df
292
293
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
294
ols(X,Y)
295
296
297
298
### para cuando el número de observaciones es 80: n=80
299
300
import random
301
import numpy as np
302
from scipy.stats import t #para t - student
303
import pandas as pd
304
305
#se requiere una funcion con 5 variables aleatorias
306
x1 = np.random.rand(80)
307
x2 = np.random.rand(80)
308
x3 = np.random.rand(80)
309
x4 = np.random.rand(80)
310
x5 = np.random.rand(80)
311
e = np.random.normal(0,1,80)
312
313
#funcion con las 5 variables y su término de error
314
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
315
316
#matriz de la funcion Y
317
X = np.column_stack((np.ones(80),x1,x2,x3,x4,x5))
318
319
#coeficiientes estimados de la matriz X
320
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
321
322
#indexing aleatorios para la matriz X
323
random.sample( range(80) , 80 )
324
325
326
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
327
328
if standar and Pvalue and (instrumento is None) and (index is None) :
329
330
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
331
332
y_est = X @ beta
333
n = X.shape[0]
334
k = X.shape[1] - 1
335
nk = n - k
336
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
337
Var = sigma*np.linalg.inv(X.T @ X)
338
sd = np.sqrt( np.diag(Var) )
339
t_est = np.absolute(beta/sd)
340
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
341
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
342
343
344
elif (not instrumento is None) and (not index is None) :
345
346
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
347
348
index = index - 1
349
Z = X
350
Z[:,index] = z
351
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
352
x_est = Z @ beta_x
353
X[:,index] = x_est
354
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
355
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
356
357
return df
358
359
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
360
ols(X,Y)
361
362
363
364
### para cuando el número de observaciones es 120: n=120
365
366
import random
367
import numpy as np
368
from scipy.stats import t #para t - student
369
import pandas as pd
370
371
#se requiere una funcion con 5 variables aleatorias
372
x1 = np.random.rand(120)
373
x2 = np.random.rand(120)
374
x3 = np.random.rand(120)
375
x4 = np.random.rand(120)
376
x5 = np.random.rand(120)
377
e = np.random.normal(0,1,120)
378
379
#funcion con las 5 variables y su término de error
380
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
381
382
#matriz de la funcion Y
383
X = np.column_stack((np.ones(120),x1,x2,x3,x4,x5))
384
385
#coeficiientes estimados de la matriz X
386
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
387
388
#indexing aleatorios para la matriz X
389
random.sample( range(120) , 120 )
390
391
392
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
393
394
if standar and Pvalue and (instrumento is None) and (index is None) :
395
396
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
397
398
y_est = X @ beta
399
n = X.shape[0]
400
k = X.shape[1] - 1
401
nk = n - k
402
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
403
Var = sigma*np.linalg.inv(X.T @ X)
404
sd = np.sqrt( np.diag(Var) )
405
t_est = np.absolute(beta/sd)
406
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
407
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
408
409
410
elif (not instrumento is None) and (not index is None) :
411
412
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
413
414
index = index - 1
415
Z = X
416
Z[:,index] = z
417
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
418
x_est = Z @ beta_x
419
X[:,index] = x_est
420
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
421
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
422
423
return df
424
425
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
426
ols(X,Y)
427
428
429
430
### para cuando el número de observaciones es 200: n=200
431
432
import random
433
import numpy as np
434
from scipy.stats import t #para t - student
435
import pandas as pd
436
437
#se requiere una funcion con 5 variables aleatorias
438
x1 = np.random.rand(200)
439
x2 = np.random.rand(200)
440
x3 = np.random.rand(200)
441
x4 = np.random.rand(200)
442
x5 = np.random.rand(200)
443
e = np.random.normal(0,1,200)
444
445
#funcion con las 5 variables y su término de error
446
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
447
448
#matriz de la funcion Y
449
X = np.column_stack((np.ones(200),x1,x2,x3,x4,x5))
450
451
#coeficiientes estimados de la matriz X
452
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
453
454
#indexing aleatorios para la matriz X
455
random.sample( range(200) , 200 )
456
457
458
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
459
460
if standar and Pvalue and (instrumento is None) and (index is None) :
461
462
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
463
464
y_est = X @ beta
465
n = X.shape[0]
466
k = X.shape[1] - 1
467
nk = n - k
468
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
469
Var = sigma*np.linalg.inv(X.T @ X)
470
sd = np.sqrt( np.diag(Var) )
471
t_est = np.absolute(beta/sd)
472
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
473
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
474
475
476
elif (not instrumento is None) and (not index is None) :
477
478
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
479
480
index = index - 1
481
Z = X
482
Z[:,index] = z
483
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
484
x_est = Z @ beta_x
485
X[:,index] = x_est
486
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
487
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
488
489
return df
490
491
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
492
ols(X,Y)
493
494
495
496
### para cuando el número de observaciones es 500: n=500
497
498
import random
499
import numpy as np
500
from scipy.stats import t #para t - student
501
import pandas as pd
502
503
#se requiere una funcion con 5 variables aleatorias
504
x1 = np.random.rand(500)
505
x2 = np.random.rand(500)
506
x3 = np.random.rand(500)
507
x4 = np.random.rand(500)
508
x5 = np.random.rand(500)
509
e = np.random.normal(0,1,500)
510
511
#funcion con las 5 variables y su término de error
512
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
513
514
#matriz de la funcion Y
515
X = np.column_stack((np.ones(500),x1,x2,x3,x4,x5))
516
517
#coeficiientes estimados de la matriz X
518
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
519
520
#indexing aleatorios para la matriz X
521
random.sample( range(500) , 500 )
522
523
524
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
525
526
if standar and Pvalue and (instrumento is None) and (index is None) :
527
528
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
529
530
y_est = X @ beta
531
n = X.shape[0]
532
k = X.shape[1] - 1
533
nk = n - k
534
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
535
Var = sigma*np.linalg.inv(X.T @ X)
536
sd = np.sqrt( np.diag(Var) )
537
t_est = np.absolute(beta/sd)
538
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
539
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
540
541
542
elif (not instrumento is None) and (not index is None) :
543
544
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
545
546
index = index - 1
547
Z = X
548
Z[:,index] = z
549
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
550
x_est = Z @ beta_x
551
X[:,index] = x_est
552
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
553
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
554
555
return df
556
557
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
558
ols(X,Y)
559
560
561
562
### para cuando el número de observaciones es 800: n=800
563
564
import random
565
import numpy as np
566
from scipy.stats import t #para t - student
567
import pandas as pd
568
569
#se requiere una funcion con 5 variables aleatorias
570
x1 = np.random.rand(800)
571
x2 = np.random.rand(800)
572
x3 = np.random.rand(800)
573
x4 = np.random.rand(800)
574
x5 = np.random.rand(800)
575
e = np.random.normal(0,1,800)
576
577
#funcion con las 5 variables y su término de error
578
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
579
580
#matriz de la funcion Y
581
X = np.column_stack((np.ones(800),x1,x2,x3,x4,x5))
582
583
#coeficiientes estimados de la matriz X
584
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
585
586
#indexing aleatorios para la matriz X
587
random.sample( range(800) , 800 )
588
589
590
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
591
592
if standar and Pvalue and (instrumento is None) and (index is None) :
593
594
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
595
596
y_est = X @ beta
597
n = X.shape[0]
598
k = X.shape[1] - 1
599
nk = n - k
600
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
601
Var = sigma*np.linalg.inv(X.T @ X)
602
sd = np.sqrt( np.diag(Var) )
603
t_est = np.absolute(beta/sd)
604
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
605
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
606
607
608
elif (not instrumento is None) and (not index is None) :
609
610
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
611
612
index = index - 1
613
Z = X
614
Z[:,index] = z
615
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
616
x_est = Z @ beta_x
617
X[:,index] = x_est
618
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
619
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
620
621
return df
622
623
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
624
ols(X,Y)
625
626
627
628
### para cuando el número de observaciones es 100: n=100
629
630
import random
631
import numpy as np
632
from scipy.stats import t #para t - student
633
import pandas as pd
634
635
#se requiere una funcion con 5 variables aleatorias
636
x1 = np.random.rand(100)
637
x2 = np.random.rand(100)
638
x3 = np.random.rand(100)
639
x4 = np.random.rand(100)
640
x5 = np.random.rand(100)
641
e = np.random.normal(0,1,100)
642
643
#funcion con las 5 variables y su término de error
644
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
645
646
#matriz de la funcion Y
647
X = np.column_stack((np.ones(100),x1,x2,x3,x4,x5))
648
649
#coeficiientes estimados de la matriz X
650
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
651
652
#indexing aleatorios para la matriz X
653
random.sample( range(100) , 100 )
654
655
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
656
657
if standar and Pvalue and (instrumento is None) and (index is None) :
658
659
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
660
661
y_est = X @ beta
662
n = X.shape[0]
663
k = X.shape[1] - 1
664
nk = n - k
665
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
666
Var = sigma*np.linalg.inv(X.T @ X)
667
sd = np.sqrt( np.diag(Var) )
668
t_est = np.absolute(beta/sd)
669
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
670
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
671
672
673
elif (not instrumento is None) and (not index is None) :
674
675
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
676
677
index = index - 1
678
Z = X
679
Z[:,index] = z
680
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
681
x_est = Z @ beta_x
682
X[:,index] = x_est
683
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
684
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
685
686
return df
687
688
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
689
ols(X,Y)
690
691
692
693
### para cuando el número de observaciones es 5000: n=5000
694
695
import random
696
import numpy as np
697
from scipy.stats import t #para t - student
698
import pandas as pd
699
700
#se requiere una funcion con 5 variables aleatorias
701
x1 = np.random.rand(5000)
702
x2 = np.random.rand(5000)
703
x3 = np.random.rand(5000)
704
x4 = np.random.rand(5000)
705
x5 = np.random.rand(5000)
706
e = np.random.normal(0,1,5000)
707
708
#funcion con las 5 variables y su término de error
709
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
710
711
#matriz de la funcion Y
712
X = np.column_stack((np.ones(5000),x1,x2,x3,x4,x5))
713
714
#coeficiientes estimados de la matriz X
715
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
716
717
#indexing aleatorios para la matriz X
718
random.sample( range(5000) , 5000 )
719
720
721
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
722
723
if standar and Pvalue and (instrumento is None) and (index is None) :
724
725
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
726
727
y_est = X @ beta
728
n = X.shape[0]
729
k = X.shape[1] - 1
730
nk = n - k
731
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
732
Var = sigma*np.linalg.inv(X.T @ X)
733
sd = np.sqrt( np.diag(Var) )
734
t_est = np.absolute(beta/sd)
735
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
736
df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )
737
738
739
elif (not instrumento is None) and (not index is None) :
740
741
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
742
743
index = index - 1
744
Z = X
745
Z[:,index] = z
746
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
747
x_est = Z @ beta_x
748
X[:,index] = x_est
749
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
750
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
751
752
return df
753
754
#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar
755
ols(X,Y)
756
757
758
759
###### Fin del ejrecicio 3 ###### Fin del ejrecicio 3 ######
760
761
762
763
764
765
766
######EJERCICIO 4
767
768
import numpy as np
769
import match
770
import pandas
771
772
random.seed(175)
773
#Creando un proceso generador de datos con 8 variables (intercepto y 7 explicativas)
774
#Necesitamos 800 observaciones, por ello generaremos 800 números enteros.
775
776
#Entonces se tiene a continuación las siguientes 7 variables explicativas con distribución normal
777
X1 = np.random.rand(800)
778
X2 = np.random.rand(800)
779
X3 = np.random.rand(800)
780
X4 = np.random.rand(800)
781
X5 = np.random.rand(800)
782
X6 = np.random.rand(800)
783
X7 = np.random.rand(800)
784
785
#Seguido a ello, la creación del error bajo distribucion normal
786
u = np.random.normal(0, 1, 800)
787
788
#El modelo a estimar que tendremos es Y = b0 + b1*X1 + b2*X2 + b3*X3 + b4*X4 + b5*X5 + b6*X6 + b7*X7 +u
789
Y = 1 + 0.6*X1 + 0.5*X2 + 1.2*X3 + 2.5*X4 + 0.4*X5 + 0.8*X6 + 1.6*X7 +u
790
#Ahora, se juntarán todos los vectores generados anteriormente en una matriz
791
792
matriz = np.column_stack((np.ones(800), X1, X2, X3, X4, X5, X6, X7))
793
print (matriz)
794
795
#Ahora, podemos hallar los betas o también llamados coeficientes estimados, mediante una transposicion de la matriz
796
betas = np.linalg.inv(matriz.T @ matriz) @ ((matriz.T)@ Y)
797
print (betas)
798
799
#Ahora ya se tiene un vector que contiene a los betas o coeficientes
800
#Entonces, a continuación se estimará los errores estándar de cada beta
801
802
def statistics.stdev(betas)
803
betas = betas**1/2
804
print (betas)
805
806
807
808
809
810