| 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 |
! Linear interpolation with bracket search for policy functions |
| 19 |
|
| 20 |
module interpolation |
| 21 |
use iso_fortran_env, only: real64, int32 |
| 22 |
use calibration_types, only: gray_code_cache |
| 23 |
use gray_code, only: compute_linear_indices |
| 24 |
implicit none (type, external) |
| 25 |
contains |
| 26 |
subroutine bracket_linear_weight(x, n, xq, nq, xqi, xqpi) |
| 27 |
!----------------------------------------------------------------------- |
| 28 |
! Subroutine: bracket_linear_weight |
| 29 |
! |
| 30 |
! Purpose: |
| 31 |
! For a given monotonic increasing grid `x` and a vector of query |
| 32 |
! points `xq`, computes for each query: |
| 33 |
! - the index `xqi` such that xq ∈ [x(xqi), x(xqi+1)] |
| 34 |
! - the linear interpolation weight `xqpi` for xqi |
| 35 |
! |
| 36 |
! This allows for fast and robust piecewise linear interpolation. |
| 37 |
! |
| 38 |
! Arguments: |
| 39 |
! x (in) [real64, dimension(n)] |
| 40 |
! Monotonically increasing 1D grid. |
| 41 |
! |
| 42 |
! n (in) [int32] |
| 43 |
! Number of elements in x (length of x). |
| 44 |
! |
| 45 |
! xq (in) [real64, dimension(nq)] |
| 46 |
! Query points to locate in the grid. |
| 47 |
! |
| 48 |
! nq (in) [int32] |
| 49 |
! Number of query points (length of xq). |
| 50 |
! |
| 51 |
! xqi (out) [int32, dimension(nq)] |
| 52 |
! For each xq(i), the index `ilow` such that: |
| 53 |
! x(ilow) ≤ xq(i) < x(ilow+1) |
| 54 |
! |
| 55 |
! xqpi (out) [real64, dimension(nq)] |
| 56 |
! For each xq(i), the relative weight in the bracketed interval: |
| 57 |
! xqpi(i) = (x(ilow+1) - xq(i)) / (x(ilow+1) - x(ilow)) |
| 58 |
! |
| 59 |
! Notes: |
| 60 |
! - Extrapolation is clamped to the first or last interval. |
| 61 |
! - Grid `x` must be strictly increasing for meaningful output. |
| 62 |
! |
| 63 |
!----------------------------------------------------------------------- |
| 64 |
|
| 65 |
integer(int32), intent(in) :: n, nq |
| 66 |
real(real64), intent(in) :: x(n), xq(nq) |
| 67 |
integer(int32), intent(out) :: xqi(nq) |
| 68 |
real(real64), intent(out) :: xqpi(nq) |
| 69 |
|
| 70 |
integer(int32) :: iq, ilow, ihigh, imid |
| 71 |
|
| 72 |
do iq = 1, nq |
| 73 |
if (xq(iq) < x(1)) then |
| 74 |
ilow = 1 |
| 75 |
xqi(iq) = ilow |
| 76 |
xqpi(iq) = 1.0_real64 |
| 77 |
else if (xq(iq) >= x(n)) then |
| 78 |
! Clamp to last interval [x(n-1), x(n)] with weight 0 on lower point |
| 79 |
! This ensures ilow+1 = n is valid for interpolation |
| 80 |
ilow = n - 1 |
| 81 |
xqi(iq) = ilow |
| 82 |
xqpi(iq) = 0.0_real64 |
| 83 |
else |
| 84 |
ilow = 1 |
| 85 |
ihigh = n |
| 86 |
do while (ihigh - ilow > 1) |
| 87 |
imid = (ihigh + ilow) / 2 |
| 88 |
if (xq(iq) >= x(imid)) then |
| 89 |
ilow = imid |
| 90 |
else |
| 91 |
ihigh = imid |
| 92 |
end if |
| 93 |
end do |
| 94 |
xqi(iq) = ilow |
| 95 |
xqpi(iq) = (x(ilow+1) - xq(iq)) / (x(ilow+1) - x(ilow)) |
| 96 |
end if |
| 97 |
end do |
| 98 |
end subroutine bracket_linear_weight |
| 99 |
|
| 100 |
subroutine interpolate(out, xq) |
| 101 |
type(gray_code_cache), target, intent(inout) :: out |
| 102 |
real(real64), contiguous, target, intent(in) :: xq(:,:) ! N_x × (N_e ⋅ N_a_sp) |
| 103 |
|
| 104 |
! Useful local variables |
| 105 |
integer(int32) :: n, M, N_a_om, N_a_sp, a, kf, t |
| 106 |
real(real64), pointer, contiguous :: mat(:,:) => null(), v(:,:) => null() |
| 107 |
N_a_om = size(out%is_hard_one, 1) |
| 108 |
n = size(out%is_hard_one, 2) |
| 109 |
M = size(out%acc, 1) / N_a_om ! M = N_x ⋅ N_e |
| 110 |
N_a_sp = size(xq, 2) / (M / size(xq,1)) |
| 111 |
mat(1:M, 1:N_a_om) => out%acc |
| 112 |
v(1:M, 1:N_a_sp) => xq |
| 113 |
|
| 114 |
! Initialization at the low corner |
| 115 |
out%linear_idx = out%corner_idx |
| 116 |
out%sk = .false. |
| 117 |
out%z = count(out%is_hard_zero, dim=2) |
| 118 |
out%beta = out%beta_0 |
| 119 |
do concurrent (a=1:N_a_om) |
| 120 |
if (out%z(a) == 0_int32) then |
| 121 |
mat(:,a) = out%beta(a)*v(:,out%linear_idx(a)) |
| 122 |
else |
| 123 |
mat(:,a) = 0.0_real64 |
| 124 |
end if |
| 125 |
end do |
| 126 |
|
| 127 |
! Gray code traversal over state dimensions for the other corners |
| 128 |
do t = 1, out%Kcorn - 1 |
| 129 |
kf = out%flip_idx(t) |
| 130 |
if (.not. out%sk(kf)) then |
| 131 |
! Lower -> upper on dimension kf |
| 132 |
out%linear_idx = out%linear_idx + out%stride_states(kf) |
| 133 |
out%sk(kf) = .true. |
| 134 |
do concurrent (a=1:N_a_om) |
| 135 |
if (out%is_hard_one(a,kf)) then |
| 136 |
! For nodes with a hard-one kf dim, the number of hard-one dims flipped |
| 137 |
! to upper increases |
| 138 |
out%z(a) = out%z(a)+1_int32 |
| 139 |
else if (out%is_hard_zero(a,kf)) then |
| 140 |
! For nodes with a hard-zero kf dim, the number of hard-zero dims flipped |
| 141 |
! to lower decreases |
| 142 |
out%z(a) = out%z(a)-1_int32 |
| 143 |
else |
| 144 |
! For nodes with a soft kf dim, we update the beta |
| 145 |
! coefficient |
| 146 |
out%beta(a) = out%beta(a)*out%r_up(a,kf) |
| 147 |
end if |
| 148 |
end do |
| 149 |
else |
| 150 |
! Upper -> lower on dimension kf |
| 151 |
out%linear_idx = out%linear_idx - out%stride_states(kf) |
| 152 |
out%sk(kf) = .false. |
| 153 |
do concurrent (a=1:N_a_om) |
| 154 |
if (out%is_hard_one(a,kf)) then |
| 155 |
! For nodes with a hard-one kf dim, the number of hard-one dims flipped |
| 156 |
! to upper decreases |
| 157 |
out%z(a) = out%z(a)-1_int32 |
| 158 |
else if (out%is_hard_zero(a,kf)) then |
| 159 |
! For nodes with a hard-zero kf dim, the number of hard-zero dims flipped |
| 160 |
! to lower increases |
| 161 |
out%z(a) = out%z(a)+1_int32 |
| 162 |
else |
| 163 |
! For nodes with a soft kf dim, we update the beta |
| 164 |
! coefficient |
| 165 |
out%beta(a) = out%beta(a)*out%r_down(a,kf) |
| 166 |
end if |
| 167 |
end do |
| 168 |
end if |
| 169 |
do a = 1,N_a_om |
| 170 |
if (out%z(a) == 0_int32) mat(:,a) = mat(:,a) + out%beta(a)*v(:,out%linear_idx(a)) |
| 171 |
end do |
| 172 |
end do |
| 173 |
|
| 174 |
end subroutine interpolate |
| 175 |
|
| 176 |
end module interpolation |