Path: blob/main/Trabajo_grupal/WG1/Grupo_8_py.Py
2714 views
#%% Pregunta 112#Creamos un vector cuyos datos estén entre 0 y 500 y contenga 20 datos.34import random5import numpy as np6import math78x = np.random.randint(0, 500, 20)910# Elaboramos una estructura If statement para la siguiente función11# y aplicamos condición a cada uno de los elementos1213def calculator(x):14x = x1516if 0<=x<100:17return f"F(X)= {x **(1/2)}"18elif 100<=x<300:19return f"F(X)={x-5}"20elif 300<=x:21return print( "F(X)=50")2223print(calculator(300))242526#%%Pregunta 22728import numpy as np29# creamos un vector "v" de 100 observaciones30v = np.arange (0,100)31print(v)3233np.min(v) #para ir observando el mínimo de dicho vector34np.max(v)3536#creamos una matriz "M"37M = np.arange(0,5000).reshape (100, 50) #esta tiene valores del 0 al 4999 y se utiliza el "reshape" para que sea una matriz de 100x5038print(M)39M.shape #comprobamos que M sea una matriz de 100x5040type(v)41type(M) #observamos que es numpy.ndarray4243print(np.min(M, axis=0))44print(np.max(M, axis=0))45#para reescalar los datos de las columnas de la matriz, queremos ver sus mínimos y máximos por columna46X = np.min(M, axis=0) #axis=0 pq queremos observar por columnas47Y = np.max(M, axis=0)48print(X)49X.shape5051#reescalamos vector y matriz52try:5354print((v-min(v))/max(v)-min(v)) # No corre el código si detecta un error5556except TypeError:57print("El argumento deberia ser un vector")5859try:6061print((M-X)/Y-X) # No corre el código si detecta un error6263except TypeError:64print("El argumento deberia ser una matriz")6566#%%Pregunta 36768#Importamos las bases requeridas:69import random70import pandas as pd71import numpy as np7273#Establecemos una base de datos que no cambia para cada tamaño de muestra74random.seed(100000)75random.sample(range(10000),k=10)7677#Se establece los X necesarios para un tamaño de muestra de 1078x1 = np.random.rand(10) # uniform distribution [0,1]79x2 = np.random.rand(10) # uniform distribution [0,1]80x3 = np.random.rand(10) # uniform distribution [0,1]81x4 = np.random.rand(10) # uniform distribution [0,1]82e = np.random.normal(0,1,10) # normal distribution mean = 0 and sd = 183z = np.random.rand(10)84# Se genera la ecuación de regresión8586Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e8788X = np.column_stack((np.ones(10),x1,x2,x3,x4))89X9091from scipy.stats import t # t - student92def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):9394if standar and Pvalue and (instrumento is None) and (index is None) :9596beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta9798y_est = X @ beta ## Y estimado99n = X.shape[0]100k = X.shape[1] - 1101nk = n - k ## grados de libertad102sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk103Var = sigma*np.linalg.inv(X.T @ X)104sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var105t_est = np.absolute(beta/sd)106pvalue = (1 - t.cdf(t_est, df=nk) ) * 2107df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,108"Pvalue" : pvalue})109110111elif (not instrumento is None) and (not index is None) :112113beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )114115index = index - 1116Z = X117Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables118beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )119x_est = Z @ beta_x120X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado121beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )122df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})123124return df125126127128ols(X,Y)129130ols(X,Y,instrumento = z, index = 1)131132# Para un tamaño de muestra de 50133134random.sample(range(10000),k=50)135x1 = np.random.rand(50) # uniform distribution [0,1]136x2 = np.random.rand(50) # uniform distribution [0,1]137x3 = np.random.rand(50) # uniform distribution [0,1]138x4 = np.random.rand(50) # uniform distribution [0,1]139e = np.random.normal(0,1,50) # normal distribution mean = 0 and sd = 1140z = np.random.rand(50)141# Poblacional regression (Data Generating Process GDP)142143144Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e145X = np.column_stack((np.ones(50),x1,x2,x3,x4))146X147from scipy.stats import t # t - student148def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):149150if standar and Pvalue and (instrumento is None) and (index is None) :151152beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta153154y_est = X @ beta ## Y estimado155n = X.shape[0]156k = X.shape[1] - 1157nk = n - k ## grados de libertad158sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk159Var = sigma*np.linalg.inv(X.T @ X)160sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var161t_est = np.absolute(beta/sd)162pvalue = (1 - t.cdf(t_est, df=nk) ) * 2163df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,164"Pvalue" : pvalue})165166167elif (not instrumento is None) and (not index is None) :168169beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )170171index = index - 1172Z = X173Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables174beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )175x_est = Z @ beta_x176X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado177beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )178df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})179180return df181182183184ols(X,Y)185186ols(X,Y,instrumento = z, index = 1)187188# Para una muestra de 80189190random.sample(range(10000),k=80)191x1 = np.random.rand(80) # uniform distribution [0,1]192x2 = np.random.rand(80) # uniform distribution [0,1]193x3 = np.random.rand(80) # uniform distribution [0,1]194x4 = np.random.rand(80) # uniform distribution [0,1]195e = np.random.normal(0,1,80) # normal distribution mean = 0 and sd = 1196z = np.random.rand(80)197# Poblacional regression (Data Generating Process GDP)198199200Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e201202X = np.column_stack((np.ones(80),x1,x2,x3,x4))203X204from scipy.stats import t # t - student205def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):206207if standar and Pvalue and (instrumento is None) and (index is None) :208209beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta210211y_est = X @ beta ## Y estimado212n = X.shape[0]213k = X.shape[1] - 1214nk = n - k ## grados de libertad215sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk216Var = sigma*np.linalg.inv(X.T @ X)217sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var218t_est = np.absolute(beta/sd)219pvalue = (1 - t.cdf(t_est, df=nk) ) * 2220df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,221"Pvalue" : pvalue})222223224elif (not instrumento is None) and (not index is None) :225226beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )227228index = index - 1229Z = X230Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables231beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )232x_est = Z @ beta_x233X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado234beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )235df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})236237return df238239240241ols(X,Y)242243ols(X,Y,instrumento = z, index = 1)244245# Para una muestra de 120246247random.sample(range(10000),k=120)248x1 = np.random.rand(120) # uniform distribution [0,1]249x2 = np.random.rand(120) # uniform distribution [0,1]250x3 = np.random.rand(120) # uniform distribution [0,1]251x4 = np.random.rand(120) # uniform distribution [0,1]252e = np.random.normal(0,1,120) # normal distribution mean = 0 and sd = 1253z = np.random.rand(120)254# Poblacional regression (Data Generating Process GDP)255256257Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e258X = np.column_stack((np.ones(120),x1,x2,x3,x4))259X260from scipy.stats import t # t - student261def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):262263if standar and Pvalue and (instrumento is None) and (index is None) :264265beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta266267y_est = X @ beta ## Y estimado268n = X.shape[0]269k = X.shape[1] - 1270nk = n - k ## grados de libertad271sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk272Var = sigma*np.linalg.inv(X.T @ X)273sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var274t_est = np.absolute(beta/sd)275pvalue = (1 - t.cdf(t_est, df=nk) ) * 2276df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,277"Pvalue" : pvalue})278279280elif (not instrumento is None) and (not index is None) :281282beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )283284index = index - 1285Z = X286Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables287beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )288x_est = Z @ beta_x289X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado290beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )291df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})292293return df294295296297ols(X,Y)298299ols(X,Y,instrumento = z, index = 1)300301302# Para una muestra de 200303304random.sample(range(10000),k=200)305x1 = np.random.rand(200) # uniform distribution [0,1]306x2 = np.random.rand(200) # uniform distribution [0,1]307x3 = np.random.rand(200) # uniform distribution [0,1]308x4 = np.random.rand(200) # uniform distribution [0,1]309e = np.random.normal(0,1,200) # normal distribution mean = 0 and sd = 1310z = np.random.rand(200)311# Poblacional regression (Data Generating Process GDP)312313314Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e315X = np.column_stack((np.ones(200),x1,x2,x3,x4))316X317from scipy.stats import t # t - student318def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):319320if standar and Pvalue and (instrumento is None) and (index is None) :321322beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta323324y_est = X @ beta ## Y estimado325n = X.shape[0]326k = X.shape[1] - 1327nk = n - k ## grados de libertad328sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk329Var = sigma*np.linalg.inv(X.T @ X)330sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var331t_est = np.absolute(beta/sd)332pvalue = (1 - t.cdf(t_est, df=nk) ) * 2333df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,334"Pvalue" : pvalue})335336337elif (not instrumento is None) and (not index is None) :338339beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )340341index = index - 1342Z = X343Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables344beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )345x_est = Z @ beta_x346X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado347beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )348df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})349350return df351352# Para una muestra de 500353354ols(X,Y)355356ols(X,Y,instrumento = z, index = 1)357358random.sample(range(10000),k=500)359x1 = np.random.rand(500) # uniform distribution [0,1]360x2 = np.random.rand(500) # uniform distribution [0,1]361x3 = np.random.rand(500) # uniform distribution [0,1]362x4 = np.random.rand(500) # uniform distribution [0,1]363e = np.random.normal(0,1,500) # normal distribution mean = 0 and sd = 1364z = np.random.rand(500)365# Poblacional regression (Data Generating Process GDP)366367368Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e369X = np.column_stack((np.ones(500),x1,x2,x3,x4))370X371from scipy.stats import t # t - student372def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):373374if standar and Pvalue and (instrumento is None) and (index is None) :375376beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta377378y_est = X @ beta ## Y estimado379n = X.shape[0]380k = X.shape[1] - 1381nk = n - k ## grados de libertad382sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk383Var = sigma*np.linalg.inv(X.T @ X)384sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var385t_est = np.absolute(beta/sd)386pvalue = (1 - t.cdf(t_est, df=nk) ) * 2387df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,388"Pvalue" : pvalue})389390391elif (not instrumento is None) and (not index is None) :392393beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )394395index = index - 1396Z = X397Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables398beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )399x_est = Z @ beta_x400X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado401beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )402df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})403404return df405406407408ols(X,Y)409410ols(X,Y,instrumento = z, index = 1)411412# Para una muestra de 1000413414random.sample(range(10000),k=800)415x1 = np.random.rand(800) # uniform distribution [0,1]416x2 = np.random.rand(800) # uniform distribution [0,1]417x3 = np.random.rand(800) # uniform distribution [0,1]418x4 = np.random.rand(800) # uniform distribution [0,1]419e = np.random.normal(0,1,800) # normal distribution mean = 0 and sd = 1420z = np.random.rand(800)421# Poblacional regression (Data Generating Process GDP)422423424Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e425X = np.column_stack((np.ones(800),x1,x2,x3,x4))426X427from scipy.stats import t # t - student428def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):429430if standar and Pvalue and (instrumento is None) and (index is None) :431432beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta433434y_est = X @ beta ## Y estimado435n = X.shape[0]436k = X.shape[1] - 1437nk = n - k ## grados de libertad438sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk439Var = sigma*np.linalg.inv(X.T @ X)440sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var441t_est = np.absolute(beta/sd)442pvalue = (1 - t.cdf(t_est, df=nk) ) * 2443df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,444"Pvalue" : pvalue})445446447elif (not instrumento is None) and (not index is None) :448449beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )450451index = index - 1452Z = X453Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables454beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )455x_est = Z @ beta_x456X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado457beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )458df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})459460return df461462463464ols(X,Y)465466ols(X,Y,instrumento = z, index = 1)467468469# Para una muestra de 5000470random.sample(range(10000),k=1000)471x1 = np.random.rand(1000) # uniform distribution [0,1]472x2 = np.random.rand(1000) # uniform distribution [0,1]473x3 = np.random.rand(1000) # uniform distribution [0,1]474x4 = np.random.rand(1000) # uniform distribution [0,1]475e = np.random.normal(0,1,1000) # normal distribution mean = 0 and sd = 1476z = np.random.rand(1000)477# Poblacional regression (Data Generating Process GDP)478479480Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e481X = np.column_stack((np.ones(1000),x1,x2,x3,x4))482X483from scipy.stats import t # t - student484def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):485486if standar and Pvalue and (instrumento is None) and (index is None) :487488beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta489490y_est = X @ beta ## Y estimado491n = X.shape[0]492k = X.shape[1] - 1493nk = n - k ## grados de libertad494sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk495Var = sigma*np.linalg.inv(X.T @ X)496sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var497t_est = np.absolute(beta/sd)498pvalue = (1 - t.cdf(t_est, df=nk) ) * 2499df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,500"Pvalue" : pvalue})501502503elif (not instrumento is None) and (not index is None) :504505beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )506507index = index - 1508Z = X509Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables510beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )511x_est = Z @ beta_x512X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado513beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )514df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})515516return df517518519520ols(X,Y)521522ols(X,Y,instrumento = z, index = 1)523524# PREGUNTA 4525526random.sample(range(800),k=50)527x1 = np.random.rand(800) # uniform distribution [0,1]528x2 = np.random.rand(800) # uniform distribution [0,1]529x3 = np.random.rand(800) # uniform distribution [0,1]530x4 = np.random.rand(800) # uniform distribution [0,1]531x5 = np.random.rand(800) # uniform distribution [0,1]532x6 = np.random.rand(800)# uniform distribution [0,1]533x7 = np.random.rand(800)# uniform distribution [0,1]534535e = np.random.normal(0,1,800) # normal distribution mean = 0 and sd = 1536z = np.random.rand(800)537# Poblacional regression (Data Generating Process GDP)538539540Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 +1.5*x5 +1.5*x6 +1.5*x7+ e541X = np.column_stack((np.ones(800),x1,x2,x3,x4,x5,x6,x7))542X543from scipy.stats import t # t - student544def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):545546if standar and Pvalue and (instrumento is None) and (index is None) :547548beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta549550y_est = X @ beta ## Y estimado551n = X.shape[0]552k = X.shape[1] - 1553nk = n - k ## grados de libertad554sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk555Var = sigma*np.linalg.inv(X.T @ X)556sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var557t_est = np.absolute(beta/sd)558root =sum(list( map( lambda x: x**2 , Y - y_est) )) / n559liminf = beta - 1.96 * sd # Limite inferior560limsup = beta + 1.96*sd # Limite superior561pvalue = (1 - t.cdf(t_est, df=nk) ) * 2562df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,563"Pvalue" : pvalue, "Lim.Inf": liminf, "Limf.Sup": limsup})564565566elif (not instrumento is None) and (not index is None) :567568beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )569570index = index - 1571Z = X572Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables573beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )574x_est = Z @ beta_x575X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado576beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )577df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})578579return df580581ols(X,Y)582583584585#%%Pregunta 3586587#Importamos las bases requeridas:588import random589import pandas as pd590import numpy as np591592#Establecemos una base de datos que no cambia para cada tamaño de muestra593random.seed(100000)594random.sample(range(10000),k=10)595596#Se establece los X necesarios para un tamaño de muestra de 10597x1 = np.random.rand(10) # uniform distribution [0,1]598x2 = np.random.rand(10) # uniform distribution [0,1]599x3 = np.random.rand(10) # uniform distribution [0,1]600x4 = np.random.rand(10) # uniform distribution [0,1]601e = np.random.normal(0,1,10) # normal distribution mean = 0 and sd = 1602z = np.random.rand(10)603# Se genera la ecuación de regresión604605Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e606607X = np.column_stack((np.ones(10),x1,x2,x3,x4))608X609610from scipy.stats import t # t - student611def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):612613if standar and Pvalue and (instrumento is None) and (index is None) :614615beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta616617y_est = X @ beta ## Y estimado618n = X.shape[0]619k = X.shape[1] - 1620nk = n - k ## grados de libertad621sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk622Var = sigma*np.linalg.inv(X.T @ X)623sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var624t_est = np.absolute(beta/sd)625pvalue = (1 - t.cdf(t_est, df=nk) ) * 2626df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,627"Pvalue" : pvalue})628629630elif (not instrumento is None) and (not index is None) :631632beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )633634index = index - 1635Z = X636Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables637beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )638x_est = Z @ beta_x639X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado640beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )641df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})642643return df644645646647ols(X,Y)648649ols(X,Y,instrumento = z, index = 1)650651# Para un tamaño de muestra de 50652653random.sample(range(10000),k=50)654x1 = np.random.rand(50) # uniform distribution [0,1]655x2 = np.random.rand(50) # uniform distribution [0,1]656x3 = np.random.rand(50) # uniform distribution [0,1]657x4 = np.random.rand(50) # uniform distribution [0,1]658e = np.random.normal(0,1,50) # normal distribution mean = 0 and sd = 1659z = np.random.rand(50)660# Poblacional regression (Data Generating Process GDP)661662663Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e664X = np.column_stack((np.ones(50),x1,x2,x3,x4))665X666from scipy.stats import t # t - student667def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):668669if standar and Pvalue and (instrumento is None) and (index is None) :670671beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta672673y_est = X @ beta ## Y estimado674n = X.shape[0]675k = X.shape[1] - 1676nk = n - k ## grados de libertad677sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk678Var = sigma*np.linalg.inv(X.T @ X)679sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var680t_est = np.absolute(beta/sd)681pvalue = (1 - t.cdf(t_est, df=nk) ) * 2682df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,683"Pvalue" : pvalue})684685686elif (not instrumento is None) and (not index is None) :687688beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )689690index = index - 1691Z = X692Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables693beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )694x_est = Z @ beta_x695X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado696beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )697df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})698699return df700701702703ols(X,Y)704705ols(X,Y,instrumento = z, index = 1)706707# Para una muestra de 80708709random.sample(range(10000),k=80)710x1 = np.random.rand(80) # uniform distribution [0,1]711x2 = np.random.rand(80) # uniform distribution [0,1]712x3 = np.random.rand(80) # uniform distribution [0,1]713x4 = np.random.rand(80) # uniform distribution [0,1]714e = np.random.normal(0,1,80) # normal distribution mean = 0 and sd = 1715z = np.random.rand(80)716# Poblacional regression (Data Generating Process GDP)717718719Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e720721X = np.column_stack((np.ones(80),x1,x2,x3,x4))722X723from scipy.stats import t # t - student724def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):725726if standar and Pvalue and (instrumento is None) and (index is None) :727728beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta729730y_est = X @ beta ## Y estimado731n = X.shape[0]732k = X.shape[1] - 1733nk = n - k ## grados de libertad734sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk735Var = sigma*np.linalg.inv(X.T @ X)736sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var737t_est = np.absolute(beta/sd)738pvalue = (1 - t.cdf(t_est, df=nk) ) * 2739df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,740"Pvalue" : pvalue})741742743elif (not instrumento is None) and (not index is None) :744745beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )746747index = index - 1748Z = X749Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables750beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )751x_est = Z @ beta_x752X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado753beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )754df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})755756return df757758759760ols(X,Y)761762ols(X,Y,instrumento = z, index = 1)763764# Para una muestra de 120765766random.sample(range(10000),k=120)767x1 = np.random.rand(120) # uniform distribution [0,1]768x2 = np.random.rand(120) # uniform distribution [0,1]769x3 = np.random.rand(120) # uniform distribution [0,1]770x4 = np.random.rand(120) # uniform distribution [0,1]771e = np.random.normal(0,1,120) # normal distribution mean = 0 and sd = 1772z = np.random.rand(120)773# Poblacional regression (Data Generating Process GDP)774775776Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e777X = np.column_stack((np.ones(120),x1,x2,x3,x4))778X779from scipy.stats import t # t - student780def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):781782if standar and Pvalue and (instrumento is None) and (index is None) :783784beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta785786y_est = X @ beta ## Y estimado787n = X.shape[0]788k = X.shape[1] - 1789nk = n - k ## grados de libertad790sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk791Var = sigma*np.linalg.inv(X.T @ X)792sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var793t_est = np.absolute(beta/sd)794pvalue = (1 - t.cdf(t_est, df=nk) ) * 2795df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,796"Pvalue" : pvalue})797798799elif (not instrumento is None) and (not index is None) :800801beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )802803index = index - 1804Z = X805Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables806beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )807x_est = Z @ beta_x808X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado809beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )810df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})811812return df813814815816ols(X,Y)817818ols(X,Y,instrumento = z, index = 1)819820821# Para una muestra de 200822823random.sample(range(10000),k=200)824x1 = np.random.rand(200) # uniform distribution [0,1]825x2 = np.random.rand(200) # uniform distribution [0,1]826x3 = np.random.rand(200) # uniform distribution [0,1]827x4 = np.random.rand(200) # uniform distribution [0,1]828e = np.random.normal(0,1,200) # normal distribution mean = 0 and sd = 1829z = np.random.rand(200)830# Poblacional regression (Data Generating Process GDP)831832833Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e834X = np.column_stack((np.ones(200),x1,x2,x3,x4))835X836from scipy.stats import t # t - student837def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):838839if standar and Pvalue and (instrumento is None) and (index is None) :840841beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta842843y_est = X @ beta ## Y estimado844n = X.shape[0]845k = X.shape[1] - 1846nk = n - k ## grados de libertad847sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk848Var = sigma*np.linalg.inv(X.T @ X)849sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var850t_est = np.absolute(beta/sd)851pvalue = (1 - t.cdf(t_est, df=nk) ) * 2852df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,853"Pvalue" : pvalue})854855856elif (not instrumento is None) and (not index is None) :857858beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )859860index = index - 1861Z = X862Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables863beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )864x_est = Z @ beta_x865X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado866beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )867df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})868869return df870871# Para una muestra de 500872873ols(X,Y)874875ols(X,Y,instrumento = z, index = 1)876877random.sample(range(10000),k=500)878x1 = np.random.rand(500) # uniform distribution [0,1]879x2 = np.random.rand(500) # uniform distribution [0,1]880x3 = np.random.rand(500) # uniform distribution [0,1]881x4 = np.random.rand(500) # uniform distribution [0,1]882e = np.random.normal(0,1,500) # normal distribution mean = 0 and sd = 1883z = np.random.rand(500)884# Poblacional regression (Data Generating Process GDP)885886887Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e888X = np.column_stack((np.ones(500),x1,x2,x3,x4))889X890from scipy.stats import t # t - student891def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):892893if standar and Pvalue and (instrumento is None) and (index is None) :894895beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta896897y_est = X @ beta ## Y estimado898n = X.shape[0]899k = X.shape[1] - 1900nk = n - k ## grados de libertad901sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk902Var = sigma*np.linalg.inv(X.T @ X)903sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var904t_est = np.absolute(beta/sd)905pvalue = (1 - t.cdf(t_est, df=nk) ) * 2906df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,907"Pvalue" : pvalue})908909910elif (not instrumento is None) and (not index is None) :911912beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )913914index = index - 1915Z = X916Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables917beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )918x_est = Z @ beta_x919X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado920beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )921df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})922923return df924925926927ols(X,Y)928929ols(X,Y,instrumento = z, index = 1)930931# Para una muestra de 1000932933random.sample(range(10000),k=800)934x1 = np.random.rand(800) # uniform distribution [0,1]935x2 = np.random.rand(800) # uniform distribution [0,1]936x3 = np.random.rand(800) # uniform distribution [0,1]937x4 = np.random.rand(800) # uniform distribution [0,1]938e = np.random.normal(0,1,800) # normal distribution mean = 0 and sd = 1939z = np.random.rand(800)940# Poblacional regression (Data Generating Process GDP)941942943Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e944X = np.column_stack((np.ones(800),x1,x2,x3,x4))945X946from scipy.stats import t # t - student947def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):948949if standar and Pvalue and (instrumento is None) and (index is None) :950951beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta952953y_est = X @ beta ## Y estimado954n = X.shape[0]955k = X.shape[1] - 1956nk = n - k ## grados de libertad957sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk958Var = sigma*np.linalg.inv(X.T @ X)959sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var960t_est = np.absolute(beta/sd)961pvalue = (1 - t.cdf(t_est, df=nk) ) * 2962df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,963"Pvalue" : pvalue})964965966elif (not instrumento is None) and (not index is None) :967968beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )969970index = index - 1971Z = X972Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables973beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )974x_est = Z @ beta_x975X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado976beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )977df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})978979return df980981982983ols(X,Y)984985ols(X,Y,instrumento = z, index = 1)986987988# Para una muestra de 5000989random.sample(range(10000),k=1000)990x1 = np.random.rand(1000) # uniform distribution [0,1]991x2 = np.random.rand(1000) # uniform distribution [0,1]992x3 = np.random.rand(1000) # uniform distribution [0,1]993x4 = np.random.rand(1000) # uniform distribution [0,1]994e = np.random.normal(0,1,1000) # normal distribution mean = 0 and sd = 1995z = np.random.rand(1000)996# Poblacional regression (Data Generating Process GDP)997998999Y = 1 + 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + e1000X = np.column_stack((np.ones(1000),x1,x2,x3,x4))1001X1002from scipy.stats import t # t - student1003def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):10041005if standar and Pvalue and (instrumento is None) and (index is None) :10061007beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y ) ## estimación de beta10081009y_est = X @ beta ## Y estimado1010n = X.shape[0]1011k = X.shape[1] - 11012nk = n - k ## grados de libertad1013sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk1014Var = sigma*np.linalg.inv(X.T @ X)1015sd = np.sqrt( np.diag(Var) ) ## raíz cuadrado a los datos de la diagonal principal de Var1016t_est = np.absolute(beta/sd)1017pvalue = (1 - t.cdf(t_est, df=nk) ) * 21018df = pd.DataFrame( {"OLS": beta , "standar_error" : sd ,1019"Pvalue" : pvalue})102010211022elif (not instrumento is None) and (not index is None) :10231024beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )10251026index = index - 11027Z = X1028Z[:,index] = z ## reemplazamos la variable endógena por el instrumento en la matrix de covariables1029beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )1030x_est = Z @ beta_x1031X[:,index] = x_est ## se reemplaza la variable x endógena por su estimado1032beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )1033df = pd.DataFrame( {"OLS": beta , "OLS_IV" : beta_iv})10341035return df1036103710381039ols(X,Y)10401041ols(X,Y,instrumento = z, index = 1)10421043104410451046