using InstantiateFromURL github_project("QuantEcon/quantecon-notebooks-julia", version = "0.2.0") using LinearAlgebra, Statistics randn() using Plots gr(fmt=:png); # setting for easier display in jupyter notebooks n = 100 ϵ = randn(n) plot(1:n, ϵ) typeof(ϵ) ϵ[1:5] # poor style n = 100 ϵ = zeros(n) for i in 1:n ϵ[i] = randn() end # better style n = 100 ϵ = zeros(n) for i in eachindex(ϵ) ϵ[i] = randn() end ϵ_sum = 0.0 # careful to use 0.0 here, instead of 0 m = 5 for ϵ_val in ϵ[1:m] ϵ_sum = ϵ_sum + ϵ_val end ϵ_mean = ϵ_sum / m ϵ_mean ≈ mean(ϵ[1:m]) ϵ_mean ≈ sum(ϵ[1:m]) / m # poor style function generatedata(n) ϵ = zeros(n) for i in eachindex(ϵ) ϵ[i] = (randn())^2 # squaring the result end return ϵ end data = generatedata(10) plot(data) # still poor style function generatedata(n) ϵ = randn(n) # use built in function for i in eachindex(ϵ) ϵ[i] = ϵ[i]^2 # squaring the result end return ϵ end data = generatedata(5) # better style function generatedata(n) ϵ = randn(n) # use built in function return ϵ.^2 end data = generatedata(5) # good style generatedata(n) = randn(n).^2 data = generatedata(5) # good style f(x) = x^2 # simple square function generatedata(n) = f.(randn(n)) # uses broadcast for some function `f` data = generatedata(5) generatedata(n, gen) = gen.(randn(n)) # uses broadcast for some function `gen` f(x) = x^2 # simple square function data = generatedata(5, f) # applies f # direct solution with broadcasting, and small user-defined function n = 100 f(x) = x^2 x = randn(n) plot(f.(x), label="x^2") plot!(x, label="x") # layer on the same plot using Distributions function plothistogram(distribution, n) ϵ = rand(distribution, n) # n draws from distribution histogram(ϵ) end lp = Laplace() plothistogram(lp, 500) rand(3) # poor style p = 1.0 # note 1.0 rather than 1 β = 0.9 maxiter = 1000 tolerance = 1.0E-7 v_iv = 0.8 # initial condition # setup the algorithm v_old = v_iv normdiff = Inf iter = 1 while normdiff > tolerance && iter <= maxiter v_new = p + β * v_old # the f(v) map normdiff = norm(v_new - v_old) # replace and continue v_old = v_new iter = iter + 1 end println("Fixed point = $v_old, and |f(x) - x| = $normdiff in $iter iterations") # setup the algorithm v_old = v_iv normdiff = Inf iter = 1 for i in 1:maxiter v_new = p + β * v_old # the f(v) map normdiff = norm(v_new - v_old) if normdiff < tolerance # check convergence iter = i break # converged, exit loop end # replace and continue v_old = v_new end println("Fixed point = $v_old, and |f(x) - x| = $normdiff in $iter iterations") # better, but still poor style function v_fp(β, ρ, v_iv, tolerance, maxiter) # setup the algorithm v_old = v_iv normdiff = Inf iter = 1 while normdiff > tolerance && iter <= maxiter v_new = p + β * v_old # the f(v) map normdiff = norm(v_new - v_old) # replace and continue v_old = v_new iter = iter + 1 end return (v_old, normdiff, iter) # returns a tuple end # some values p = 1.0 # note 1.0 rather than 1 β = 0.9 maxiter = 1000 tolerance = 1.0E-7 v_initial = 0.8 # initial condition v_star, normdiff, iter = v_fp(β, p, v_initial, tolerance, maxiter) println("Fixed point = $v_star, and |f(x) - x| = $normdiff in $iter iterations") # better style function fixedpointmap(f, iv, tolerance, maxiter) # setup the algorithm x_old = iv normdiff = Inf iter = 1 while normdiff > tolerance && iter <= maxiter x_new = f(x_old) # use the passed in map normdiff = norm(x_new - x_old) x_old = x_new iter = iter + 1 end return (x_old, normdiff, iter) end # define a map and parameters p = 1.0 β = 0.9 f(v) = p + β * v # note that p and β are used in the function! maxiter = 1000 tolerance = 1.0E-7 v_initial = 0.8 # initial condition v_star, normdiff, iter = fixedpointmap(f, v_initial, tolerance, maxiter) println("Fixed point = $v_star, and |f(x) - x| = $normdiff in $iter iterations") # good style function fixedpointmap(f; iv, tolerance=1E-7, maxiter=1000) # setup the algorithm x_old = iv normdiff = Inf iter = 1 while normdiff > tolerance && iter <= maxiter x_new = f(x_old) # use the passed in map normdiff = norm(x_new - x_old) x_old = x_new iter = iter + 1 end return (value = x_old, normdiff=normdiff, iter=iter) # A named tuple end # define a map and parameters p = 1.0 β = 0.9 f(v) = p + β * v # note that p and β are used in the function! sol = fixedpointmap(f, iv=0.8, tolerance=1.0E-8) # don't need to pass println("Fixed point = $(sol.value), and |f(x) - x| = $(sol.normdiff) in $(sol.iter)"* " iterations") r = 2.0 f(x) = r * x * (1 - x) sol = fixedpointmap(f, iv=0.8) println("Fixed point = $(sol.value), and |f(x) - x| = $(sol.normdiff) in $(sol.iter) iterations") # best style using NLsolve p = 1.0 β = 0.9 f(v) = p .+ β * v # broadcast the + sol = fixedpoint(f, [0.8]) println("Fixed point = $(sol.zero), and |f(x) - x| = $(norm(f(sol.zero) - sol.zero)) in " * "$(sol.iterations) iterations") # best style p = 1.0 β = 0.9 iv = [0.8] sol = fixedpoint(v -> p .+ β * v, iv) println("Fixed point = $(sol.zero), and |f(x) - x| = $(norm(f(sol.zero) - sol.zero)) in " * "$(sol.iterations) iterations") eps() # use arbitrary precision floating points p = 1.0 β = 0.9 iv = [BigFloat(0.8)] # higher precision # otherwise identical sol = fixedpoint(v -> p .+ β * v, iv) println("Fixed point = $(sol.zero), and |f(x) - x| = $(norm(f(sol.zero) - sol.zero)) in " * "$(sol.iterations) iterations") p = [1.0, 2.0] β = 0.9 iv = [0.8, 2.0] f(v) = p .+ β * v # note that p and β are used in the function! sol = fixedpointmap(f, iv = iv, tolerance = 1.0E-8) println("Fixed point = $(sol.value), and |f(x) - x| = $(sol.normdiff) in $(sol.iter)"* "iterations") using NLsolve p = [1.0, 2.0, 0.1] β = 0.9 iv =[0.8, 2.0, 51.0] f(v) = p .+ β * v sol = fixedpoint(v -> p .+ β * v, iv) println("Fixed point = $(sol.zero), and |f(x) - x| = $(norm(f(sol.zero) - sol.zero)) in " * "$(sol.iterations) iterations") using NLsolve, StaticArrays p = @SVector [1.0, 2.0, 0.1] β = 0.9 iv = @SVector [0.8, 2.0, 51.0] f(v) = p .+ β * v sol = fixedpoint(v -> p .+ β * v, iv) println("Fixed point = $(sol.zero), and |f(x) - x| = $(norm(f(sol.zero) - sol.zero)) in " * "$(sol.iterations) iterations") using ForwardDiff # operator to get the derivative of this function using AD D(f) = x -> ForwardDiff.derivative(f, x) # example usage: create a function and get the derivative f(x) = x^2 f_prime = D(f) f(0.1), f_prime(0.1) function factorial2(n) k = 1 for i in 1:n k *= i # or k = k * i end return k end factorial2(4) factorial2(4) == factorial(4) # built-in function function binomial_rv(n, p) count = 0 U = rand(n) for i in 1:n if U[i] < p count += 1 # or count = count + 1 end end return count end for j in 1:25 b = binomial_rv(10, 0.5) print("$b, ") end n = 1000000 count = 0 for i in 1:n u, v = rand(2) d = sqrt((u - 0.5)^2 + (v - 0.5)^2) # distance from middle of square if d < 0.5 count += 1 end end area_estimate = count / n print(area_estimate * 4) # dividing by radius**2 payoff = 0 count = 0 print("Count = ") for i in 1:10 U = rand() if U < 0.5 count += 1 else count = 0 end print(count) if count == 3 payoff = 1 end end println("\npayoff = $payoff") a = 1 < 2 ? "foo" : "bar" a = 1 > 2 ? "foo" : "bar" payoff = 0.0 count = 0.0 print("Count = ") for i in 1:10 U = rand() count = U < 0.5 ? count + 1 : 0 print(count) if count == 3 payoff = 1 end end println("\npayoff = $payoff") using Plots gr(fmt=:png); # setting for easier display in jupyter notebooks α = 0.9 n = 200 x = zeros(n + 1) for t in 1:n x[t+1] = α * x[t] + randn() end plot(x) αs = [0.0, 0.8, 0.98] n = 200 p = plot() # naming a plot to add to for α in αs x = zeros(n + 1) x[1] = 0.0 for t in 1:n x[t+1] = α * x[t] + randn() end plot!(p, x, label = "alpha = $α") # add to plot p end p # display plot function drawsuntilthreshold(threshold; maxdraws=100) for i in 1:maxdraws val = rand() if val < threshold # checks threshold return i # leaves function, returning draw number end end return Inf # if here, reached maxdraws end draws = drawsuntilthreshold(0.2, maxdraws=100) vals = zeros(0) # empty vector for i in 1:100 val = rand() if val < 0.5 push!(vals, val) end end println("There were $(length(vals)) below 0.5")