Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Trabajo_grupal/WG1/Grupo_5_py.py
2714 views
1
import random # Primero importamos las bibliotecas pertinentes
2
import numpy as np
3
import math
4
5
6
# %% EJERCICIO NUMERO 1
7
8
np.random.seed(188) # Luego, definimos la random seed 188
9
# Posteriormente, creamos x que será un vector 1x20 con valores entre 0 y 500
10
x = np.random.randint(0, 500, size=20)
11
print(x) # Comprobamos la matriz x
12
13
y = [] # Ahora, para el punto 1.1, definimos el vector y que tendrá como componentes a los valores de x
14
print(type(y))
15
for i in x:
16
if i > 0 and i <= 100: # Si i entre 0 y 100, el componente 1xi adoptará el valor de i^0.5
17
y.append(i**0.5)
18
else:
19
y.append(i**0) # Caso contrario, se computará el valor de 1
20
21
print(y) # Comprobamos la matriz y
22
23
z = [] # Ahora, para el punto 1.2, definimos el vector z que tendrá como componentes a los valores de x
24
for i in x:
25
if i > 100 and i <= 300: # Si i entre 100 y 300, el componente 1xi adoptará el valor de i-5
26
z.append(i-5)
27
else:
28
z.append(i**0) # Caso contrario, se computará el valor de 1
29
30
print(z) # Comprobamos la matriz z
31
32
k = [] # Ahora, para el punto 1.3, definimos el vector k que tendrá como componentes a los valores de x
33
for i in x:
34
if i > 300 and i <= 500: # Si i entre 300 y 500, el componente 1xi adoptará el valor de 50
35
k.append(50)
36
else:
37
k.append(i**0) # Caso contrario, se computará el valor de 1
38
39
print(k) # Comprobamos la matriz z
40
41
42
43
# %% EJERCICIO 2
44
45
np.random.seed(100) # Definimos la random seed 100
46
47
# Creamos el vector 1x100 llamada v1
48
v1 = np.random.randint(0, 500, size=(100))
49
50
51
# Creamos la matriz 100x50 llamada M1
52
m1 = np.random.randint(0, 500, size=(100, 50))
53
54
55
def escalamiento(variable):
56
57
try:
58
if not isinstance(variable, np.ndarray):
59
raise TypeError("ERROR, NO ESTÁ BIEN EL TIPO DE VARIABLE")
60
61
colum = variable.shape[1]
62
63
except IndexError:
64
65
# SOLO PARA VECTOR
66
minimo = min(variable)
67
maximo = max(variable)
68
resultado = []
69
70
for i in range(variable.shape[0]):
71
escalar = ((variable[i]-minimo)/maximo-minimo)
72
resultado.append(escalar)
73
74
print(resultado)
75
76
else:
77
78
l = []
79
lista_nuevo = []
80
lista_n1 = []
81
for i in range(variable.shape[0]):
82
for j in range(variable.shape[1]):
83
l.append(variable[i][j])
84
85
maximo = max(l)
86
minimo = min(l)
87
for k in range(variable.shape[1]):
88
escalar = (((variable[i][k])-minimo)/(maximo-minimo))
89
lista_nuevo.append(escalar)
90
91
lista_n1.append(lista_nuevo)
92
lista_nuevo = []
93
l = []
94
95
print(lista_n1)
96
97
98
escalamiento(m1)
99
100
101
102
# %% EJERCICIO 3
103
104
import random
105
import numpy as np
106
107
from scipy.stats import t # t - student
108
import pandas as pd
109
110
np.random.seed(175)
111
112
x1 = np.random.rand(10000) # uniform distribution [0,1]
113
x2 = np.random.rand(10000) # uniform distribution [0,1]
114
x3 = np.random.rand(10000) # uniform distribution [0,1]
115
x4 = np.random.rand(10000) # uniform distribution [0,1]
116
x5 = np.random.rand(10000) #uniform distribution [0,1]
117
e = np.random.normal(0,1,10000) # normal distribution mean = 0 and sd = 1
118
z = np.random.rand(10000)
119
# Poblacional regression (Data Generating Process GDP)
120
121
122
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 2.2*x5 + e
123
124
X = np.column_stack((np.ones(10000),x1,x2,x3,x4,x5))
125
126
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
127
128
if standar and Pvalue and (instrumento is None) and (index is None) :
129
130
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
131
132
y_est = X @ beta
133
n = X.shape[0]
134
k = X.shape[1] - 1
135
nk = n - k
136
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
137
Var = sigma*np.linalg.inv(X.T @ X)
138
sd = np.sqrt( np.diag(Var) )
139
t_est = np.absolute(beta/sd)
140
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
141
df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,
142
"Pvalue" : pvalue} )
143
144
145
elif (not instrumento is None) and (not index is None) :
146
147
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
148
149
index = index - 1
150
Z = X
151
Z[:,index] = z
152
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
153
x_est = Z @ beta_x
154
X[:,index] = x_est
155
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
156
df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})
157
158
return df
159
160
ols(X,Y)
161
## n=50
162
163
164
165
# %% EJERCICIO 4
166
167
import random
168
import numpy as np
169
170
from scipy.stats import t # t - student
171
import pandas as pd
172
173
np.random.seed(175)
174
175
x1 = np.random.rand(800) # uniform distribution [0,1]
176
x2 = np.random.rand(800) # uniform distribution [0,1]
177
x3 = np.random.rand(800) # uniform distribution [0,1]
178
x4 = np.random.rand(800) # uniform distribution [0,1]
179
x5 = np.random.rand(800) #uniform distribution [0,1]
180
x6 = np.random.rand(800) #uniform distribution [0,1]
181
x7= np.random.rand(800)
182
e = np.random.normal(0,1,800) # normal distribution mean = 0 and sd = 1
183
z = np.random.rand(800)
184
# Poblacional regression (Data Generating Process GDP)
185
186
187
Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 2.2*x5 + 3.5*x6 + 4.2*x7 + e
188
189
X = np.column_stack((np.ones(800),x1,x2,x3,x4,x5,x6,x7))
190
191
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
192
193
if standar and Pvalue and (instrumento is None) and (index is None) :
194
195
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
196
197
y_est = X @ beta
198
n = X.shape[0]
199
k = X.shape[1] - 1
200
nk = n - k
201
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
202
Var = sigma*np.linalg.inv(X.T @ X)
203
sd = np.sqrt( np.diag(Var) )
204
t_est = np.absolute(beta/sd)
205
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
206
df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,
207
"Pvalue" : pvalue} )
208
209
210
elif (not instrumento is None) and (not index is None) :
211
212
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
213
214
index = index - 1
215
Z = X
216
Z[:,index] = z
217
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
218
x_est = Z @ beta_x
219
X[:,index] = x_est
220
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
221
df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})
222
223
return df
224
225
ols(X,Y)
226
##Limite inferior = beta_estimado - 1.96 * error estandar
227
#Limite superior = beta_estimado + 1.96*error estandar
228
229
230
231
232
233
234
235
236
237
238
239
240