-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Remove API for regular likelihood #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -306,23 +306,31 @@ impl EquationPriv for SDE { | |||||||||||||
| //e = y[t] .- x[:,1] | ||||||||||||||
| // q = pdf.(Distributions.Normal(0, 0.5), e) | ||||||||||||||
| if let Some(em) = error_models { | ||||||||||||||
| let mut q: Vec<f64> = Vec::with_capacity(self.nparticles); | ||||||||||||||
| // Compute log-likelihoods for each particle | ||||||||||||||
| let mut log_q: Vec<f64> = Vec::with_capacity(self.nparticles); | ||||||||||||||
|
|
||||||||||||||
| pred.iter().for_each(|p| { | ||||||||||||||
| let lik = p.likelihood(em); | ||||||||||||||
| match lik { | ||||||||||||||
| Ok(l) => q.push(l), | ||||||||||||||
| Err(e) => panic!("Error in likelihood calculation: {:?}", e), | ||||||||||||||
| let log_lik = p.log_likelihood(em); | ||||||||||||||
| match log_lik { | ||||||||||||||
| Ok(l) => log_q.push(l), | ||||||||||||||
| Err(e) => panic!("Error in log-likelihood calculation: {:?}", e), | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| let sum_q: f64 = q.iter().sum(); | ||||||||||||||
| let w: Vec<f64> = q.iter().map(|qi| qi / sum_q).collect(); | ||||||||||||||
|
|
||||||||||||||
| // Use log-sum-exp trick for numerical stability | ||||||||||||||
| let max_log_q = log_q.iter().cloned().fold(f64::NEG_INFINITY, f64::max); | ||||||||||||||
| let sum_exp: f64 = log_q.iter().map(|&lq| (lq - max_log_q).exp()).sum(); | ||||||||||||||
| let log_sum_q = max_log_q + sum_exp.ln(); | ||||||||||||||
|
|
||||||||||||||
| // Compute normalized weights from log-likelihoods | ||||||||||||||
| let w: Vec<f64> = log_q.iter().map(|&lq| (lq - log_sum_q).exp()).collect(); | ||||||||||||||
| let i = sysresample(&w); | ||||||||||||||
| let a: Vec<DVector<f64>> = i.iter().map(|&i| x[i].clone()).collect(); | ||||||||||||||
| *x = a; | ||||||||||||||
| likelihood.push(sum_q / self.nparticles as f64); | ||||||||||||||
| // let qq: Vec<f64> = i.iter().map(|&i| q[i]).collect(); | ||||||||||||||
| // likelihood.push(qq.iter().sum::<f64>() / self.nparticles as f64); | ||||||||||||||
|
|
||||||||||||||
| // Push the average likelihood (in regular space) for final computation | ||||||||||||||
| // log(mean(likelihood)) = log(sum(exp(log_lik))) - log(n) | ||||||||||||||
| likelihood.push((log_sum_q - (self.nparticles as f64).ln()).exp()); | ||||||||||||||
|
Comment on lines
+331
to
+333
|
||||||||||||||
| // Push the average likelihood (in regular space) for final computation | |
| // log(mean(likelihood)) = log(sum(exp(log_lik))) - log(n) | |
| likelihood.push((log_sum_q - (self.nparticles as f64).ln()).exp()); | |
| // Push the average log-likelihood for final computation | |
| // log(mean(likelihood)) = log(sum(exp(log_lik))) - log(n) | |
| likelihood.push(log_sum_q - (self.nparticles as f64).ln()); |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function _estimate_likelihood_no_cache is called but never defined in this module or elsewhere in the codebase. This will cause a compilation error. You need to either define this function or change the implementation to use simulate_subject directly without caching, similar to how it's done for the cached version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inline comment correctly describes returning log-likelihood, which matches the implementation. However, the function's documentation comment at line 231 (outside the changed region) still says "optional likelihood" and should be updated in a follow-up to say "optional log-likelihood" for consistency.