| 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 |
! Distribution derivative computation (curly D matrices) |
| 19 |
|
| 20 |
module curlyDs |
| 21 |
use iso_fortran_env, only: real64, int32 |
| 22 |
use gray_code |
| 23 |
use blas |
| 24 |
implicit none (type, external) |
| 25 |
|
| 26 |
contains |
| 27 |
|
| 28 |
!--------------------------------------------------------------------------- |
| 29 |
! Compute distribution derivative matrix (curly D) for one-state case |
| 30 |
! |
| 31 |
! Computes D(i,j',s) = Σⱼ (∂Bⁱ)ⱼ',ⱼ · vᵢ,ⱼ,ₖ,ₛ · ωᵢ,ⱼ |
| 32 |
! then D(i',j',s) = Σᵢ μᵢ',ᵢ · D(i,j',s) |
| 33 |
! |
| 34 |
! Arguments: |
| 35 |
! D [out] : Distribution derivative [N_om × S] |
| 36 |
! ind [in] : Bracket indices [N_om × 1] |
| 37 |
! inv_h [in] : Inverse grid spacing [N_om × 1] |
| 38 |
! a_hat [in] : Policy function derivatives [N_om × S × 1] |
| 39 |
! om [in] : Distribution [N_om] |
| 40 |
! Mu [in] : Transition matrix [N_e × N_e] |
| 41 |
!--------------------------------------------------------------------------- |
| 42 |
subroutine compute_curlyDs_1d(D, ind, inv_h, a_hat, om, Mu) |
| 43 |
! Inputs/Outputs |
| 44 |
real(real64), contiguous, target, intent(out) :: D(:,:) |
| 45 |
real(real64), contiguous, intent(in) :: inv_h(:,:), a_hat(:,:,:), Mu(:,:), om(:) |
| 46 |
integer(int32), contiguous, intent(in) :: ind(:,:) |
| 47 |
|
| 48 |
! Local variables |
| 49 |
real(real64) :: inc |
| 50 |
real(real64), allocatable, target :: acc(:,:) |
| 51 |
real(real64), pointer, contiguous :: D_mat(:,:), D_rhs(:,:) |
| 52 |
integer(int32) :: N_om, N_e, N_a, S, i, j, k, m, a, a_next |
| 53 |
|
| 54 |
! Get dimensions |
| 55 |
N_e = size(Mu, 1, int32) |
| 56 |
N_om = size(D, 1, int32) |
| 57 |
S = size(D, 2, int32) |
| 58 |
N_a = N_om / N_e |
| 59 |
|
| 60 |
allocate(acc(N_om,S)) |
| 61 |
acc = 0.0_real64 |
| 62 |
do i=1,N_e |
| 63 |
do concurrent (k=1:S, j=1:N_a) |
| 64 |
m = (j-1)*N_e + i |
| 65 |
a = (ind(i+(j-1)*N_e,1)-1)*N_e + i |
| 66 |
a_next = a+N_e |
| 67 |
inc = inv_h(m,1)*om(m)*a_hat(m,k,1) |
| 68 |
acc(a,k) = acc(a,k) - inc |
| 69 |
acc(a_next,k) = acc(a_next,k) + inc |
| 70 |
end do |
| 71 |
end do |
| 72 |
D_rhs(1:N_e,1:(N_a*S)) => acc |
| 73 |
D_mat(1:N_e,1:(N_a*S)) => D |
| 74 |
call matmul_add("T", "N", 1.0_real64, Mu, D_rhs, 0.0_real64, D_mat) |
| 75 |
|
| 76 |
end subroutine compute_curlyDs_1d |
| 77 |
|
| 78 |
!--------------------------------------------------------------------------- |
| 79 |
! Compute distribution derivative matrix (curly D) for n-state case |
| 80 |
! |
| 81 |
! Uses Gray code traversal for efficient hypercube corner iteration. |
| 82 |
! Handles hard boundaries (weight = 0 or 1) with special cases. |
| 83 |
! |
| 84 |
! Arguments: |
| 85 |
! D [out] : Distribution derivative [N_om × S] |
| 86 |
! ind [in] : Multi-dimensional bracket indices [N_om × n] |
| 87 |
! w [in] : Interpolation weights [N_om × n] |
| 88 |
! inv_h [in] : Inverse grid spacing [N_om × n] |
| 89 |
! a_hat [in] : Policy function derivatives [N_om × S × n] |
| 90 |
! om [in] : Distribution [N_om] |
| 91 |
! mu [in] : Transition matrix [N_e × N_e] |
| 92 |
! dims [in] : Grid dimensions for each state [n] |
| 93 |
!--------------------------------------------------------------------------- |
| 94 |
subroutine compute_curlyDs_nd(D, ind, w, inv_h, a_hat, om, mu, dims) |
| 95 |
! Inputs/Outpus |
| 96 |
real(real64), contiguous, target, intent(out) :: D(:,:) |
| 97 |
real(real64), contiguous, intent(in) :: a_hat(:,:,:), w(:,:), inv_h(:,:), mu(:,:), om(:) |
| 98 |
integer(int32), contiguous, intent(in) :: ind(:,:), dims(:) |
| 99 |
|
| 100 |
! Local variables |
| 101 |
real(real64), allocatable, target :: acc(:,:) |
| 102 |
real(real64), allocatable :: beta_k(:), beta_0(:), inc(:), gamma(:), rhs(:,:), r_up(:,:), r_down(:,:) |
| 103 |
real(real64), pointer, contiguous :: D_mat(:,:) |
| 104 |
integer(int32) :: N_om, N_e, N_a, S, n, Kcorn, t, i, j, k, l, m, kf, z, lin |
| 105 |
integer(int32), allocatable :: flip_idx(:), a(:) |
| 106 |
integer(int32), allocatable, target :: stride(:) |
| 107 |
logical, allocatable :: sk(:), is_hard_one(:,:), is_hard_zero(:,:) |
| 108 |
|
| 109 |
N_om = size(D, 1, int32) |
| 110 |
S = size(D, 2, int32) |
| 111 |
n = size(dims, 1, int32) |
| 112 |
N_e = size(mu, 1, int32) |
| 113 |
N_a = N_om / N_e |
| 114 |
|
| 115 |
! Generate Gray code flip indices |
| 116 |
call generate_flip_indices(n, flip_idx, Kcorn) |
| 117 |
|
| 118 |
! ---- Strides for state-only linear indices (1..N_a) ---- |
| 119 |
call compute_strides(dims, stride) |
| 120 |
|
| 121 |
! Low-corner linear indices in 1, ..., N_a |
| 122 |
allocate(a(N_om)) |
| 123 |
call compute_linear_indices(ind, stride, a) |
| 124 |
|
| 125 |
! Fetch it into 1, ..., N_om |
| 126 |
do concurrent (j=1:N_a, i=1:N_e) |
| 127 |
a((j-1_int32)*N_e+i) = (a((j-1_int32)*N_e+i)-1)*N_e+i |
| 128 |
end do |
| 129 |
|
| 130 |
! Coefficient updates and hard dimensions |
| 131 |
allocate(r_up(N_om, n), r_down(N_om, n), is_hard_one(N_om, n), is_hard_zero(N_om, n)) |
| 132 |
call compute_coefficient_updates(w, r_up, r_down, is_hard_one, is_hard_zero) |
| 133 |
|
| 134 |
! Low corner initial weight |
| 135 |
allocate(beta_0(N_om)) |
| 136 |
beta_0 = product(w, dim=2, mask=(.not. is_hard_zero)) |
| 137 |
|
| 138 |
! ---- Expectations ---- |
| 139 |
allocate(sk(n), beta_k(N_om), acc(S,N_om), inc(S), gamma(N_om)) |
| 140 |
acc = 0.0_real64 |
| 141 |
stride = stride*N_e |
| 142 |
|
| 143 |
do k=1, n |
| 144 |
beta_k = inv_h(:,k)*beta_0*om |
| 145 |
where (.not. is_hard_zero(:,k)) beta_k = beta_k / w(:,k) |
| 146 |
do i=1, N_e |
| 147 |
do j=1,N_a |
| 148 |
sk = .false. |
| 149 |
lin = (j-1)*N_e + i |
| 150 |
z = count(is_hard_zero(lin,:)) |
| 151 |
m = a(lin) |
| 152 |
inc = -beta_k(lin)*a_hat(lin,:,k) |
| 153 |
if (z == 0_int32) acc(:,m) = acc(:,m) + inc |
| 154 |
do t=1, Kcorn-1 |
| 155 |
kf = flip_idx(t) |
| 156 |
if (kf==k) then |
| 157 |
inc = -inc |
| 158 |
if (.not. sk(kf)) then |
| 159 |
! lower -> upper on dim kf |
| 160 |
m = m+stride(kf) |
| 161 |
sk(kf) = .true. |
| 162 |
else |
| 163 |
! upper -> lower on dim kf |
| 164 |
m = m-stride(kf) |
| 165 |
sk(kf) = .false. |
| 166 |
end if |
| 167 |
else |
| 168 |
if (.not. sk(kf)) then |
| 169 |
! lower -> upper on dim kf |
| 170 |
m = m + stride(kf) |
| 171 |
sk(kf) = .true. |
| 172 |
if (is_hard_one(lin,kf)) then |
| 173 |
! No contribution. The number of |
| 174 |
! hard-one dims flipped to upper increases |
| 175 |
z = z+1_int32 |
| 176 |
else if (is_hard_zero(lin,kf)) then |
| 177 |
! No contribution. The number of |
| 178 |
! hard-zero dims flipped to lower decreases |
| 179 |
z = z-1_int32 |
| 180 |
else |
| 181 |
! Both sides are higher than 0, but |
| 182 |
! there is a contribution if and only |
| 183 |
! if no hard dims is flipped to upper |
| 184 |
inc = inc*r_up(lin,kf) |
| 185 |
end if |
| 186 |
else |
| 187 |
! upper -> lower on dim kf |
| 188 |
m = m - stride(kf) |
| 189 |
sk(kf) = .false. |
| 190 |
if (is_hard_one(lin,kf)) then |
| 191 |
! The number of hard-one dims flipped to upper decreases |
| 192 |
z = z-1_int32 |
| 193 |
else if (is_hard_zero(lin,kf)) then |
| 194 |
! The number of hard-zero dims flipped to lower increases |
| 195 |
z = z+1_int32 |
| 196 |
else |
| 197 |
! Both sides are higher than 0, but |
| 198 |
! there is a contribution if and only |
| 199 |
! if no hard dims is flipped to upper |
| 200 |
inc = inc*r_down(lin,kf) |
| 201 |
end if |
| 202 |
end if |
| 203 |
end if |
| 204 |
if (z==0_int32) acc(:,m) = acc(:,m)+inc |
| 205 |
end do |
| 206 |
end do |
| 207 |
end do |
| 208 |
end do |
| 209 |
|
| 210 |
! Weighting with the Markov kernel |
| 211 |
allocate(rhs(S*N_a,N_e)) |
| 212 |
do concurrent(l=1:S,j=1:N_a,i=1:N_e) |
| 213 |
rhs((l-1)*N_a+j,i) = acc(l,(j-1)*N_e+i) |
| 214 |
end do |
| 215 |
D_mat(1:N_e,1:(N_a*S))=>D |
| 216 |
call matmul_add("T", "T", 1.0_real64, Mu, rhs, 0.0_real64, D_mat) |
| 217 |
|
| 218 |
end subroutine compute_curlyDs_nd |
| 219 |
|
| 220 |
end module curlyDs |