Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Lab1/Julia_lab_1.ipynb
2714 views
Kernel: Julia 1.7.3

Laboratorio 1 - Julia

@author : Roberto Mendoza

# cargamos las librerias, similar a install.packages("nombre de liberia") # import Pkg # Pkg.add("LinearAlgebra") # Pkg.add("Random") # Pkg.add("Distributions") # Pkg.add("Statistics") # Una vez instalado no se necesita cargar nuevamente using LinearAlgebra, Random, Distributions, Statistics # similar a library("nombre de libreria")
a2 = 3.1614 print(a2) typeof(a2)
3.1614
Float64
typeof(floor(a2)) # Entero menor más cercano
Float64
Int(floor(a2))
3
b1 = 10000 typeof(b1)
Int64
# Int to float convert(Float64,b1)
10000.0
# Symbol type print(:a) typeof(:a)
a
Symbol
c1 = "My first python code" print(c1) typeof(c1)
My first python code
String
c1 = "First python code" print(c1, '\n','\n')
First python code
c2 = "Lab1 Pucp" print(c1," : ",c2)
First python code : Lab1 Pucp
print("$c1 : semester 2022-1")
First python code : semester 2022-1
# f-string d = 2022 print("$c1 : semester $d-1")
First python code : semester 2022-1
#first character print("First letter is :", c1[1] )
First letter is :F
#first word print("First word is :", c1[1:5] )
First word is :First

2.0 Boolean

It’s used to represent the truth value of an expression.

"a" == "a"
true
z1 = (1==1) typeof(z1)
Bool
z1 = Int(z1) print(z1)
1
z2 = (10 > 20) Int(z2)
0
z3 = (100 != 100) z3 = Int(z3) typeof(z3)
Int64
L1 = []
Any[]
#double quote is mandatory dis1 = ["ATE", "BARRANCO","BREÑA", "CALLAO", "CARABAYLLO"] dis1
5-element Vector{String}: "ATE" "BARRANCO" "BREÑA" "CALLAO" "CARABAYLLO"
typeof(dis1)
Vector{String} (alias for Array{String, 1})
dis2 = ["ATE", "BARRANCO","BREÑA", "CALLAO", "CARABAYLLO","CHACLACAYO","CHORRILLOS","CIENEGUILLA" ,"COMAS","EL_AGUSTINO","INDEPENDENCIA"] dis2
11-element Vector{String}: "ATE" "BARRANCO" "BREÑA" "CALLAO" "CARABAYLLO" "CHACLACAYO" "CHORRILLOS" "CIENEGUILLA" "COMAS" "EL_AGUSTINO" "INDEPENDENCIA"
dis1[1]
"ATE"
dis1[2:5]
4-element Vector{String}: "BARRANCO" "BREÑA" "CALLAO" "CARABAYLLO"
num = [13,5,5,8,9,10,5,8,13,1,20]
11-element Vector{Int64}: 13 5 5 8 9 10 5 8 13 1 20
a = sort(num) print(a) a[11]
[1, 5, 5, 5, 8, 8, 9, 10, 13, 13, 20]
20
maximum(num)
20
length(dis2)
11
# ! permite alterar el objeto sin asignar nuevamente push!(num, 102)
12-element Vector{Int64}: 13 5 5 8 9 10 5 8 13 1 20 102
num2 = [10,20,30] append!(num, num2)
15-element Vector{Int64}: 13 5 5 8 9 10 5 8 13 1 20 102 10 20 30
print("Suma:", sum(num),'\n', "Minimo:", minimum(num), '\n', "Maximo:", maximum(num))
Suma:259 Minimo:1 Maximo:102

3.0 Dictionary

