Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Trabajo_grupal/WG1/Grupo_5_pynb.ipynb
2714 views
Kernel: Julia 1.6.7
#Ejercicio 5: Traducción de OLS de phyton a julia using Random using Statistics using Distributions, LinearAlgebra using DataFrames Random.seed!(756) x1 = rand(500) # uniform distribution x2 = rand(500) # uniform distribution x3 = rand(500) # uniform distribution x4 = rand(500) # uniform distribution e = randn(500) # normal dsitribution mean = 0 y sd = 1 z = rand(500) # DGP Y = ones(500) + 0.8.*x1 + 1.2.*x2 + 0.5.*x3 + 1.5.*x4 + e X = hcat(ones(500),x1,x2,x3,x4) function ols(M::Matrix , est::Bool = true, pvalue::Bool = true, instrumento = nothing, index = nothing) if est && pvalue && isnothing(instrumento) && isnothing(index) # V y V: V beta = inv(transpose(X)*X)*(transpose(X)*Y) y_est = X * beta n = size(X,1) #número de observaciones contando filas (500) k = size(X,2) - 1 #número de variables menos el intercepto (columnas - 1) (4) nk = n - k #grados de libertad sigma = (sum(string( map((x) -> x^2, Y - y_est) ))) / nk Var = sigma*inv(transpose(X)*X) sd = Var[ diagind(Var) ].^2 t_est = abs(beta/sd) dist = TDist(4) # 4 grados de libertad pvalue = 2*(1 - cdf(dist, test)) df = DataFrame( OLS = beta , standar_error = sd , Pvalue = pvalue) elseif !isnothing(instrumento) && !isnothing(index) beta = inv(transpose(X)*X)*(transpose(X)*Y) index = index - 1 Z = X Z[:,index] = z beta_x = inv(transpose(Z)*Z)*(transpose(Z)*X[:,index] ) x_est = Z * beta_x X[:,index] = x_est beta_iv = inv(transpose(X)*X)*(transpose(X)*Y) df = Dataframe( OLS = beta , OLS_IV = beta_iv) end end return df ols(X,Y) ols(X,Y,instrumento = z, index = 1)
UndefVarError: df not defined Stacktrace: [1] top-level scope @ In[46]:51 [2] eval @ .\boot.jl:360 [inlined] [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base .\loading.jl:1116
#Comprobación línea por línea using Random using Statistics using Distributions, LinearAlgebra using DataFrames Random.seed!(756) x1 = rand(500) # uniform distribution x2 = rand(500) # uniform distribution x3 = rand(500) # uniform distribution x4 = rand(500) # uniform distribution e = randn(500) # normal dsitribution mean = 0 y sd = 1 z = rand(500) # DGP Y = ones(500) + 0.8.*x1 + 1.2.*x2 + 0.5.*x3 + 1.5.*x4 + e X = hcat(ones(500),x1,x2,x3,x4) function ols(M::Matrix , est::Bool = true, pvalue::Bool = true, instrumento = nothing, index = nothing) if est && pvalue && isnothing(instrumento) && isnothing(index) # V y V: V beta = inv(transpose(X)*X)*(transpose(X)*Y) y_est = X * beta n = size(X,1) k = size(X,2) - 1 nk = n - k sigma =sum(any(map((x) -> x.^2,Y-y_est))) / nk end end return sigma #Tuvimos problemas para traducir el sigma y lo demás se tradujo. Pero, al no poder pasar del código de sigma no podemos confir- #mar si la traducción es correcta
500-element Vector{Float64}: 5.4031216073460805e-5 0.0022181722627542654 0.000790960259504303 0.0007112961665073163 0.00032634339912251015 0.004831753130268498 0.005019252688530333 0.000288060620731548 7.174040487996433e-5 0.00011049075694795605 0.0004795361534546874 0.00023878930841659334 0.00019166141127916798 ⋮ 0.0004397426700596576 0.006165252292643008 0.0018333857917609967 0.001544754247826793 0.00011580608403475287 0.010760224300972383 0.0009984917503637041 7.835101346891622e-5 0.0015877328914698987 0.0009349801824308511 0.0003167844872841053 3.4382481100425256e-5