|
1 |
| -// convolve two vectors as a backwards dot product |
2 |
| -// y vector should be reversed |
3 |
| -// limited to the length of x and backwards looking for x indexes |
| 1 | +/** |
| 2 | + * Calculate convolution indices for the case where s <= xlen |
| 3 | + * |
| 4 | + * @param s Current position in the output vector |
| 5 | + * @param xlen Length of the x vector |
| 6 | + * @param ylen Length of the y vector |
| 7 | + * @return An array of integers: {start_x, end_x, start_y, end_y} |
| 8 | + */ |
| 9 | +array[] int calc_conv_indices_xlen(int s, int xlen, int ylen) { |
| 10 | + int s_minus_ylen = s - ylen; |
| 11 | + int start_x = max(1, s_minus_ylen + 1); |
| 12 | + int end_x = s; |
| 13 | + int start_y = max(1, 1 - s_minus_ylen); |
| 14 | + int end_y = ylen; |
| 15 | + return {start_x, end_x, start_y, end_y}; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Calculate convolution indices for the case where s > xlen |
| 20 | + * |
| 21 | + * @param s Current position in the output vector |
| 22 | + * @param xlen Length of the x vector |
| 23 | + * @param ylen Length of the y vector |
| 24 | + * @return An array of integers: {start_x, end_x, start_y, end_y} |
| 25 | + */ |
| 26 | +array[] int calc_conv_indices_len(int s, int xlen, int ylen) { |
| 27 | + int s_minus_ylen = s - ylen; |
| 28 | + int start_x = max(1, s_minus_ylen + 1); |
| 29 | + int end_x = xlen; |
| 30 | + int start_y = max(1, 1 - s_minus_ylen);; |
| 31 | + int end_y = ylen + xlen - s; |
| 32 | + return {start_x, end_x, start_y, end_y}; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Convolve a vector with a reversed probability mass function. |
| 37 | + * |
| 38 | + * This function performs a discrete convolution of two vectors, where the second vector |
| 39 | + * is assumed to be an already reversed probability mass function. |
| 40 | + * |
| 41 | + * @param x The input vector to be convolved. |
| 42 | + * @param y The already reversed probability mass function vector. |
| 43 | + * @param len The desired length of the output vector. |
| 44 | + * @return A vector of length `len` containing the convolution result. |
| 45 | + * @throws If `len` is not of equal length to the sum of the lengths of `x` and `y`. |
| 46 | + */ |
4 | 47 | vector convolve_with_rev_pmf(vector x, vector y, int len) {
|
5 |
| - int xlen = num_elements(x); |
6 |
| - int ylen = num_elements(y); |
7 |
| - vector[len] z; |
8 |
| - if (xlen + ylen <= len) { |
9 |
| - reject("convolve_with_rev_pmf: len is longer then x and y combined"); |
10 |
| - } |
11 |
| - for (s in 1:len) { |
12 |
| - z[s] = dot_product( |
13 |
| - x[max(1, (s - ylen + 1)):min(s, xlen)], |
14 |
| - y[max(1, ylen - s + 1):min(ylen, ylen + xlen - s)] |
15 |
| - ); |
| 48 | + int xlen = num_elements(x); |
| 49 | + int ylen = num_elements(y); |
| 50 | + vector[len] z; |
| 51 | + |
| 52 | + if (xlen + ylen - 1 < len) { |
| 53 | + reject("convolve_with_rev_pmf: len is longer than x and y convolved"); |
| 54 | + } |
| 55 | + |
| 56 | + if (xlen > len) { |
| 57 | + reject("convolve_with_rev_pmf: len is shorter than x"); |
| 58 | + } |
| 59 | + |
| 60 | + for (s in 1:xlen) { |
| 61 | + array[4] int indices = calc_conv_indices_xlen(s, xlen, ylen); |
| 62 | + z[s] = dot_product(x[indices[1]:indices[2]], y[indices[3]:indices[4]]); |
| 63 | + } |
| 64 | + |
| 65 | + if (len > xlen) { |
| 66 | + for (s in (xlen + 1):len) { |
| 67 | + array[4] int indices = calc_conv_indices_len(s, xlen, ylen); |
| 68 | + z[s] = dot_product(x[indices[1]:indices[2]], y[indices[3]:indices[4]]); |
16 | 69 | }
|
17 |
| - return(z); |
18 | 70 | }
|
| 71 | + |
| 72 | + return z; |
| 73 | +} |
19 | 74 |
|
20 |
| - |
21 |
| -// convolve latent infections to reported (but still unobserved) cases |
| 75 | +/** |
| 76 | + * Convolve infections to reported cases. |
| 77 | + * |
| 78 | + * This function convolves a vector of infections with a reversed delay |
| 79 | + * distribution to produce a vector of reported cases. |
| 80 | + * |
| 81 | + * @param infections A vector of infection counts. |
| 82 | + * @param delay_rev_pmf A vector representing the reversed probability mass |
| 83 | + * function of the delay distribution. |
| 84 | + * @param seeding_time The number of initial time steps to exclude from the |
| 85 | + * output. |
| 86 | + * @return A vector of reported cases, starting from `seeding_time + 1`. |
| 87 | + */ |
22 | 88 | vector convolve_to_report(vector infections,
|
23 | 89 | vector delay_rev_pmf,
|
24 | 90 | int seeding_time) {
|
25 | 91 | int t = num_elements(infections);
|
26 |
| - vector[t - seeding_time] reports; |
27 |
| - vector[t] unobs_reports = infections; |
28 | 92 | int delays = num_elements(delay_rev_pmf);
|
29 |
| - if (delays) { |
30 |
| - unobs_reports = convolve_with_rev_pmf(unobs_reports, delay_rev_pmf, t); |
31 |
| - reports = unobs_reports[(seeding_time + 1):t]; |
32 |
| - } else { |
33 |
| - reports = infections[(seeding_time + 1):t]; |
| 93 | + |
| 94 | + if (delays == 0) { |
| 95 | + return infections[(seeding_time + 1):t]; |
34 | 96 | }
|
35 |
| - return(reports); |
| 97 | + |
| 98 | + vector[t] unobs_reports = convolve_with_rev_pmf(infections, delay_rev_pmf, t); |
| 99 | + return unobs_reports[(seeding_time + 1):t]; |
36 | 100 | }
|
0 commit comments