import numpy as np
def fdiscontinuous():
x=np.arange(0,500,1)
l=len(x)
y=l
for i in range(l):
if (x[i]>0 and x[i]<100):
print(y[i]==x[i]**1/2)
elif (x[i]>100 and x[i]<300):
print(y[i]==x[i]-5)
elif (x[i]>300 and x[i]<500):
print(y[i]==50)
vector=np.random.randint(0,500,20)
print (vector)
for range(vector) in fdiscontinuous():
print(range(vector))
A <- matrix(vecA, nrow = 1, ncol = 100, byrow = FALSE)
A
B <- matrix(vecA, nrow = 100, ncol = 50, byrow = FALSE)
B
min = which(A == min(A), arr.ind = TRUE)
max = which(A == max(A), arr.ind = TRUE)
Numerador = xi - A[max]
Denominador = A[max] - A[min]
Numerador/denominador
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(10000)
x2 = np.random.rand(10000)
x3 = np.random.rand(10000)
x4 = np.random.rand(10000)
x5 = np.random.rand(10000)
e = np.random.normal(0,1,10000)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(10000),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(10000) , 10000 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(10)
x2 = np.random.rand(10)
x3 = np.random.rand(10)
x4 = np.random.rand(10)
x5 = np.random.rand(10)
e = np.random.normal(0,1,10)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(10),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(10) , 10 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(50)
x2 = np.random.rand(50)
x3 = np.random.rand(50)
x4 = np.random.rand(50)
x5 = np.random.rand(50)
e = np.random.normal(0,1,50)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(50),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(50) , 50 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(80)
x2 = np.random.rand(80)
x3 = np.random.rand(80)
x4 = np.random.rand(80)
x5 = np.random.rand(80)
e = np.random.normal(0,1,80)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(80),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(80) , 80 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(120)
x2 = np.random.rand(120)
x3 = np.random.rand(120)
x4 = np.random.rand(120)
x5 = np.random.rand(120)
e = np.random.normal(0,1,120)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(120),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(120) , 120 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(200)
x2 = np.random.rand(200)
x3 = np.random.rand(200)
x4 = np.random.rand(200)
x5 = np.random.rand(200)
e = np.random.normal(0,1,200)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(200),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(200) , 200 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(500)
x2 = np.random.rand(500)
x3 = np.random.rand(500)
x4 = np.random.rand(500)
x5 = np.random.rand(500)
e = np.random.normal(0,1,500)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(500),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(500) , 500 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(800)
x2 = np.random.rand(800)
x3 = np.random.rand(800)
x4 = np.random.rand(800)
x5 = np.random.rand(800)
e = np.random.normal(0,1,800)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(800),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(800) , 800 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(100)
x2 = np.random.rand(100)
x3 = np.random.rand(100)
x4 = np.random.rand(100)
x5 = np.random.rand(100)
e = np.random.normal(0,1,100)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(100),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(100) , 100 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import random
import numpy as np
from scipy.stats import t
import pandas as pd
x1 = np.random.rand(5000)
x2 = np.random.rand(5000)
x3 = np.random.rand(5000)
x4 = np.random.rand(5000)
x5 = np.random.rand(5000)
e = np.random.normal(0,1,5000)
Y = 0.8*x1 + 1.2*x2 + 0.5*x3 + 1.5*x4 + 3.2*x5 + e
X = np.column_stack((np.ones(5000),x1,x2,x3,x4,x5))
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
random.sample( range(5000) , 5000 )
def ols(M,Y, standar = True, Pvalue = True , instrumento = None, index = None):
if standar and Pvalue and (instrumento is None) and (index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
y_est = X @ beta
n = X.shape[0]
k = X.shape[1] - 1
nk = n - k
sigma = sum(list( map( lambda x: x**2 , Y - y_est) )) / nk
Var = sigma*np.linalg.inv(X.T @ X)
sd = np.sqrt( np.diag(Var) )
t_est = np.absolute(beta/sd)
pvalue = (1 - t.cdf(t_est, df=nk) ) * 2
df = pd.DataFrame( {"tama�o_de_muestra": n , "coeficiente": beta , "error_est�ndar" : sd} )
elif (not instrumento is None) and (not index is None) :
beta = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
index = index - 1
Z = X
Z[:,index] = z
beta_x = np.linalg.inv(Z.T @ Z) @ ((Z.T) @ X[:,index] )
x_est = Z @ beta_x
X[:,index] = x_est
beta_iv = np.linalg.inv(X.T @ X) @ ((X.T) @ Y )
df = pd.DataFrame( {"coeficiente": beta , "coeficiente_IV" : beta_iv})
return df
ols(X,Y)
import numpy as np
import match
import pandas
random.seed(175)
X1 = np.random.rand(800)
X2 = np.random.rand(800)
X3 = np.random.rand(800)
X4 = np.random.rand(800)
X5 = np.random.rand(800)
X6 = np.random.rand(800)
X7 = np.random.rand(800)
u = np.random.normal(0, 1, 800)
Y = 1 + 0.6*X1 + 0.5*X2 + 1.2*X3 + 2.5*X4 + 0.4*X5 + 0.8*X6 + 1.6*X7 +u
matriz = np.column_stack((np.ones(800), X1, X2, X3, X4, X5, X6, X7))
print (matriz)
betas = np.linalg.inv(matriz.T @ matriz) @ ((matriz.T)@ Y)
print (betas)
def statistics.stdev(betas)
betas = betas**1/2
print (betas)