Skip to content

Commit 9fed40a

Browse files
committed
feat: to increase precision, perform convolution with fine grid dx and then subsample to coarse grid dx
1 parent cecf691 commit 9fed40a

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

packages/treetime/src/distribution/distribution_convolution.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn convolution_function_function(
115115
let dx_a = compute_uniform_spacing(a.t())?;
116116
let dx_b = compute_uniform_spacing(b.t())?;
117117
// Use max instead of min to prevent exponentially growing grid sizes
118-
let dx = dx_a.max(dx_b);
118+
let dx = dx_a.min(dx_b);
119119

120120
if !(dx.is_finite() && dx > 0.0) {
121121
return make_error!("Invalid grid spacing detected during convolution: {dx}");
@@ -140,8 +140,28 @@ fn convolution_function_function(
140140

141141
// Perform convolution with unified grids
142142
let conv_result = convolve(dx, &a_values, &b_values)?;
143+
// check that the convolution result has the expected length
144+
if conv_result.len() != output_len {
145+
return make_error!(
146+
"Convolution result length mismatch: expected {}, got {}",
147+
output_len,
148+
conv_result.len()
149+
);
150+
}
151+
let conv_distr = DistributionFunction::new(output_grid, conv_result)?;
152+
153+
// resample distribution to coarser grid
154+
let coarse_dx = dx_a.max(dx_b);
155+
let (final_values, coarse_grid_min) = resample_distribution(&conv_distr, coarse_dx)?;
156+
let coarse_grid = ndarray_uniform_grid(coarse_grid_min, coarse_dx, final_values.len());
157+
158+
// sanity check: more than one point
159+
if coarse_grid.len() < 2 {
160+
return make_error!("Final distribution after convolution has less than two points");
161+
}
143162

144-
Distribution::function(output_grid, conv_result)
163+
// return final distribution
164+
Distribution::function(coarse_grid, final_values)
145165
}
146166

147167
fn compute_uniform_spacing(grid: &Array1<f64>) -> Result<f64, Report> {

0 commit comments

Comments
 (0)