Skip to content

Commit a1eb5b5

Browse files
committed
Add more examples
1 parent 973413d commit a1eb5b5

4 files changed

Lines changed: 143 additions & 9 deletions

File tree

src/arma.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ response1 = impulse_response(lp1)
194194
plot(1:length(response1),response1,color=:blue, lw=2, marker=:circle, markersize=3)
195195
196196
lp2 = ARMA([0.8, 0.5], [0.7, 0.3], 0.5)
197-
response2 = impulse_response(lp2)
197+
response2 = impulse_response(lp2,impulse_length = 50)
198198
plot(1:length(response2),response2,color=:blue, lw=2, marker=:circle, markersize=3)
199199
200200
```

src/markov/mc_tools.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ Indicate whether the Markov chain `mc` is irreducible.
200200
201201
- `::Bool`
202202
203+
##### Examples
204+
205+
```jlcon
206+
julia> P1 = [0 0.5 0.5; 0.3 0 0.7; 0 0 1]
207+
208+
julia> is_irreducible(MarkovChain(P1))
209+
false
210+
211+
julia> P2 = [0 1 0; 0 0 1; 1 0 0]
212+
213+
julia> is_irreducible(MarkovChain(P2))
214+
true
215+
```
216+
203217
"""
204218
is_irreducible(mc::MarkovChain) = is_strongly_connected(DiGraph(mc.p))
205219

@@ -214,6 +228,20 @@ Indicate whether the Markov chain `mc` is aperiodic.
214228
215229
- `::Bool`
216230
231+
##### Examples
232+
233+
```jlcon
234+
julia> P1 = [1 0; 0 1]
235+
236+
julia> is_aperiodic(MarkovChain(P1))
237+
true
238+
239+
julia> P1 = [0 1 0 0; 1 0 0 0; 0 0 0 1; 0 0 1 0]
240+
241+
julia> is_aperiodic(MarkovChain(P1))
242+
false
243+
```
244+
217245
"""
218246
is_aperiodic(mc::MarkovChain) = period(mc) == 1
219247

@@ -373,6 +401,23 @@ The resulting vector has the state values of `mc` as elements.
373401
374402
- `X::Vector` : Vector containing the sample path, with length
375403
ts_length
404+
405+
### Examples
406+
407+
```jlcon
408+
julia> P = [0.3 0.7; 0.75 0.25];
409+
410+
julia> mc = MarkovChain(P,["Umemployed", "Employed"]);
411+
412+
julia> simulate(mc,5,init=2)
413+
5-element Array{String,1}:
414+
"Employed"
415+
"Umemployed"
416+
"Employed"
417+
"Umemployed"
418+
"Employed"
419+
```
420+
376421
"""
377422
function simulate(mc::MarkovChain, ts_length::Int;
378423
init::Int=rand(1:n_states(mc)))
@@ -397,6 +442,26 @@ same as the type of the state values of `mc`
397442
- vector: cycle through the elements, applying each as an
398443
initial condition until all columns have an initial condition
399444
(allows for more columns than initial conditions)
445+
446+
### Examples
447+
448+
```jlcon
449+
julia> using Plots
450+
451+
julia> P = [0 0.5 0.5; 0.5 0 0.5; 0.5 0.5 0];
452+
453+
julia> mc = MarkovChain(P);
454+
455+
julia> X = zeros(15,4);
456+
457+
julia> simulate!(X,mc,init = 1);
458+
459+
julia> series = plot(1:15, X[:,1], lw=2, ylim = (0,4), label = "Sample path 1", size=(800,550))
460+
461+
julia> for i in 2:4
462+
plot!(1:15,X[:,i], lw=2, label = "Sample path \$i")
463+
end
464+
```
400465
"""
401466
function simulate!(X::Union{AbstractVector,AbstractMatrix},
402467
mc::MarkovChain; init=rand(1:n_states(mc), size(X, 2)))
@@ -427,6 +492,19 @@ The resulting vector has the indices of the state values of `mc` as elements.
427492
428493
- `X::Vector{Int}` : Vector containing the sample path, with length
429494
ts_length
495+
496+
### Examples
497+
498+
```jlcon
499+
julia> P = [0.3 0.7; 0.75 0.25];
500+
501+
julia> mc = MarkovChain(P,["Umemployed", "Employed"]);
502+
503+
julia> X = simulate_indices(mc,10,init=1);
504+
505+
julia> plot(1:10,X,lw=2,label="Sample Path",ylim=[1,2])
506+
```
507+
430508
"""
431509
function simulate_indices(mc::MarkovChain, ts_length::Int;
432510
init::Int=rand(1:n_states(mc)))

src/modeltools/utility.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@ u(c) = log(1e-10) + 1e10*(c - 1e-10)
1717
1818
##### Examples
1919
20-
```julia
21-
u1 = LogUtility()
22-
u1(10.)
23-
u1.([1.e-15, 1.e15])
24-
u2 = LogUtility(10)
25-
u2.([10., 1.e-15])
20+
```jlcon
21+
julia> u1 = LogUtility()
22+
23+
julia> u1(10.)
24+
2.302585092994046
25+
26+
julia> u1.([1.e-15, 1.e15])
27+
2-element Array{Float64,1}:
28+
-24.025840929940458
29+
34.538776394910684
30+
31+
julia> u2 = LogUtility(10)
32+
33+
julia> u2.([10., 1.e-15])
34+
2-element Array{Float64,1}:
35+
23.02585092994046
36+
-240.25840929940458
2637
```
2738
2839
"""

src/util.jl

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ ckron(arrays::AbstractArray...) = reduce(kron, arrays)
2828
2929
Repeatedly apply kronecker products to the arrays. Equilvalent to
3030
`reduce(kron, arrays)`
31+
32+
##### Example
33+
34+
```jlcon
35+
julia> A = [10 16 11; 5 3 7]
36+
37+
julia> B = B = [0.25 0.5; 0.15 0.4]
38+
39+
julia> C = [120 100; 375 240; 540 650]
40+
41+
julia> ckron(A,B,C)
42+
12×12 Array{Float64,2}:
43+
300.0 250.0 600.0 500.0 480.0 … 330.0 275.0 660.0 550.0
44+
937.5 600.0 1875.0 1200.0 1500.0 1031.25 660.0 2062.5 1320.0
45+
1350.0 1625.0 2700.0 3250.0 2160.0 1485.0 1787.5 2970.0 3575.0
46+
180.0 150.0 480.0 400.0 288.0 198.0 165.0 528.0 440.0
47+
562.5 360.0 1500.0 960.0 900.0 618.75 396.0 1650.0 1056.0
48+
810.0 975.0 2160.0 2600.0 1296.0 … 891.0 1072.5 2376.0 2860.0
49+
150.0 125.0 300.0 250.0 90.0 210.0 175.0 420.0 350.0
50+
468.75 300.0 937.5 600.0 281.25 656.25 420.0 1312.5 840.0
51+
675.0 812.5 1350.0 1625.0 405.0 945.0 1137.5 1890.0 2275.0
52+
90.0 75.0 240.0 200.0 54.0 126.0 105.0 336.0 280.0
53+
281.25 180.0 750.0 480.0 168.75 … 393.75 252.0 1050.0 672.0
54+
405.0 487.5 1080.0 1300.0 243.0 567.0 682.5 1512.0 1820.0
55+
56+
```
57+
3158
"""
3259
ckron
3360

