Path: blob/main/Trabajo_grupal/WG1/Grupo_6.py
2714 views
####EJERCICIO 112#Importamos la librería numpy3import numpy as np45#Como se utilizará una función utilizacom def6def fdiscontinuous():78#Creamos el vector, donde queremos entre 0 y 500 observaciones con rango 19x=np.arange(0,500,1)10l=len(x)11y=l12#Creamos i para que el programa itere los valores del vector y asi los evalue segun cada condicion13for i in range(l):14if (x[i]>0 and x[i]<100):15print(y[i]==x[i]**1/2)16elif (x[i]>100 and x[i]<300):17print(y[i]==x[i]-5)18elif (x[i]>300 and x[i]<500):19print(y[i]==50)20#El vector aleatorio se desarrollará a continuación, donde se pide 20 observaciones aleatorias21vector=np.random.randint(0,500,20)22print (vector)23for range(vector) in fdiscontinuous():24print(range(vector))2526272829######EJERCICIO 23031#Importamos los numpys, al igual que random para la realización del ejercicio32import numpy as np33import random34random.seed(7)353637# In[170]:383940#Creación del vector y matriz aleatorios*41#especificando un límite de 100 observaciones y de 2 cifras cada número.424344# In[171]:454647vector_2=np.random.randint(0, 100, size=(1, 100))48print(vector_2)495051# In[172]:525354matriz_2=np.random.randint(0, 100, size=(100, 50))55print(matriz_2)565758# In[173]:596061#Una vez creados, podremos hallar los valores máximo y mínimo de cada uno para la elaboración de la fórmula solicitada.6263#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.646566# In[174]:676869#Ahora podremos plantear la fórmula y reescribir los valores de la matriz solicitada. Esto servirá para Reescalar Cada elemento.707172# In[189]:737475for f in range(0, 100):76for g in range(100, 50):77escalar = (matriz_2[f][g]-np.max(vector_2, axis = 1))/((np.max(vector_2, axis = 1)-np.min(vector_2, axis = 1)))78if matriz_2[f][g] >= 101:79matriz_2[f][g] == 080else:81matriz_2[f][g] = escalar82print(matriz_2)8384858687####EJERCICIO 38889#Crear un proceso generador de datos con 5 variables y un tamaño de población de 10 mil observaciones.90#Crear un Loop que estime los coeficientes de un modelo de regresión lineal omitiendo una variable explicativa91#del proceso generador de datos. Asimismo, debe hallar el error estándar en cada iteración.92#Usar los siguientes tamaños de muestra en el Loop n : 10, 50, 80 , 120 , 200, 500,800,100, 5000.93#Comente sus resultados. Los resultados deben presentarse en una Dataframe.94#El nombre de las columnas serán (tamaño de muestra, coeficiente y error estándar ).95#Nótese que debe construir las columnas del coeficiente y error estándar para cada regresor x .969798### para cuando el número de observaciones es 10000: n=1000099100#Importamos las librerias necesarias para el programa101import random102import numpy as np103from scipy.stats import t #para t - student104import pandas as pd105106#Generamos un proceso generador de datos con 5 variables y un error estandar, que posee 10 000 observaciones107x1 = np.random.rand(10000)108x2 = np.random.rand(10000)109x3 = np.random.rand(10000)110x4 = np.random.rand(10000)111x5 = np.random.rand(10000)112e = np.random.normal(0,1,10000) #El error estandar tiene distribucion normal113114#funcion con las 5 variables y su término de error115Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e116117#matriz de la funcion Y118X = np.column_stack((np.ones(10000),x1,x2,x3,x4,x5))119120#coeficiientes estimados de la matriz X121beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )122123#indexing aleatorios para la matriz X124random.sample( range(10000) , 10000 )125126127def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):128129if standar and Pvalue and (instrumento is None) and (index is None) :130131beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )132133y_est = X @ beta134n = X.shape[0]135k = X.shape[1] - 1136nk = n - k137sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk138Var = sigma*np.linalg.inv(X.T @ X)139sd = np.sqrt( np.diag(Var) )140t_est = np.absolute(beta/sd)141pvalue = (1 - t.cdf(t_est, df=nk) ) * 2142df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )143144145elif (not instrumento is None) and (not index is None) :146147beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )148149index = index - 1150Z = X151Z[:,index] = z152beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )153x_est = Z @ beta_x154X[:,index] = x_est155beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )156df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})157158return df159160#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar161ols(X,Y)162163164165### para cuando el número de observaciones es 10: n=10166167import random168import numpy as np169from scipy.stats import t #para t - student170import pandas as pd171172#se requiere una funcion con 5 variables aleatorias173x1 = np.random.rand(10)174x2 = np.random.rand(10)175x3 = np.random.rand(10)176x4 = np.random.rand(10)177x5 = np.random.rand(10)178e = np.random.normal(0,1,10)179180#funcion con las 5 variables y su término de error181Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e182183#matriz de la funcion Y184X = np.column_stack((np.ones(10),x1,x2,x3,x4,x5))185186#coeficiientes estimados de la matriz X187beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )188189#indexing aleatorios para la matriz X190random.sample( range(10) , 10 )191192193def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):194195if standar and Pvalue and (instrumento is None) and (index is None) :196197beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )198199y_est = X @ beta200n = X.shape[0]201k = X.shape[1] - 1202nk = n - k203sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk204Var = sigma*np.linalg.inv(X.T @ X)205sd = np.sqrt( np.diag(Var) )206t_est = np.absolute(beta/sd)207pvalue = (1 - t.cdf(t_est, df=nk) ) * 2208df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )209210211elif (not instrumento is None) and (not index is None) :212213beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )214215index = index - 1216Z = X217Z[:,index] = z218beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )219x_est = Z @ beta_x220X[:,index] = x_est221beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )222df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})223224return df225226#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar227ols(X,Y)228229230231### para cuando el número de observaciones es 50: n=50232233import random234import numpy as np235from scipy.stats import t #para t - student236import pandas as pd237238#se requiere una funcion con 5 variables aleatorias239x1 = np.random.rand(50)240x2 = np.random.rand(50)241x3 = np.random.rand(50)242x4 = np.random.rand(50)243x5 = np.random.rand(50)244e = np.random.normal(0,1,50)245246#funcion con las 5 variables y su término de error247Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e248249#matriz de la funcion Y250X = np.column_stack((np.ones(50),x1,x2,x3,x4,x5))251252#coeficiientes estimados de la matriz X253beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )254255#indexing aleatorios para la matriz X256random.sample( range(50) , 50 )257258259def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):260261if standar and Pvalue and (instrumento is None) and (index is None) :262263beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )264265y_est = X @ beta266n = X.shape[0]267k = X.shape[1] - 1268nk = n - k269sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk270Var = sigma*np.linalg.inv(X.T @ X)271sd = np.sqrt( np.diag(Var) )272t_est = np.absolute(beta/sd)273pvalue = (1 - t.cdf(t_est, df=nk) ) * 2274df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )275276277elif (not instrumento is None) and (not index is None) :278279beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )280281index = index - 1282Z = X283Z[:,index] = z284beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )285x_est = Z @ beta_x286X[:,index] = x_est287beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )288df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})289290return df291292#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar293ols(X,Y)294295296297### para cuando el número de observaciones es 80: n=80298299import random300import numpy as np301from scipy.stats import t #para t - student302import pandas as pd303304#se requiere una funcion con 5 variables aleatorias305x1 = np.random.rand(80)306x2 = np.random.rand(80)307x3 = np.random.rand(80)308x4 = np.random.rand(80)309x5 = np.random.rand(80)310e = np.random.normal(0,1,80)311312#funcion con las 5 variables y su término de error313Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e314315#matriz de la funcion Y316X = np.column_stack((np.ones(80),x1,x2,x3,x4,x5))317318#coeficiientes estimados de la matriz X319beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )320321#indexing aleatorios para la matriz X322random.sample( range(80) , 80 )323324325def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):326327if standar and Pvalue and (instrumento is None) and (index is None) :328329beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )330331y_est = X @ beta332n = X.shape[0]333k = X.shape[1] - 1334nk = n - k335sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk336Var = sigma*np.linalg.inv(X.T @ X)337sd = np.sqrt( np.diag(Var) )338t_est = np.absolute(beta/sd)339pvalue = (1 - t.cdf(t_est, df=nk) ) * 2340df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )341342343elif (not instrumento is None) and (not index is None) :344345beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )346347index = index - 1348Z = X349Z[:,index] = z350beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )351x_est = Z @ beta_x352X[:,index] = x_est353beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )354df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})355356return df357358#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar359ols(X,Y)360361362363### para cuando el número de observaciones es 120: n=120364365import random366import numpy as np367from scipy.stats import t #para t - student368import pandas as pd369370#se requiere una funcion con 5 variables aleatorias371x1 = np.random.rand(120)372x2 = np.random.rand(120)373x3 = np.random.rand(120)374x4 = np.random.rand(120)375x5 = np.random.rand(120)376e = np.random.normal(0,1,120)377378#funcion con las 5 variables y su término de error379Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e380381#matriz de la funcion Y382X = np.column_stack((np.ones(120),x1,x2,x3,x4,x5))383384#coeficiientes estimados de la matriz X385beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )386387#indexing aleatorios para la matriz X388random.sample( range(120) , 120 )389390391def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):392393if standar and Pvalue and (instrumento is None) and (index is None) :394395beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )396397y_est = X @ beta398n = X.shape[0]399k = X.shape[1] - 1400nk = n - k401sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk402Var = sigma*np.linalg.inv(X.T @ X)403sd = np.sqrt( np.diag(Var) )404t_est = np.absolute(beta/sd)405pvalue = (1 - t.cdf(t_est, df=nk) ) * 2406df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )407408409elif (not instrumento is None) and (not index is None) :410411beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )412413index = index - 1414Z = X415Z[:,index] = z416beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )417x_est = Z @ beta_x418X[:,index] = x_est419beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )420df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})421422return df423424#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar425ols(X,Y)426427428429### para cuando el número de observaciones es 200: n=200430431import random432import numpy as np433from scipy.stats import t #para t - student434import pandas as pd435436#se requiere una funcion con 5 variables aleatorias437x1 = np.random.rand(200)438x2 = np.random.rand(200)439x3 = np.random.rand(200)440x4 = np.random.rand(200)441x5 = np.random.rand(200)442e = np.random.normal(0,1,200)443444#funcion con las 5 variables y su término de error445Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e446447#matriz de la funcion Y448X = np.column_stack((np.ones(200),x1,x2,x3,x4,x5))449450#coeficiientes estimados de la matriz X451beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )452453#indexing aleatorios para la matriz X454random.sample( range(200) , 200 )455456457def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):458459if standar and Pvalue and (instrumento is None) and (index is None) :460461beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )462463y_est = X @ beta464n = X.shape[0]465k = X.shape[1] - 1466nk = n - k467sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk468Var = sigma*np.linalg.inv(X.T @ X)469sd = np.sqrt( np.diag(Var) )470t_est = np.absolute(beta/sd)471pvalue = (1 - t.cdf(t_est, df=nk) ) * 2472df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )473474475elif (not instrumento is None) and (not index is None) :476477beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )478479index = index - 1480Z = X481Z[:,index] = z482beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )483x_est = Z @ beta_x484X[:,index] = x_est485beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )486df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})487488return df489490#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar491ols(X,Y)492493494495### para cuando el número de observaciones es 500: n=500496497import random498import numpy as np499from scipy.stats import t #para t - student500import pandas as pd501502#se requiere una funcion con 5 variables aleatorias503x1 = np.random.rand(500)504x2 = np.random.rand(500)505x3 = np.random.rand(500)506x4 = np.random.rand(500)507x5 = np.random.rand(500)508e = np.random.normal(0,1,500)509510#funcion con las 5 variables y su término de error511Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e512513#matriz de la funcion Y514X = np.column_stack((np.ones(500),x1,x2,x3,x4,x5))515516#coeficiientes estimados de la matriz X517beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )518519#indexing aleatorios para la matriz X520random.sample( range(500) , 500 )521522523def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):524525if standar and Pvalue and (instrumento is None) and (index is None) :526527beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )528529y_est = X @ beta530n = X.shape[0]531k = X.shape[1] - 1532nk = n - k533sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk534Var = sigma*np.linalg.inv(X.T @ X)535sd = np.sqrt( np.diag(Var) )536t_est = np.absolute(beta/sd)537pvalue = (1 - t.cdf(t_est, df=nk) ) * 2538df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )539540541elif (not instrumento is None) and (not index is None) :542543beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )544545index = index - 1546Z = X547Z[:,index] = z548beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )549x_est = Z @ beta_x550X[:,index] = x_est551beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )552df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})553554return df555556#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar557ols(X,Y)558559560561### para cuando el número de observaciones es 800: n=800562563import random564import numpy as np565from scipy.stats import t #para t - student566import pandas as pd567568#se requiere una funcion con 5 variables aleatorias569x1 = np.random.rand(800)570x2 = np.random.rand(800)571x3 = np.random.rand(800)572x4 = np.random.rand(800)573x5 = np.random.rand(800)574e = np.random.normal(0,1,800)575576#funcion con las 5 variables y su término de error577Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e578579#matriz de la funcion Y580X = np.column_stack((np.ones(800),x1,x2,x3,x4,x5))581582#coeficiientes estimados de la matriz X583beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )584585#indexing aleatorios para la matriz X586random.sample( range(800) , 800 )587588589def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):590591if standar and Pvalue and (instrumento is None) and (index is None) :592593beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )594595y_est = X @ beta596n = X.shape[0]597k = X.shape[1] - 1598nk = n - k599sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk600Var = sigma*np.linalg.inv(X.T @ X)601sd = np.sqrt( np.diag(Var) )602t_est = np.absolute(beta/sd)603pvalue = (1 - t.cdf(t_est, df=nk) ) * 2604df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )605606607elif (not instrumento is None) and (not index is None) :608609beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )610611index = index - 1612Z = X613Z[:,index] = z614beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )615x_est = Z @ beta_x616X[:,index] = x_est617beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )618df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})619620return df621622#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar623ols(X,Y)624625626627### para cuando el número de observaciones es 100: n=100628629import random630import numpy as np631from scipy.stats import t #para t - student632import pandas as pd633634#se requiere una funcion con 5 variables aleatorias635x1 = np.random.rand(100)636x2 = np.random.rand(100)637x3 = np.random.rand(100)638x4 = np.random.rand(100)639x5 = np.random.rand(100)640e = np.random.normal(0,1,100)641642#funcion con las 5 variables y su término de error643Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e644645#matriz de la funcion Y646X = np.column_stack((np.ones(100),x1,x2,x3,x4,x5))647648#coeficiientes estimados de la matriz X649beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )650651#indexing aleatorios para la matriz X652random.sample( range(100) , 100 )653654def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):655656if standar and Pvalue and (instrumento is None) and (index is None) :657658beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )659660y_est = X @ beta661n = X.shape[0]662k = X.shape[1] - 1663nk = n - k664sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk665Var = sigma*np.linalg.inv(X.T @ X)666sd = np.sqrt( np.diag(Var) )667t_est = np.absolute(beta/sd)668pvalue = (1 - t.cdf(t_est, df=nk) ) * 2669df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )670671672elif (not instrumento is None) and (not index is None) :673674beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )675676index = index - 1677Z = X678Z[:,index] = z679beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )680x_est = Z @ beta_x681X[:,index] = x_est682beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )683df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})684685return df686687#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar688ols(X,Y)689690691692### para cuando el número de observaciones es 5000: n=5000693694import random695import numpy as np696from scipy.stats import t #para t - student697import pandas as pd698699#se requiere una funcion con 5 variables aleatorias700x1 = np.random.rand(5000)701x2 = np.random.rand(5000)702x3 = np.random.rand(5000)703x4 = np.random.rand(5000)704x5 = np.random.rand(5000)705e = np.random.normal(0,1,5000)706707#funcion con las 5 variables y su término de error708Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e709710#matriz de la funcion Y711X = np.column_stack((np.ones(5000),x1,x2,x3,x4,x5))712713#coeficiientes estimados de la matriz X714beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )715716#indexing aleatorios para la matriz X717random.sample( range(5000) , 5000 )718719720def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):721722if standar and Pvalue and (instrumento is None) and (index is None) :723724beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )725726y_est = X @ beta727n = X.shape[0]728k = X.shape[1] - 1729nk = n - k730sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk731Var = sigma*np.linalg.inv(X.T @ X)732sd = np.sqrt( np.diag(Var) )733t_est = np.absolute(beta/sd)734pvalue = (1 - t.cdf(t_est, df=nk) ) * 2735df = pd.DataFrame( {"tamaño_de_muestra": n , "coeficiente": beta , "error_estándar" : sd} )736737738elif (not instrumento is None) and (not index is None) :739740beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )741742index = index - 1743Z = X744Z[:,index] = z745beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )746x_est = Z @ beta_x747X[:,index] = x_est748beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )749df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})750751return df752753#para mostrar la dataframe con el tamaño de muestra, coeficientes y el error estándar754ols(X,Y)755756757758###### Fin del ejrecicio 3 ###### Fin del ejrecicio 3 ######759760761762763764765######EJERCICIO 4766767import numpy as np768import match769import pandas770771random.seed(175)772#Creando un proceso generador de datos con 8 variables (intercepto y 7 explicativas)773#Necesitamos 800 observaciones, por ello generaremos 800 números enteros.774775#Entonces se tiene a continuación las siguientes 7 variables explicativas con distribución normal776X1 = np.random.rand(800)777X2 = np.random.rand(800)778X3 = np.random.rand(800)779X4 = np.random.rand(800)780X5 = np.random.rand(800)781X6 = np.random.rand(800)782X7 = np.random.rand(800)783784#Seguido a ello, la creación del error bajo distribucion normal785u = np.random.normal(0, 1, 800)786787#El modelo a estimar que tendremos es Y = b0 + b1*X1 + b2*X2 + b3*X3 + b4*X4 + b5*X5 + b6*X6 + b7*X7 +u788Y = 1 + 0.6*X1 + 0.5*X2 + 1.2*X3 + 2.5*X4 + 0.4*X5 + 0.8*X6 + 1.6*X7 +u789#Ahora, se juntarán todos los vectores generados anteriormente en una matriz790791matriz = np.column_stack((np.ones(800), X1, X2, X3, X4, X5, X6, X7))792print (matriz)793794#Ahora, podemos hallar los betas o también llamados coeficientes estimados, mediante una transposicion de la matriz795betas = np.linalg.inv(matriz.T @ matriz) @ ((matriz.T)@ Y)796print (betas)797798#Ahora ya se tiene un vector que contiene a los betas o coeficientes799#Entonces, a continuación se estimará los errores estándar de cada beta800801def statistics.stdev(betas)802betas = betas**1/2803print (betas)804805806807808809810