| 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 |
! Expected value and derivative computation for policy functions |
| 19 |
|
| 20 |
module expectations |
| 21 |
use iso_fortran_env, only: real64, int32 |
| 22 |
use blas |
| 23 |
use gray_code |
| 24 |
implicit none (type, external) |
| 25 |
|
| 26 |
contains |
| 27 |
|
| 28 |
! Compute 𝔼[x̄ₐ|θ,a], one-state case |
| 29 |
subroutine compute_expected_dx_1d(E, x, mu, ind, inv_h) |
| 30 |
! Inputs/Outpus |
| 31 |
real(real64), contiguous, intent(out) :: E(:,:,:) |
| 32 |
real(real64), contiguous, intent(in), target :: x(:,:) |
| 33 |
real(real64), contiguous, intent(in) :: mu(:,:), inv_h(:,:) |
| 34 |
integer(int32), contiguous, intent(in) :: ind(:,:) |
| 35 |
|
| 36 |
! Useful variables |
| 37 |
real(real64), allocatable :: Y_flat(:,:) |
| 38 |
real(real64), target, allocatable :: Dx_flat(:,:) |
| 39 |
real(real64), pointer, contiguous :: x_bar_dash(:,:,:) |
| 40 |
integer(int32) :: N_x, N_sp, N_a, N_e, i, a, j, j_a, j_e |
| 41 |
|
| 42 |
! Sizes |
| 43 |
N_x = size(x,1,int32) |
| 44 |
N_sp = size(x,2,int32) |
| 45 |
N_e = size(mu,1,int32) |
| 46 |
N_a = N_sp / N_e |
| 47 |
|
| 48 |
! Reshape of x |
| 49 |
x_bar_dash(1:N_x,1:N_e,1:N_a) => x |
| 50 |
|
| 51 |
! First difference w.r.t the state |
| 52 |
allocate(Dx_flat(N_x*(N_a-1),N_e)) |
| 53 |
do concurrent (j=1:N_a-1, i=1:N_x) |
| 54 |
Dx_flat((j-1)*N_x+i,:) = x_bar_dash(i,:,j+1)-x_bar_dash(i,:,j) |
| 55 |
end do |
| 56 |
|
| 57 |
! Weighting with the transition matrix |
| 58 |
allocate(Y_flat(N_x*(N_a-1), N_e)) |
| 59 |
call matmul_add("N", "T", 1.0_real64, Dx_flat, mu, 0.0_real64, Y_flat) |
| 60 |
|
| 61 |
! Final calculation |
| 62 |
do concurrent (j_a=1:N_a, j_e=1:N_e) |
| 63 |
j = (j_a-1)*N_e+j_e |
| 64 |
a = ind(j,1) |
| 65 |
E(:,1,j) = inv_h(j,1) * Y_flat((a-1)*N_x+1:a*N_x, j_e) |
| 66 |
end do |
| 67 |
|
| 68 |
end subroutine compute_expected_dx_1d |
| 69 |
|
| 70 |
! Compute 𝔼[x̄ₐ|θ,a], n-state case |
| 71 |
subroutine compute_expected_dx_nd(E, x, mu, ind, w, inv_h, dims) |
| 72 |
! Inputs/Outpus |
| 73 |
real(real64), contiguous, intent(out) :: E(:,:,:) |
| 74 |
real(real64), contiguous, intent(in), target :: x(:,:), w(:,:) |
| 75 |
real(real64), contiguous, intent(in) :: mu(:,:), inv_h(:,:) |
| 76 |
integer(int32), contiguous, intent(in), target :: ind(:,:) |
| 77 |
integer(int32), contiguous, intent(in) :: dims(:) |
| 78 |
|
| 79 |
! Useful variables |
| 80 |
real(real64) :: beta |
| 81 |
real(real64), pointer, contiguous :: x_bar_dash(:,:,:) |
| 82 |
real(real64), allocatable :: Y(:,:), beta_0(:), beta_k(:), r_up(:,:), r_down(:,:), x_bar_dash_mat(:,:) |
| 83 |
integer(int32) :: N_x, N_sp, N_a, N_e, n, Kcorn, t, i, j_a, j_e, j, k, l_a, kf, z |
| 84 |
integer(int32), allocatable :: flip_idx(:), a(:), stride(:) |
| 85 |
logical, allocatable :: is_hard_one(:,:), is_hard_zero(:,:), sk(:) |
| 86 |
|
| 87 |
N_x = size(x,1,int32) |
| 88 |
N_sp = size(x,2,int32) |
| 89 |
N_e = size(mu,1,int32) |
| 90 |
N_a = N_sp / N_e |
| 91 |
n = size(E,2,int32) |
| 92 |
|
| 93 |
! Useful reshape of x |
| 94 |
x_bar_dash(1:N_x,1:N_e,1:N_a) => x |
| 95 |
|
| 96 |
! Generate flip indices using Gray code |
| 97 |
call generate_flip_indices(n, flip_idx) |
| 98 |
Kcorn = ishft(1, n) |
| 99 |
|
| 100 |
! ---- Strides for state-only linear indices (1..N_a) ---- |
| 101 |
call compute_strides(dims, stride) |
| 102 |
|
| 103 |
! Low-corner linear indices in 1, ..., N_a |
| 104 |
allocate(a(N_sp)) |
| 105 |
call compute_linear_indices(ind, stride, a) |
| 106 |
|
| 107 |
! Coefficient updates and hard dimensions |
| 108 |
allocate(r_up(N_sp, n), r_down(N_sp, n), is_hard_one(N_sp, n), is_hard_zero(N_sp, n)) |
| 109 |
call compute_coefficient_updates(w, r_up, r_down, is_hard_one, is_hard_zero) |
| 110 |
|
| 111 |
! Low corner initial weight |
| 112 |
allocate(beta_0(N_sp)) |
| 113 |
beta_0 = product(w, dim=2, mask=(.not. is_hard_zero)) |
| 114 |
|
| 115 |
! Weighting with the transition matrix |
| 116 |
allocate(Y(N_x*N_a,N_e), x_bar_dash_mat(N_x*N_a,N_e)) |
| 117 |
do concurrent (j_a=1:N_a,j_e=1:N_e,i=1:N_x) |
| 118 |
x_bar_dash_mat((j_a-1)*N_x+i,j_e) = x_bar_dash(i,j_e,j_a) |
| 119 |
end do |
| 120 |
call matmul_add("N", "T", 1.0_real64, x_bar_dash_mat, mu, 0.0_real64, Y) |
| 121 |
|
| 122 |
! ---- Expectations ---- |
| 123 |
allocate(sk(n), beta_k(N_sp)) |
| 124 |
do k=1, n |
| 125 |
beta_k = beta_0 |
| 126 |
where (.not. is_hard_zero(:,k)) beta_k = beta_k / w(:,k) |
| 127 |
do j_a=1,N_a |
| 128 |
do j_e=1,N_e |
| 129 |
! do concurrent (j_a=1:N_a, j_e=1:N_e) |
| 130 |
! Linear index |
| 131 |
j = (j_a-1)*N_e+j_e |
| 132 |
|
| 133 |
! Initialization |
| 134 |
beta = -inv_h(j,k)*beta_k(j) |
| 135 |
sk = .false. |
| 136 |
z = count(is_hard_zero(j,:)) |
| 137 |
l_a = a(j) |
| 138 |
if (z == 0_int32) then |
| 139 |
E(:,k,j) = beta*Y((l_a-1)*N_x+1:l_a*N_x,j_e) |
| 140 |
else |
| 141 |
E(:,k,j) = 0.0_real64 |
| 142 |
end if |
| 143 |
|
| 144 |
! Remaining corners |
| 145 |
do t=1, Kcorn-1 |
| 146 |
kf = flip_idx(t) |
| 147 |
if (kf == k) then |
| 148 |
beta = -beta |
| 149 |
if (.not. sk(kf)) then |
| 150 |
! lower -> upper on dim kf |
| 151 |
l_a = l_a + stride(kf) |
| 152 |
sk(kf) = .true. |
| 153 |
else |
| 154 |
! upper -> lower |
| 155 |
l_a = l_a - stride(kf) |
| 156 |
sk(kf) = .false. |
| 157 |
end if |
| 158 |
else |
| 159 |
if (.not. sk(kf)) then |
| 160 |
! lower -> upper on dim kf |
| 161 |
l_a = l_a + stride(kf) |
| 162 |
sk(kf) = .true. |
| 163 |
if (is_hard_one(j,kf)) then |
| 164 |
! No contribution. The number of |
| 165 |
! hard-one dims flipped to upper increases |
| 166 |
z = z+1_int32 |
| 167 |
else if (is_hard_zero(j,kf)) then |
| 168 |
! No contribution. The number of |
| 169 |
! hard-zero dims flipped to lower decreases |
| 170 |
z = z-1_int32 |
| 171 |
else |
| 172 |
! Both sides are higher than 0, but |
| 173 |
! there is a contribution if and only |
| 174 |
! if no hard dims is flipped to upper |
| 175 |
beta = beta*r_up(j,kf) |
| 176 |
end if |
| 177 |
else |
| 178 |
! upper -> lower on dim kf |
| 179 |
l_a = l_a - stride(kf) |
| 180 |
sk(kf) = .false. |
| 181 |
if (is_hard_one(j,kf)) then |
| 182 |
z = z-1_int32 |
| 183 |
else if (is_hard_zero(j,kf)) then |
| 184 |
z = z+1_int32 |
| 185 |
else |
| 186 |
! Both sides are higher than 0, but |
| 187 |
! there is a contribution if and only |
| 188 |
! if no hard dims is flipped to upper |
| 189 |
beta = beta*r_down(j,kf) |
| 190 |
end if |
| 191 |
end if |
| 192 |
end if |
| 193 |
if (z==0_int32) E(:,k,j) = E(:,k,j)+beta*Y((l_a-1)*N_x+1:l_a*N_x,j_e) |
| 194 |
end do |
| 195 |
end do |
| 196 |
end do |
| 197 |
end do |
| 198 |
end subroutine compute_expected_dx_nd |
| 199 |
|
| 200 |
subroutine compute_expected_y(E, y, mu, ind, w, dims) |
| 201 |
! Inputs/Outpus |
| 202 |
real(real64), contiguous, intent(out) :: E(:,:) |
| 203 |
real(real64), contiguous, intent(in), target :: y(:,:) |
| 204 |
real(real64), contiguous, intent(in) :: mu(:,:), w(:,:) |
| 205 |
integer(int32), contiguous, intent(in), target :: ind(:,:) |
| 206 |
integer(int32), contiguous, intent(in) :: dims(:) |
| 207 |
|
| 208 |
! Local variables |
| 209 |
real(real64), pointer, contiguous :: y_ptr(:,:) => null() |
| 210 |
real(real64), allocatable, target :: y_mat(:,:) |
| 211 |
real(real64), allocatable :: beta(:), r_up(:,:), r_down(:,:) |
| 212 |
integer(int32) :: n, N_e, N_a, N_om, S, Kcorn, t, kf, i, j, m |
| 213 |
integer(int32), allocatable :: flip_idx(:), z(:), stride(:), a(:) |
| 214 |
logical, allocatable :: sk(:), is_hard_one(:,:), is_hard_zero(:,:) |
| 215 |
|
| 216 |
! Size variables |
| 217 |
n = size(dims, 1, int32) |
| 218 |
N_e = size(mu, 1, int32) |
| 219 |
N_om = size(y, 1, int32) |
| 220 |
N_a = N_om/N_e |
| 221 |
S = size(y, 2, int32) |
| 222 |
|
| 223 |
! Generate flip indices using Gray code |
| 224 |
call generate_flip_indices(n, flip_idx) |
| 225 |
Kcorn = ishft(1, n) |
| 226 |
|
| 227 |
! ---- Strides for state-only linear indices (1..N_a) ---- |
| 228 |
call compute_strides(dims, stride) |
| 229 |
|
| 230 |
! Low-corner linear indices in 1, ..., N_a |
| 231 |
allocate(a(N_om)) |
| 232 |
call compute_linear_indices(ind, stride, a) |
| 233 |
|
| 234 |
! Fetch it into 1, ..., N_om |
| 235 |
do concurrent (j=1:N_a, i=1:N_e) |
| 236 |
a((j-1_int32)*N_e+i) = (a((j-1_int32)*N_e+i)-1)*N_e+i |
| 237 |
end do |
| 238 |
|
| 239 |
! Coefficient updates and hard dimensions |
| 240 |
allocate(r_up(N_om, n), r_down(N_om, n), is_hard_one(N_om, n), is_hard_zero(N_om, n)) |
| 241 |
call compute_coefficient_updates(w, r_up, r_down, is_hard_one, is_hard_zero) |
| 242 |
|
| 243 |
! Low corner initial weight |
| 244 |
allocate(beta(N_om)) |
| 245 |
beta = product(w, dim=2, mask=(.not. is_hard_zero)) |
| 246 |
|
| 247 |
! Weighting with the Mu matrix |
| 248 |
y_ptr(1:N_e,1:(N_a*S))=>y |
| 249 |
allocate(y_mat(N_e,N_a*S)) |
| 250 |
call matmul_add("N", "N", 1.0_real64, mu, y_ptr, 0.0_real64, y_mat) |
| 251 |
y_ptr(1:N_om,1:S)=>y_mat |
| 252 |
|
| 253 |
! Computes ℰ(i, j, s) = sum_{i'} sum_{j'} [ ∏_ℓ (B^ℓ)_{j'_ℓ, i, j} ] * |
| 254 |
! y_{i', j', s} * μ_{i,i'}. Note that the i and j dimensions are merged |
| 255 |
! in the following calculation |
| 256 |
allocate(sk(n),z(N_om)) |
| 257 |
|
| 258 |
! Initialization at the low corner |
| 259 |
sk = .false. |
| 260 |
z = count(is_hard_zero, dim=2) |
| 261 |
do concurrent (j=1:N_om, m=1:S) |
| 262 |
if (z(j) == 0_int32) then |
| 263 |
E(j,m) = beta(j)*y_ptr(a(j),m) |
| 264 |
else |
| 265 |
E(j,m) = 0.0_real64 |
| 266 |
end if |
| 267 |
end do |
| 268 |
stride = stride * N_e |
| 269 |
|
| 270 |
! Going through the remaining corners |
| 271 |
do t=1,Kcorn-1 |
| 272 |
kf = flip_idx(t) |
| 273 |
if (.not. sk(kf)) then |
| 274 |
! lower -> upper on dim kf |
| 275 |
a = a + stride(kf) |
| 276 |
sk(kf) = .true. |
| 277 |
do concurrent (j=1:N_om) |
| 278 |
if (is_hard_one(j,kf)) then |
| 279 |
! For nodes with a hard-one kf dim, the number of hard dims flipped |
| 280 |
! to upper increases |
| 281 |
z(j) = z(j)+1_int32 |
| 282 |
else if (is_hard_zero(j,kf)) then |
| 283 |
! For nodes with a hard-zero kf dim, the number of hard dims flipped |
| 284 |
! to lower decreases |
| 285 |
z(j) = z(j)-1_int32 |
| 286 |
else |
| 287 |
! For nodes with a soft kf dim, we update the beta |
| 288 |
! coefficient |
| 289 |
beta(j) = beta(j)*r_up(j,kf) |
| 290 |
end if |
| 291 |
end do |
| 292 |
else |
| 293 |
! upper -> lower on dim kf |
| 294 |
a = a - stride(kf) |
| 295 |
sk(kf) = .false. |
| 296 |
do concurrent (j=1:N_om) |
| 297 |
if (is_hard_one(j,kf)) then |
| 298 |
! For nodes with a hard-one kf dim, the number of hard dims flipped |
| 299 |
! to upper decreases |
| 300 |
z(j) = z(j)-1_int32 |
| 301 |
else if (is_hard_zero(j,kf)) then |
| 302 |
! For nodes with a hard-zero kf dim, the number of hard dims flipped |
| 303 |
! to lower increases |
| 304 |
z(j) = z(j)+1_int32 |
| 305 |
else |
| 306 |
! For nodes with a soft kf dim, we update the beta |
| 307 |
! coefficient |
| 308 |
beta(j) = beta(j)*r_down(j,kf) |
| 309 |
end if |
| 310 |
end do |
| 311 |
end if |
| 312 |
do concurrent (j=1:N_om) |
| 313 |
if (z(j) == 0_int32) then |
| 314 |
do m=1,S |
| 315 |
E(j,m) = E(j,m) + beta(j) * y_ptr(a(j),m) |
| 316 |
end do |
| 317 |
end if |
| 318 |
end do |
| 319 |
end do |
| 320 |
|
| 321 |
end subroutine compute_expected_y |
| 322 |
|
| 323 |
end module expectations |