pfjax.test
Modules:
| Name | Description |
|---|---|
models |
|
utils |
Utilities for both formal and interactive testing. |
Functions:
| Name | Description |
|---|---|
loglik_full |
Calculate the complete data loglikelihood for a state space model. |
resample_multinomial |
Particle resampler. |
sinkhorn_test |
Sinkhorn algorithm as described in Corenflos et al (2021). |
resample_multinomial_old |
Particle resampler. |
resample_mvn_for |
Particle resampler with Multivariate Normal approximation using for-loop for testing. |
particle_filter_for |
Apply particle filter for given value of |
particle_filter_rb_for |
Rao-Blackwellized particle filter. |
loglik_full_for |
Calculate the joint loglikelihood |
simulate_for |
Simulate data from the state-space model. |
param_mwg_update_for |
Parameter update by Metropolis-within-Gibbs random walk. |
particle_smooth_for |
Draw a sample from |
particle_loglik |
Calculate particle filter marginal loglikelihood. |
particle_ancestor |
Return a full particle by backtracking through ancestors of particle |
accumulate_smooth |
Accumulate expectation using the basic particle smoother. |
logw_to_prob |
Calculate normalized probabilities from unnormalized log weights. |
rm_keys |
Remove specified keys from given dict. |
tree_array2d |
Convert a PyTree into a 2D JAX array. |
tree_add |
Add two pytrees leafwise. |
tree_mean |
Weighted mean of each leaf of a pytree along leading dimension. |
tree_subset |
Subset the leading dimension of each leaf of a pytree by values in index. |
tree_zeros |
Fill pytree with zeros. |
tree_remove_last |
Remove last element of each leaf of pytree. |
tree_remove_first |
Remove first element of each leaf of pytree. |
tree_keep_last |
Keep only last element of each leaft of pytree. |
tree_append_first |
Append |
tree_append_last |
Append |
loglik_full(model, y_meas, x_state, theta)
Calculate the complete data loglikelihood for a state space model.
Calculates p(y_{0:T} | x_{0:T}, theta) * p(x_{0:T} | theta).
Notes:
- Currently ignores the prior on the initial state
x_0. - Requires
T > 0, i.e.,n_obs > 1. This could be fixed by forcingy_measandx_stateto pad with an extra dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Object specifying the state-space model having the following methods:
|
required | |
y_meas
|
The sequence of |
required | |
x_state
|
The sequence of |
required | |
theta
|
Parameter value. |
required |
Returns:
| Type | Description |
|---|---|
|
The value of the complete data loglikelihood. |
Source code in src/pfjax/loglik_full.py
resample_multinomial(key, x_particles_prev, logw)
Particle resampler.
This basic one just does a multinomial sampler, i.e., sample with replacement proportional to weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNG key. |
required | |
x_particles_prev
|
An |
required | |
logw
|
Vector of corresponding |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary with elements:
- |
Source code in src/pfjax/particle_resamplers.py
sinkhorn_test(a, b, u, v, epsilon, n_iterations, scale_cost=1.0)
Sinkhorn algorithm as described in Corenflos et al (2021).
This is for testing purposes: it returns the whole OT matrix and doesn't leverage the fixed-point algorithm for gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
First probability vector of length |
required | |
b
|
Second probability vector of length |
required | |
u
|
First particle set with leading dimension |
required | |
v
|
Second particle set with leading dimension |
required | |
epsilon
|
Regularization parameter. |
required | |
n_iterations
|
Number of Sinkhorn iterations. |
required | |
scale_cost
|
Distance matrix gets divided by this number. |
1.0
|
Returns:
| Type | Description |
|---|---|
|
A tuple with elements |
|
|
|
|
|
|
|
|
Source code in src/pfjax/test/utils.py
resample_multinomial_old(key, logw)
Particle resampler.
This basic one just does a multinomial sampler, i.e., sample with replacement proportional to weights.
Old API, to be depreciated after testing against particle_filter_for().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNG key. |
required | |
logw
|
Vector of |
required |
Returns:
| Type | Description |
|---|---|
|
Vector of |
Source code in src/pfjax/test/utils.py
resample_mvn_for(key, x_particles_prev, logw)
Particle resampler with Multivariate Normal approximation using for-loop for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNG key. |
required | |
x_particles_prev
|
An |
required | |
logw
|
Vector of corresponding |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary with elements:
- |
Source code in src/pfjax/test/utils.py
particle_filter_for(model, key, y_meas, theta, n_particles)
Apply particle filter for given value of theta.
Closely follows Algorithm 2 of Murray 2013 https://arxiv.org/abs/1306.3277.
This is the testing version which does the following:
- Uses for-loops instead of
lax.scanandvmap/xmap. - Only does basic particle sampling using
resample_multinomial_old().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Object specifying the state-space model. |
required | |
key
|
PRNG key. |
required | |
y_meas
|
The sequence of |
required | |
theta
|
Parameter value. |
required | |
n_particles
|
Number of particles. |
required |
Returns:
| Type | Description |
|---|---|
|
A dictionary with elements:
- |
Source code in src/pfjax/test/utils.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | |
particle_filter_rb_for(model, key, y_meas, theta, n_particles, resampler=resample_multinomial, score=True, fisher=False, history=False)
Rao-Blackwellized particle filter.
This is the for-loop version used only for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Object specifying the state-space model having the following methods:
- |
required | |
key
|
PRNG key. |
required | |
y_meas
|
JAX array with leading dimension |
required | |
theta
|
Parameter value. |
required | |
n_particles
|
Number of particles. |
required | |
resampler
|
Function used at step |
resample_multinomial
|
|
score
|
Whether or not to return an estimate of the score function at |
True
|
|
fisher
|
Whether or not to return an estimate of the Fisher information at |
False
|
|
history
|
Whether to output the history of the filter or only the last step. |
False
|
Returns:
| Type | Description |
|---|---|
|
A dictionary with elements:
- |
Source code in src/pfjax/test/utils.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
loglik_full_for(model, y_meas, x_state, theta)
Calculate the joint loglikelihood p(y_{0:T} | x_{0:T}, theta) * p(x_{0:T} | theta).
For-loop version for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Object specifying the state-space model. |
required | |
y_meas
|
The sequence of |
required | |
x_state
|
The sequence of |
required | |
theta
|
Parameter value. |
required |
Returns:
| Type | Description |
|---|---|
|
The value of the loglikelihood. |
Source code in src/pfjax/test/utils.py
simulate_for(model, key, n_obs, x_init, theta)
Simulate data from the state-space model.
FIXME: This is the testing version which uses a for-loop. This should be put in a separate class in a test subfolder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Object specifying the state-space model. |
required | |
key
|
PRNG key. |
required | |
n_obs
|
Number of observations to generate. |
required | |
x_init
|
Initial state value at time |
required | |
theta
|
Parameter value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
y_meas |
The sequence of measurement variables |
|
x_state |
The sequence of state variables |
Source code in src/pfjax/test/utils.py
param_mwg_update_for(model, prior, key, theta, x_state, y_meas, rw_sd, theta_order)
Parameter update by Metropolis-within-Gibbs random walk.
Version for testing using for-loops.
Notes:
- Assumes the parameters are real valued. Next step might be to provide a parameter validator to the model.
- Potentially wastes an initial evaluation of
loglik_full(theta). Could be passed in from a previous calculation but a bit cumbersome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Object specifying the state-space model. |
required | |
prior
|
Object specifying the parameter prior. |
required | |
key
|
PRNG key. |
required | |
theta
|
Current parameter vector. |
required | |
x_state
|
The sequence of |
required | |
y_meas
|
The sequence of |
required | |
rw_sd
|
Vector of length |
required | |
theta_order
|
Vector of integers between 0 and |
required |
Returns:
| Name | Type | Description |
|---|---|---|
theta_out |
Updated parameter vector. |
|
accept |
Boolean vector of size |
Source code in src/pfjax/test/utils.py
particle_smooth_for(key, logw, x_particles, ancestors, n_sample=1)
Draw a sample from p(x_state | x_meas, theta) using the basic particle smoothing algorithm.
For-loop version for testing.
Source code in src/pfjax/test/utils.py
particle_loglik(logw)
Calculate particle filter marginal loglikelihood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logw
|
An |
required |
Returns:
| Type | Description |
|---|---|
|
Particle filter approximation of |
|
|
``` |
|
|
log p(y_meas | theta) = log int p(y_meas | x_state, theta) * p(x_state | theta) dx_state |
|
|
``` |
Source code in src/pfjax/test/utils.py
particle_ancestor(x_particles, ancestors, id_particle_last)
Return a full particle by backtracking through ancestors of particle i_part at last time point.
Differs from the version in the pfjax.particle.filter module in that the latter does random sampling whereas here the index of the final particle is fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_particles
|
JAX array with leading dimensions |
required | |
ancestors
|
JAX integer array of shape |
required | |
id_particle_last
|
Index of the particle at the last time point |
required |
Returns:
| Type | Description |
|---|---|
|
A JAX array with leading dimension |
Source code in src/pfjax/test/utils.py
accumulate_smooth(logw, x_particles, ancestors, y_meas, theta, accumulator, mean=True)
Accumulate expectation using the basic particle smoother.
Performs exactly the same calculation as the accumulator in particle_accumulator(), except by smoothing the particle history instead of directly in the filter step (no history required).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logw
|
JAX array of shape |
required | |
x_particles
|
JAX array with leading dimensions |
required | |
ancestors
|
JAX integer array of shape |
required | |
y_meas
|
JAX array with leading dimension |
required | |
theta
|
Parameter value. |
required | |
accumulator
|
Function with argument signature |
required | |
mean
|
Whether or not to compute the weighted average of the accumulated values, or to return a Pytree with each leaf having leading dimension |
True
|
Returns:
| Type | Description |
|---|---|
|
A Pytree of accumulated values. |
Source code in src/pfjax/test/utils.py
logw_to_prob(logw)
Calculate normalized probabilities from unnormalized log weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logw
|
Vector of |
required |
Returns:
| Type | Description |
|---|---|
|
Vector of |
Source code in src/pfjax/utils.py
rm_keys(x, keys)
tree_array2d(tree, shape0=None)
Convert a PyTree into a 2D JAX array.
Starts by converting each leaf array to a 2D JAX array with same leading dimension. Then concatenates these arrays along axis=1. Assumes the leading dimension of each leaf is the same.
Notes:
- This function returns a tuple containing a Callable, so can't be jitted directly. Can however be called in jitted code so long as the output is a PyTree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree
|
A Pytree. |
required | |
shape0
|
Optional value of the leading dimension. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
|
|
|
||
|
Source code in src/pfjax/utils.py
tree_add(tree1, tree2)
tree_mean(tree, logw)
Weighted mean of each leaf of a pytree along leading dimension.
Source code in src/pfjax/utils.py
tree_subset(tree, index)
tree_zeros(tree)
tree_remove_last(tree)
tree_remove_first(tree)
tree_keep_last(tree)
tree_append_first(tree, first)
Append first to start of each leaf of tree along 1st dimension.
tree_append_last(tree, last)
Append last to end of each leaf of tree along 1st dimension.