Postal_code = Dict("Majes" => "string", "Mollendo" => 40701, "Islay" => 40704, "Cotahuasi" => 40801, "Alca" => 40802 ) print(Postal_code)
Dict{String, Any}("Islay" => 40704, "Majes" => "string", "Cotahuasi" => 40801, "Mollendo" => 40701, "Alca" => 40802)
keys(Postal_code)
KeySet for a Dict{String, Int64} with 5 entries. Keys: "Islay" "Majes" "Cotahuasi" "Mollendo" "Alca"
Postal_code = Dict{String,String}("Majes" => 40520, "Mollendo" => 40701, "Islay" => 40704, "Cotahuasi" => 40801, "Alca" => 40802 ) print(Postal_code)
MethodError: Cannot `convert` an object of type Int64 to an object of type String Closest candidates are: convert(::Type{String}, ::String) at C:\Users\Roberto\AppData\Local\Programs\Julia-1.7.3\share\julia\base\essentials.jl:223 convert(::Type{T}, ::T) where T<:AbstractString at C:\Users\Roberto\AppData\Local\Programs\Julia-1.7.3\share\julia\base\strings\basic.jl:231 convert(::Type{T}, ::AbstractString) where T<:AbstractString at C:\Users\Roberto\AppData\Local\Programs\Julia-1.7.3\share\julia\base\strings\basic.jl:232 ... Stacktrace: [1] setindex!(h::Dict{String, String}, v0::Int64, key::String) @ Base .\dict.jl:381 [2] Dict{String, String}(::Pair{String, Int64}, ::Vararg{Pair{String, Int64}}) @ Base .\dict.jl:113 [3] top-level scope @ In[2]:1 [4] eval @ .\boot.jl:373 [inlined] [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base .\loading.jl:1196
# Declarando el tipo de variable en cada llave del diccionario Postal_code = Dict{String, Any}("Majes" => 40520, "Mollendo" => 40701, "Islay" => 40704, "Cotahuasi" => 40801, "Alca" => 40802 ) print(Postal_code)
Dict{String, Any}("Islay" => 40704, "Majes" => 40520, "Cotahuasi" => 40801, "Mollendo" => 40701, "Alca" => 40802)
Postal_code["Islay"]
40704
# add new element merge!(Postal_code, Dict("CHARCANA"=> 40803)) # add dictionary dt1 = Dict{String,Any}("LOMAS" => Dict("UBIGEO" => 40311, "Poverty Rate" => "18.2%", "Population" => "20 mil")) union(Postal_code, dt1)
7-element Vector{Pair{String, Any}}: "Islay" => 40704 "CHARCANA" => 40803 "Majes" => 40520 "Cotahuasi" => 40801 "Mollendo" => 40701 "Alca" => 40802 "LOMAS" => Dict{String, Any}("Poverty Rate" => "18.2%", "UBIGEO" => 40311, "Population" => "20 mil")
# keys cities = ["Fray Martin","Santa Rosa de Puquio","Cuchicorral","Santiago de Punchauca","La Cruz (11 Amigos)","Cerro Cañon", "Cabaña Suche","San Lorenzo","Jose Carlos Mariategui","Pascal"] # values postal_code1 = [15001,15003,15004,15006,15018,15019,15046,15072,15079,15081] ct_pc = Dict( zip( cities , postal_code1) )
Dict{String, Int64} with 10 entries: "Santa Rosa de Puquio" => 15003 "Fray Martin" => 15001 "La Cruz (11 Amigos)" => 15018 "San Lorenzo" => 15072 "Cuchicorral" => 15004 "Santiago de Punchauca" => 15006 "Cabaña Suche" => 15046 "Pascal" => 15081 "Cerro Cañon" => 15019 "Jose Carlos Mariategui" => 15079
ct_pc["Santa Rosa de Puquio"]
15003

6.0 Array and Matrix

# 1D array a = [1, 2, 3, 4, 5.2] typeof(a) a
5-element Vector{Float64}: 1.0 2.0 3.0 4.0 5.2
using Statistics a = [1 2 3 4 5] print(mean(a), "\n") print(std(a))
3.0 1.5811388300841898
# 2D array M = [1 2 3; 4 5 6] typeof(M) size(M) M
2×3 Matrix{Int64}: 1 2 3 4 5 6
print(mean(M, dims =1), "\n") # by coloumn print(mean(M, dims =2)) # by row
[2.5 3.5 4.5] [2.0; 5.0;;]
#rows print("Rows:",size(M,1),"\n", "Columns: ", size(M,2))
Rows:2 Columns: 3
# Create a 1D NumPy array with values from 0 to 20 (exclusively) incremented by 2.5: y = collect(0:2:19) print(y) typeof(y)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Vector{Int64} (alias for Array{Int64, 1})
y = collect(0:10) print(y)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
M1 = zeros(8, 2) print(M1)
[0.0 0.0; 0.0 0.0; 0.0 0.0; 0.0 0.0; 0.0 0.0; 0.0 0.0; 0.0 0.0; 0.0 0.0]
8×2 Matrix{Float64}: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
M2 = ones(8, 4) M2
8×4 Matrix{Float64}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
M3 = hcat(M1,M2) size(M3) M3
8×6 Matrix{Float64}: 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 1.0 1.0 1.0
M4 = [[2 2 3 4 5 1];[1 5 5 9 8 2]] M4
2×6 Matrix{Int64}: 2 2 3 4 5 1 1 5 5 9 8 2
M5 = vcat(M3,M4) M5 transpose(M5)
6×10 transpose(::Matrix{Float64}) with eltype Float64: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 2.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 2.0 5.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 3.0 5.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 4.0 9.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 5.0 8.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0
w = ones(10)
10-element Vector{Float64}: 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
using LinearAlgebra Iden = Matrix(1I,8,8)
8×8 Matrix{Int64}: 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
I3 = reshape(Iden, 32, 2) I3
32×2 Matrix{Int64}: 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ⋮ 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
print(I3') # transpose size(I3')
[1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0; 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1]
(2, 32)
using Random using Distributions 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 # DGP Y = ones(500) + 0.8.*x1 + 1.2.*x2 + 0.5.*x3 + 1.5.*x4 + e
500-element Vector{Float64}: 1.896566070281705 4.621974431654904 3.526833836299257 0.10837079046204412 0.6527452719436257 1.412358539595111 2.06859432331957 2.4565096841391636 3.9559135914316514 2.4897010290426653 2.322449429266815 0.9847006960490069 3.3249652391864455 ⋮ 4.488724947070592 2.1546739990899377 3.505306608738553 3.111049918149344 4.607534071526726 3.2385856370553174 4.20976100605967 0.8433212962236212 2.998832343279894 5.203629452752528 3.895110270410055 0.9342847527281901
# joint vectors to matrix X = hcat(ones(500),x1,x2,x3,x4)
500×5 Matrix{Float64}: 1.0 0.69395 0.230708 0.755381 0.114891 1.0 0.342808 0.391855 0.509551 0.864573 1.0 0.878973 0.688652 0.400967 0.569701 1.0 0.437436 0.0768201 0.885261 0.123005 1.0 0.189015 0.399809 0.407279 0.394585 1.0 0.218179 0.109448 0.00840352 0.108008 1.0 0.544024 0.205616 0.267832 0.0436557 1.0 0.962239 0.925667 0.216082 0.352726 1.0 0.502289 0.480901 0.288146 0.917227 1.0 0.0176643 0.874951 0.452493 0.725432 1.0 0.702488 0.203521 0.835733 0.277678 1.0 0.387177 0.185274 0.630065 0.00152385 1.0 0.75172 0.170344 0.887998 0.0214684 ⋮ 1.0 0.517387 0.0729328 0.315226 0.189123 1.0 0.32472 0.409452 0.256903 0.110878 1.0 0.848717 0.0184663 0.557234 0.792108 1.0 0.839263 0.819206 0.587164 0.0276174 1.0 0.296967 0.375418 0.44435 0.955923 1.0 0.461611 0.392524 0.155386 0.329141 1.0 0.652895 0.973743 0.164328 0.56274 1.0 0.422581 0.780213 0.350788 0.626888 1.0 0.471142 0.635874 0.0478847 0.641411 1.0 0.243467 0.652978 0.630408 0.26652 1.0 0.21262 0.738478 0.286239 0.69063 1.0 0.19871 0.16188 0.645123 0.29906
# OLS method beta = inv(transpose(X) * X) * (transpose(X) * Y)
5-element Vector{Float64}: 0.975872412578692 0.862370979221387 1.385111654309122 0.5676058489648621 1.3399985203750866