Commit eb1183a
authored
PyMC/PyTensor Implementation of Pathfinder VI (#387)
* renamed samples argument name and pathfinder variables to avoid confusion
* Minor changes made to the `fit_pathfinder` function and added test
`fit_pathfinder`
- Edited `fit_pathfinder` to produce `pathfinder_state`, `pathfinder_info`, `pathfinder_samples` and `pathfinder_idata` for closer examination of the outputs.
- Changed the `num_samples` argument name to `num_draws` to avoid `TypeError` got multiple values for keyword argument 'num_samples'.
- Initial points are automatically set to jitter as jitter is required for pathfinder.
Extras
- New function 'get_jaxified_logp_ravel_inputs' to simplify previous code structure in fit_pathfinder.
Tests
- Added extra test for pathfinder to test pathfinder_info variables and pathfinder_idata are consistent for a given random seed.
* extract additional pathfinder objects from high level API for debugging
* changed pathfinder samples argument to num_draws
* feat(pathfinder): add PyMC-based Pathfinder VI implementation
Add a new PyMC-based implementation of Pathfinder VI that uses PyTensor operations which provides support for both PyMC and BlackJAX backends in fit_pathfinder.
* Multipath Pathfinder VI implementation in pymc-experimental
- Implemented in to support running multiple Pathfinder instances in parallel.
- Implemented function in for Pareto Smoothed Importance Resampling (PSIR).
- Moved relevant pathfinder files into the directory.
- Updated tests to reflect changes in the Pathfinder implementation and added tests for new functionalities.
* Added type hints and epsilon parameter to fit_pathfinder
* Removed initial point values (l=0) to reduce iterations. Simplified and .
* Added placeholder/reminder to remove jax dependency when converting trace data to InferenceData
* Sync updates with draft PR #386. \n- Added pytensor.function for bfgs_sample
* Reduced size of compute graph with pathfinder_body_fn
Summaryh of changes:
- Remove multiprocessing code in favour of reusing compiled for each path
- takes only random_seed as argument for each path
- Compute graph significantly smaller by using pure pytensor op and symoblic variables
- Added LBFGSOp to compile with pytensor.function
- Cleaned up codes using pytensor variables
* - Added TODO comments for implementing Taylor approximation methods: and .
- Corrected the dimensions in comments for matrices Q and R in the function.
- Uumerical stability in the calculation by changing from to .
* fix: correct posterior approximations in Pathfinder VI
Fixed incorrect and inconsistent posterior approximations in the Pathfinder VI
algorithm by:
1. Adding missing parentheses in the phi calculation to ensure proper order
of operations in matrix multiplications
2. Changing the sign in mu calculation from 'x +' to 'x -' to match Stan's
implementation (which differs from the original paper)
The resulting changes now make the posterior approximations more reliable.
* feat: Add dense BFGS sampling for Pathfinder VI
Implements both sparse and dense BFGS sampling approaches for Pathfinder VI:
- Adds bfgs_sample_dense for cases where 2*maxcor >= num_params.
- Moved existing and computations to bfgs_sample_sparse, making the sparse use cases more explicit.
Other changes:
- Sets default maxcor=5 instead of dynamic sizing based on parameters
Dense approximations are recommended when the target distribution has higher dependencies among the parameters.
* feat: improve Pathfinder performance and compatibility
Bigger changes:
- Made pmx.fit compatible with method='pathfinder'
- Remove JAX dependency when inference_backend='pymc' to support Windows users
- Improve runtime performance by setting trust_input=True for compiled functions
Minor changes:
- Change default num_paths from 1 to 4 for stable and reliable approximations
- Change LBFGS code using dataclasses
- Update tests to handle both PyMC and BlackJAX backends
* minor: improve error handling in Pathfinder VI
- Add LBFGSInitFailed exception for failed LBFGS initialisation
- Skip failed paths in multipath_pathfinder and track number of failures
- Handle NaN values from Cholesky decompsition in bfgs_sample
- Add checks for numericl stabilty in matrix operations
Slight performance improvements:
- Set allow_gc=False in scan ops
- Use FAST_RUN mode consistently
* Progress bar and other minor changes
Major:
- Added progress bar support.
Minor
- Added exception for non-finite log prob values
- Removed .
- Allowed maxcor argument to be None, and dynamically set based on the number of model parameters.
- Improved logging to inform users about failed paths and lbfgs initialisation.
* set maxcor to max(5, floor(N / 1.9)). max=1 will cause error
* Refactor Pathfinder VI: Default to PSIS, Add Concurrency, and Improved Computational Performance
- Significantly computational efficiency by combining 3 computational graphs into 1 larger compile. Removed non-shared inputs and used with for significant performance gains.
- Set default importance sampling method to 'psis' for more stable posterior results, avoiding local peaks seen with 'psir'.
- Introduce concurrency options ('thread' and 'process') for multithreading and multiprocessing. Defaults to No concurrency as there haven't been any/or much reduction to the compute time.
- Adjusted default from 8 to 4 and from 1.0 to 2.0 and maxcor to max(3*log(N), 5). This default setting lessens computational time and and the degree by which the posterior variance is being underestimated.
* Improvements to Importance Sampling and InferenceData shape
- Handle different importance sampling methods for reshaping and adjusting log densities.
- Modified to return InferenceData with chain dim of size num_paths when
* Display summary of results, Improve error handling, General improvements
Changes:
- Add rich table summary display for results
- Added PathStatus and LBFGSStatus for error handling, status tracking and displaying results
- Changed importance_sampling return type to ImportanceSamplingResult
- Changed multipath_pathfinder return type to MultiPathfinderResult
- Added dataclass containers for results (ImportanceSamplingResult, PathfinderResult, MultiPathfinderResult)
- Refactored LBFGS by removing PyTensor Op classes in favor of pure functions
- Added timing and configuration tracking
- Improve concurrency with better error handling
- Improved docstrings and type hints
- Simplified logp and gradient computation by combining into single function
- Added compile_kwargs parameter for pytensor compilation options
* Move pathfinder module to pymc_extras
- Move pathfinder module from pymc_experimental to pymc_extras
- Update directory structure to match upstream repository
* Improve pathfinder error handling and type hints
- Add proper type hints throughout pathfinder module
- Improve error handling in concurrent execution paths
- Better handling of when all paths are fail by displaying results before Assertion
- Changed Australian English spelling to US
- Update compile_pymc usage to handle deprecation warning
- Add tests for concurrent execution and seed reproducibility
- Clean up imports and remove redundant code
- Improve docstrings and error messages
* fix: Use typing_extensions.Self for Python 3.10 compatibility1 parent 96f7a2e commit eb1183a
File tree
7 files changed
+2213
-145
lines changed- pymc_extras/inference
- pathfinder
- tests
7 files changed
+2213
-145
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
| |||
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | 33 | | |
38 | 34 | | |
39 | 35 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
0 commit comments