| 1 |
! Copyright © 2025-2026 Dynare Team |
| 2 |
! |
| 3 |
! This file is part of Dynare. |
| 4 |
! |
| 5 |
! Dynare is free software: you can redistribute it and/or modify it under the terms of the |
| 6 |
! GNU General Public License as published by the Free Software Foundation, either version 3 of |
| 7 |
! the License, or (at your option) any later version. |
| 8 |
! |
| 9 |
! Dynare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
| 10 |
! even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
! GNU General Public License for more details. |
| 12 |
! |
| 13 |
! You should have received a copy of the GNU General Public License along with Dynare. If not, |
| 14 |
! see <https://www.gnu.org/licenses/>. |
| 15 |
! |
| 16 |
! Original author: Normann Rion <[email protected]> |
| 17 |
! |
| 18 |
! Markov chain utilities (Rouwenhorst discretization, stationary distribution) |
| 19 |
|
| 20 |
module markov |
| 21 |
use iso_fortran_env, only: real64, int32 |
| 22 |
implicit none (type, external) |
| 23 |
contains |
| 24 |
|
| 25 |
!--------------------------------------------------------------------------- |
| 26 |
! Discretize AR(1) process using Rouwenhorst method |
| 27 |
! |
| 28 |
! Computes transition matrix P_mat, stationary distribution p_vec, and |
| 29 |
! productivity grid y = exp(s) normalized so that E[y] = 1. |
| 30 |
! |
| 31 |
! Arguments: |
| 32 |
! rho [in] : AR(1) persistence parameter |
| 33 |
! sigma [in] : AR(1) innovation standard deviation |
| 34 |
! N [in] : Number of grid points |
| 35 |
! tol [in] : Tolerance for stationary distribution |
| 36 |
! maxiter [in] : Maximum iterations for stationary distribution |
| 37 |
! y [inout] : Productivity grid [N] |
| 38 |
! p_vec [inout] : Stationary distribution [N] |
| 39 |
! P_mat [inout] : Transition matrix [N × N] |
| 40 |
!--------------------------------------------------------------------------- |
| 41 |
subroutine markov_rouwenhorst(rho, sigma, N, tol, maxiter, y, p_vec, P_mat) |
| 42 |
integer(int32), intent(in) :: N, maxiter |
| 43 |
real(real64), intent(in) :: rho, sigma, tol |
| 44 |
real(real64), dimension(N), intent(inout) :: y, p_vec |
| 45 |
real(real64), dimension(N, N), intent(inout) :: P_mat |
| 46 |
|
| 47 |
integer(int32) :: i, k |
| 48 |
real(real64) :: p, scale_factor |
| 49 |
real(real64), allocatable :: P1(:, :), P2(:, :), P3(:, :), P4(:, :) |
| 50 |
real(real64), allocatable :: s(:) |
| 51 |
logical :: flag |
| 52 |
|
| 53 |
! Step 1: Initialize transition matrix for N=2 |
| 54 |
p = (1.0_real64 + rho) / 2.0_real64 |
| 55 |
P_mat(1, 1) = p |
| 56 |
P_mat(1, 2) = 1.0_real64 - p |
| 57 |
P_mat(2, 1) = 1.0_real64 - p |
| 58 |
P_mat(2, 2) = p |
| 59 |
|
| 60 |
allocate(P1(N, N), P2(N, N), P3(N, N), P4(N, N)) |
| 61 |
|
| 62 |
! Step 2: Build transition matrix for N >= 3 using recursion |
| 63 |
do k = 3, N |
| 64 |
P1 = 0.0_real64 |
| 65 |
P2 = 0.0_real64 |
| 66 |
P3 = 0.0_real64 |
| 67 |
P4 = 0.0_real64 |
| 68 |
|
| 69 |
! Construct new transition matrix |
| 70 |
P1(1:k-1, 1:k-1) = p * P_mat(1:k-1,1:k-1) |
| 71 |
P2(1:k-1, 2:k) = (1.0_real64 - p) * P_mat(1:k-1,1:k-1) |
| 72 |
P3(2:k, 1:k-1) = (1.0_real64 - p) * P_mat(1:k-1,1:k-1) |
| 73 |
P4(2:k, 2:k) = p * P_mat(1:k-1,1:k-1) |
| 74 |
P_mat(1:k,1:k) = P1(1:k,1:k) + P2(1:k,1:k) + P3(1:k,1:k) + P4(1:k,1:k) |
| 75 |
|
| 76 |
! Divide middle rows by 2 |
| 77 |
do i = 2, k - 1 |
| 78 |
P_mat(i, :) = P_mat(i, :) / 2.0_real64 |
| 79 |
end do |
| 80 |
|
| 81 |
end do |
| 82 |
|
| 83 |
deallocate(P1, P2, P3, P4) |
| 84 |
|
| 85 |
! Step 3: Compute stationary distribution p_vec |
| 86 |
call compute_stationary(P_mat, N, p_vec, tol, maxiter, flag) |
| 87 |
|
| 88 |
! Step 4: Construct the grid (Equivalent of `np.linspace(-1, 1, N)`) |
| 89 |
allocate(s(N)) |
| 90 |
do i = 1, N |
| 91 |
s(i) = -1.0_real64 + (i - 1) * (2.0_real64 / (N - 1)) |
| 92 |
end do |
| 93 |
|
| 94 |
! Step 5: Scale `s` using sigma and variance correction |
| 95 |
scale_factor = sigma / sqrt(compute_variance(s, p_vec, N)) |
| 96 |
s = s * scale_factor |
| 97 |
|
| 98 |
! Step 6: Compute y values |
| 99 |
do i = 1, N |
| 100 |
y(i) = exp(s(i)) |
| 101 |
end do |
| 102 |
|
| 103 |
! Normalize y so that sum(p_vec * y) = 1 |
| 104 |
y = y / sum(p_vec * y) |
| 105 |
|
| 106 |
deallocate(s) |
| 107 |
end subroutine markov_rouwenhorst |
| 108 |
|
| 109 |
!--------------------------------------------------------------------------- |
| 110 |
! Compute stationary distribution via power iteration: p = p·P |
| 111 |
! |
| 112 |
! Arguments: |
| 113 |
! P_mat [in] : Transition matrix [N × N] |
| 114 |
! N [in] : Number of grid points |
| 115 |
! p_vec [out] : Stationary distribution [N] |
| 116 |
! tol [in] : Convergence tolerance |
| 117 |
! maxiter [in] : Maximum iterations |
| 118 |
! converged [out] : True if converged within tolerance |
| 119 |
!--------------------------------------------------------------------------- |
| 120 |
subroutine compute_stationary(P_mat, N, p_vec, tol, maxiter, converged) |
| 121 |
integer(int32), intent(in) :: N, maxiter |
| 122 |
real(real64), intent(in) :: P_mat(N, N), tol |
| 123 |
real(real64), intent(out) :: p_vec(N) |
| 124 |
logical, intent(out) :: converged |
| 125 |
integer(int32) :: iter |
| 126 |
real(real64) :: diff, temp(N) |
| 127 |
|
| 128 |
p_vec = 1.0_real64 / real(N, real64) ! Start with a uniform guess |
| 129 |
converged = .false. |
| 130 |
|
| 131 |
do iter = 1, maxiter |
| 132 |
temp = matmul(p_vec, P_mat) |
| 133 |
diff = maxval(abs(temp - p_vec)) |
| 134 |
p_vec = temp |
| 135 |
if (diff < tol) then |
| 136 |
converged = .true. |
| 137 |
exit |
| 138 |
end if |
| 139 |
end do |
| 140 |
end subroutine compute_stationary |
| 141 |
|
| 142 |
!--------------------------------------------------------------------------- |
| 143 |
! Compute variance of discrete distribution: Var(s) = E[(s - E[s])²] |
| 144 |
!--------------------------------------------------------------------------- |
| 145 |
function compute_variance(s, p_vec, N) result(var) |
| 146 |
integer(int32), intent(in) :: N |
| 147 |
real(real64), intent(in) :: s(N), p_vec(N) |
| 148 |
real(real64) :: mean_s, var |
| 149 |
|
| 150 |
mean_s = sum(s * p_vec) |
| 151 |
var = sum(p_vec * (s - mean_s)**2) |
| 152 |
end function compute_variance |
| 153 |
|
| 154 |
end module markov |