From 973413d88b4ce0f278af3e1e2d634e75ee94fade Mon Sep 17 00:00:00 2001 From: Nikita Kishore Date: Wed, 6 Mar 2019 22:45:02 +0530 Subject: [PATCH] Added examples to docs --- src/arma.jl | 36 ++++++++++++++++++++++++++++++++++++ src/markov/mc_tools.jl | 13 +++++++++++++ src/modeltools/utility.jl | 10 ++++++++++ src/zeros.jl | 12 ++++++++++++ 4 files changed, 71 insertions(+) diff --git a/src/arma.jl b/src/arma.jl index 31216ce1..4032463b 100644 --- a/src/arma.jl +++ b/src/arma.jl @@ -139,6 +139,27 @@ and the inverse Fourier transform. - `arma::ARMA`: Instance of `ARMA` type - `;num_autocov::Integer(16)` : The number of autocovariances to calculate +##### Returns + +- `acov::Vector{Float64}`: `acov[j]` is the autocovariance between two elements with a lag j. + +##### Examples + +```julia +using Plots + +# AR(2) process +ar = ARMA([0.8 , 0.5], Vector{Float64}(),1) +ar_acov = autocovariance(ar) +plot(1:length(ar_acov),ar_acov,color=:blue, lw=2, marker=:circle, markersize=3, label = "autocovariance") + +# ARMA(2,2) process +lp = ARMA([0.8, 0.5], [0.7, 0.3], 0.5) +lp_acov = autocovariance(lp, num_autocov = 50) +plot(1:length(lp_acov),lp_acov,color=:blue, lw=2, marker=:circle, markersize=3, label = "autocovariance") + +``` + """ function autocovariance(arma::ARMA; num_autocov::Integer=16) # Compute the autocovariance function associated with ARMA process arma @@ -163,6 +184,21 @@ Get the impulse response corresponding to our model. - `psi::Vector{Float64}`: `psi[j]` is the response at lag j of the impulse response. We take `psi[1]` as unity. +##### Examples + +```julia +using Plots + +lp1 = ARMA([0.5],[0.5],1) +response1 = impulse_response(lp1) +plot(1:length(response1),response1,color=:blue, lw=2, marker=:circle, markersize=3) + +lp2 = ARMA([0.8, 0.5], [0.7, 0.3], 0.5) +response2 = impulse_response(lp2) +plot(1:length(response2),response2,color=:blue, lw=2, marker=:circle, markersize=3) + +``` + """ function impulse_response(arma::ARMA; impulse_length=30) # Compute the impulse response function associated with ARMA process arma diff --git a/src/markov/mc_tools.jl b/src/markov/mc_tools.jl index 869076aa..ad769b3c 100644 --- a/src/markov/mc_tools.jl +++ b/src/markov/mc_tools.jl @@ -27,6 +27,19 @@ state transitions. - `p::AbstractMatrix` : The transition matrix. Must be square, all elements must be nonnegative, and all rows must sum to unity. - `state_values::AbstractVector` : Vector containing the values associated with the states. + +##### Examples + +```julia +# Without labelling states +p1 = [1 0 0; 0 1 0; 0 0 1] +mc1 = MarkovChain(p1) + +#Including labels for states +p2 = [0.3 0.7; 0.25 0.75] +states = ["unemployed", "employed"] +mc2 = MarkovChain(p2, states) +``` """ mutable struct MarkovChain{T, TM<:AbstractMatrix{T}, TV<:AbstractVector} p::TM # valid stochastic matrix diff --git a/src/modeltools/utility.jl b/src/modeltools/utility.jl index 797ba7e0..b5f4319b 100644 --- a/src/modeltools/utility.jl +++ b/src/modeltools/utility.jl @@ -15,6 +15,16 @@ Additionally, this code assumes that if c < 1e-10 then u(c) = log(1e-10) + 1e10*(c - 1e-10) +##### Examples + +```julia +u1 = LogUtility() +u1(10.) +u1.([1.e-15, 1.e15]) +u2 = LogUtility(10) +u2.([10., 1.e-15]) +``` + """ struct LogUtility <: AbstractUtility ΞΎ::Float64 diff --git a/src/zeros.jl b/src/zeros.jl index 9c4f08c0..5b14b48e 100644 --- a/src/zeros.jl +++ b/src/zeros.jl @@ -114,6 +114,18 @@ sequentially with any bracketing pairs that are found. - `x1b::Vector{T}`: `Vector` of lower borders of bracketing intervals - `x2b::Vector{T}`: `Vector` of upper borders of bracketing intervals +##### Examples + +```julia +x1a, x2a = divide_bracket(sin, -100., 100.) + +function poly(x) + return x^2 - 2x + 1 +end +x1b, x2b = divide_bracket(poly, -1., 1., 100) + +``` + ##### References This is `zbrack` from Numerical Recepies Recepies in C++