Undergraduate Computational Macro
Plan for your longrun career, languages come and go…
See Syllabus for more details
The tools are interleaved with applications such as
winget install julia -s msstore
in a Windows terminalcurl -fsSL https://install.julialang.org | sh
To open a terminal on MacOS
Cmd + Space
to open Spotlight, then type Terminal
<Cmd-Shift-P>
then View: Toggle Terminal
If you get permissions problems try
sudo curl -fsSL https://install.julialang.org | sh
If it still shows errors, then see here and do some combo of
sudo chown $(id -u):$(id -g) ~/.bashrc
sudo chown $(id -u):$(id -g) ~/.zshrc
sudo chown $(id -u):$(id -g) ~/.bash_profile
sudo curl -fsSL https://install.julialang.org | sh
Open the command palette with <Ctrl+Shift+P>
or <Cmd+Shift+P>
on mac and type > Git: Clone
and choose https://github.com/jlperla/undergrad_computational_macro_notebooks
Instantiate packages, in VSCode or
julia
and ]
enters package mode] add IJulia
, which adds to global environment] activate
, which chooses the Project.toml
file] instantiate
Then use VS Code or jupyter lab
to open
Project.toml
]
for managing packages] activate
or ] activate path/to/project
julia --project
]activate
creates one for the folder] instantiate
to install all the packagesProject.toml
file
] add IJulia
Project.toml
is a Manifest.toml
file which establishes the exact versions for reproducibility
] instantiate
will install the exact versionsep_sum = 0.0 # careful to use 0.0 here, instead of 0
for ep_val in ep
ep_sum = ep_sum + ep_val
end
@show ep_mean = ep_sum / length(ep)
@show ep_mean ≈ mean(ep)
@show ep_mean
@show sum(ep) / length(ep)
@show sum(ep_val for ep_val in ep) / length(ep); # generator/comprehension
ep_mean = ep_sum / length(ep) = -0.014019546438837875
ep_mean ≈ mean(ep) = true
ep_mean = -0.014019546438837875
sum(ep) / length(ep) = -0.014019546438837903
sum((ep_val for ep_val = ep)) / length(ep) = -0.014019546438837875
function generatedata(n)
ep = randn(n) # use built in function
for i in eachindex(ep) # or i in 1:length(ep)
ep[i] = ep[i]^2 # squaring the result
end
return ep
end
data = generatedata(5)
println(data)
[1.7182747971605918, 0.01762455734677663, 1.0111342207535723, 3.2936289192315935, 0.6153258611237733]
function generatedata(n)
ep = randn(n) # use built in function
return ep .^ 2
end
@show generatedata(5)
generatedata2(n) = randn(n) .^ 2
@show generatedata2(5);
generatedata(5) = [0.7664969548681376, 0.5658795847535621, 0.1920865182464282, 0.44516414349150646, 0.4335964686270287]
generatedata2(5) = [3.231571451656551, 0.26727719282014856, 5.756213117912331, 0.32414937784829506, 0.008613087034908314]
rand(dist, n)
changes its behavior based on the type of dist
f3
can change. Avoid ->
if name requiredf2(x; a = 1) = exp(cos(a * x)) # note the ; in the definition
# same as longform
function f(x; a = 1)
return exp(cos(a * x))
end
@show f(pi)
@show f(pi; a = 2) # passing in adate
a = 2
@show f(pi; a); # equivalent to f(pi; a = a)
f(pi) = 0.36787944117144233
f(pi; a = 2) = 2.718281828459045
f(pi; a) = 2.718281828459045
function solve_model(x)
a = x^2
b = 2 * a
c = a + b
return (; a, b, c) # note local scope of tuples!
end
@show solve_model(0.1)
# can unpack in different order, or use subset of values
(; c, a) = solve_model(0.1)
println("a = $a, c = $c");
solve_model(0.1) = (a = 0.010000000000000002, b = 0.020000000000000004, c = 0.030000000000000006)
a = 0.010000000000000002, c = 0.030000000000000006
size(b) = (3,)
size(A) = (2, 2)
typeof(b) = Vector{Float64}
typeof(A) = Matrix{Int64}
zeros(3) = [0.0, 0.0, 0.0]
ones(2, 2) = [1.0 1.0; 1.0 1.0]
fill(1.0, 2, 2) = [1.0 1.0; 1.0 1.0]
similar(A) = [0 0; 0 0]
A[1, 1] = 1
A[1, :] = [1, 2]
A[1:end, 1] = [1, 3]
A * b = [5, 11]
A' = [1 3; 2 4]
dot(b, [5.0, 2.0]) = 9.0
b' * b = 5
Diagonal([1.0, 2.0]) = [1.0 0.0; 0.0 2.0]
I = UniformScaling{Bool}(true)
inv(A) = [-1.9999999999999996 0.9999999999999998; 1.4999999999999998 -0.4999999999999999]
> Git: Clone
the https://github.com/quantecon/lecture-julia.notebooks