From f10e3c24e06dcc89caf889fa8a53db729ee52ed6 Mon Sep 17 00:00:00 2001 From: Carl Date: Tue, 31 Oct 2023 22:37:30 +0000 Subject: [PATCH] handle case of all new_wt being effectively 0 --- R/functions.R | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/R/functions.R b/R/functions.R index e044a51..32aa7e3 100644 --- a/R/functions.R +++ b/R/functions.R @@ -87,7 +87,7 @@ smooth.params <- function(params,h=1){ return(params.star) } -##` Particile filter +##` Particle filter ##` Updates state and parameter weights based on likelihood of the data ##` Will resample-move if effective sample size drops to <50% ##` @@ -113,14 +113,20 @@ ParticleFilter <- function(out,params,dat,wt=1){ } mult = mult*2 } - wt = like * wt ## update weights + new_wt = like * wt ## update weights + if ( all(new_wt == 0) ){ + new_wt = (like/sum(like)) * (wt / sum(wt)) + } + wt = new_wt ## hist(wt,main="Ensemble Weights") ## useful diagnostic if you're running line-by-line ## calculate effective sample size wtn = wt/sum(wt) ## normalized weights Neff = 1/sum(wtn^2) + + ## check if effective size has dropped below 50% threshold if(Neff < ne/2){ ## resample ensemble members in proportion to their weight