@@ -191,12 +218,13 @@ along each dimension can be obtained by `simplex_grid(m, n) / n`.
191218
192219
##### Examples
193220
194-
>>> simplex_grid(3, 4)
195-
221+
```jlcon
222+
julia> simplex_grid(3,4)
196223
3×15 Array{Int64,2}:
197224
0 0 0 0 0 1 1 1 1 2 2 2 3 3 4
198225
0 1 2 3 4 0 1 2 3 0 1 2 0 1 0
199226
4 3 2 1 0 3 2 1 0 2 1 0 1 0 0
227+
```
200228
201229
##### References
202230
@@ -354,6 +382,23 @@ array fits within the range of `T`; a sufficient condition for it is
354382
# Returns
355383
356384
- `idx::T`: Ranking of `a`.
385+
386+
##### Examples
387+
388+
```jlcon
389+
julia> k_array_rank([2,3,5,8])
390+
42
391+
392+
julia> typeof(k_array_rank([2,3,5,8]))
393+
Int64
394+
395+
julia> typeof(k_array_rank(Int128,[2,3,5,8]))
396+
Int128
397+
398+
julia> typeof(k_array_rank(BigInt,[2,3,5,8]))
399+
BigInt
400+
```
401+
357402
"""
358403
function k_array_rank(T::Type{<:Integer}, a::Vector{<:Integer})
359404
if T != BigInt

0 commit comments

Comments
 (0)