pymc-extras 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. pymc_extras/__init__.py +29 -0
  2. pymc_extras/distributions/__init__.py +40 -0
  3. pymc_extras/distributions/continuous.py +351 -0
  4. pymc_extras/distributions/discrete.py +399 -0
  5. pymc_extras/distributions/histogram_utils.py +163 -0
  6. pymc_extras/distributions/multivariate/__init__.py +3 -0
  7. pymc_extras/distributions/multivariate/r2d2m2cp.py +446 -0
  8. pymc_extras/distributions/timeseries.py +356 -0
  9. pymc_extras/gp/__init__.py +18 -0
  10. pymc_extras/gp/latent_approx.py +183 -0
  11. pymc_extras/inference/__init__.py +18 -0
  12. pymc_extras/inference/find_map.py +431 -0
  13. pymc_extras/inference/fit.py +44 -0
  14. pymc_extras/inference/laplace.py +570 -0
  15. pymc_extras/inference/pathfinder.py +134 -0
  16. pymc_extras/inference/smc/__init__.py +13 -0
  17. pymc_extras/inference/smc/sampling.py +451 -0
  18. pymc_extras/linearmodel.py +130 -0
  19. pymc_extras/model/__init__.py +0 -0
  20. pymc_extras/model/marginal/__init__.py +0 -0
  21. pymc_extras/model/marginal/distributions.py +276 -0
  22. pymc_extras/model/marginal/graph_analysis.py +372 -0
  23. pymc_extras/model/marginal/marginal_model.py +595 -0
  24. pymc_extras/model/model_api.py +56 -0
  25. pymc_extras/model/transforms/__init__.py +0 -0
  26. pymc_extras/model/transforms/autoreparam.py +434 -0
  27. pymc_extras/model_builder.py +759 -0
  28. pymc_extras/preprocessing/__init__.py +0 -0
  29. pymc_extras/preprocessing/standard_scaler.py +17 -0
  30. pymc_extras/printing.py +182 -0
  31. pymc_extras/statespace/__init__.py +13 -0
  32. pymc_extras/statespace/core/__init__.py +7 -0
  33. pymc_extras/statespace/core/compile.py +48 -0
  34. pymc_extras/statespace/core/representation.py +438 -0
  35. pymc_extras/statespace/core/statespace.py +2268 -0
  36. pymc_extras/statespace/filters/__init__.py +15 -0
  37. pymc_extras/statespace/filters/distributions.py +453 -0
  38. pymc_extras/statespace/filters/kalman_filter.py +820 -0
  39. pymc_extras/statespace/filters/kalman_smoother.py +126 -0
  40. pymc_extras/statespace/filters/utilities.py +59 -0
  41. pymc_extras/statespace/models/ETS.py +670 -0
  42. pymc_extras/statespace/models/SARIMAX.py +536 -0
  43. pymc_extras/statespace/models/VARMAX.py +393 -0
  44. pymc_extras/statespace/models/__init__.py +6 -0
  45. pymc_extras/statespace/models/structural.py +1651 -0
  46. pymc_extras/statespace/models/utilities.py +387 -0
  47. pymc_extras/statespace/utils/__init__.py +0 -0
  48. pymc_extras/statespace/utils/constants.py +74 -0
  49. pymc_extras/statespace/utils/coord_tools.py +0 -0
  50. pymc_extras/statespace/utils/data_tools.py +182 -0
  51. pymc_extras/utils/__init__.py +23 -0
  52. pymc_extras/utils/linear_cg.py +290 -0
  53. pymc_extras/utils/pivoted_cholesky.py +69 -0
  54. pymc_extras/utils/prior.py +200 -0
  55. pymc_extras/utils/spline.py +131 -0
  56. pymc_extras/version.py +11 -0
  57. pymc_extras/version.txt +1 -0
  58. pymc_extras-0.2.0.dist-info/LICENSE +212 -0
  59. pymc_extras-0.2.0.dist-info/METADATA +99 -0
  60. pymc_extras-0.2.0.dist-info/RECORD +101 -0
  61. pymc_extras-0.2.0.dist-info/WHEEL +5 -0
  62. pymc_extras-0.2.0.dist-info/top_level.txt +2 -0
  63. tests/__init__.py +13 -0
  64. tests/distributions/__init__.py +19 -0
  65. tests/distributions/test_continuous.py +185 -0
  66. tests/distributions/test_discrete.py +210 -0
  67. tests/distributions/test_discrete_markov_chain.py +258 -0
  68. tests/distributions/test_multivariate.py +304 -0
  69. tests/model/__init__.py +0 -0
  70. tests/model/marginal/__init__.py +0 -0
  71. tests/model/marginal/test_distributions.py +131 -0
  72. tests/model/marginal/test_graph_analysis.py +182 -0
  73. tests/model/marginal/test_marginal_model.py +867 -0
  74. tests/model/test_model_api.py +29 -0
  75. tests/statespace/__init__.py +0 -0
  76. tests/statespace/test_ETS.py +411 -0
  77. tests/statespace/test_SARIMAX.py +405 -0
  78. tests/statespace/test_VARMAX.py +184 -0
  79. tests/statespace/test_coord_assignment.py +116 -0
  80. tests/statespace/test_distributions.py +270 -0
  81. tests/statespace/test_kalman_filter.py +326 -0
  82. tests/statespace/test_representation.py +175 -0
  83. tests/statespace/test_statespace.py +818 -0
  84. tests/statespace/test_statespace_JAX.py +156 -0
  85. tests/statespace/test_structural.py +829 -0
  86. tests/statespace/utilities/__init__.py +0 -0
  87. tests/statespace/utilities/shared_fixtures.py +9 -0
  88. tests/statespace/utilities/statsmodel_local_level.py +42 -0
  89. tests/statespace/utilities/test_helpers.py +310 -0
  90. tests/test_blackjax_smc.py +222 -0
  91. tests/test_find_map.py +98 -0
  92. tests/test_histogram_approximation.py +109 -0
  93. tests/test_laplace.py +238 -0
  94. tests/test_linearmodel.py +208 -0
  95. tests/test_model_builder.py +306 -0
  96. tests/test_pathfinder.py +45 -0
  97. tests/test_pivoted_cholesky.py +24 -0
  98. tests/test_printing.py +98 -0
  99. tests/test_prior_from_trace.py +172 -0
  100. tests/test_splines.py +77 -0
  101. tests/utils.py +31 -0
tests/test_splines.py ADDED
@@ -0,0 +1,77 @@
1
+ # Copyright 2022 The PyMC Developers
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ import numpy as np
17
+ import pytensor.tensor as pt
18
+ import pytest
19
+
20
+ from pytensor.sparse import SparseTensorType
21
+
22
+ import pymc_extras as pmx
23
+
24
+
25
+ @pytest.mark.parametrize("dtype", [np.float32, np.float64])
26
+ @pytest.mark.parametrize("sparse", [True, False])
27
+ def test_spline_construction(dtype, sparse):
28
+ x = np.linspace(0, 1, 20, dtype=dtype)
29
+ np_out = pmx.utils.spline.numpy_bspline_basis(x, 10, 3)
30
+ assert np_out.shape == (20, 10)
31
+ assert np_out.dtype == dtype
32
+ spline_op = pmx.utils.spline.BSplineBasis(sparse=sparse)
33
+ out = spline_op(x, pt.constant(10), pt.constant(3))
34
+ if not sparse:
35
+ assert isinstance(out.type, pt.TensorType)
36
+ else:
37
+ assert isinstance(out.type, SparseTensorType)
38
+ B = out.eval()
39
+ if not sparse:
40
+ np.testing.assert_allclose(B, np_out)
41
+ else:
42
+ np.testing.assert_allclose(B.todense(), np_out)
43
+ assert B.shape == (20, 10)
44
+
45
+
46
+ @pytest.mark.parametrize("shape", [(100,), (100, 5)])
47
+ @pytest.mark.parametrize("sparse", [True, False])
48
+ @pytest.mark.parametrize("points", [dict(n=1001), dict(eval_points=np.linspace(0, 1, 1001))])
49
+ def test_interpolation_api(shape, sparse, points):
50
+ x = np.random.randn(*shape)
51
+ yt = pmx.utils.spline.bspline_interpolation(x, **points, sparse=sparse)
52
+ y = yt.eval()
53
+ assert y.shape == (1001, *shape[1:])
54
+
55
+
56
+ @pytest.mark.parametrize(
57
+ "params",
58
+ [
59
+ (dict(sparse="foo", n=100, degree=1), TypeError, "sparse should be True or False"),
60
+ (dict(n=100, degree=0.5), TypeError, "degree should be integer"),
61
+ (
62
+ dict(n=100, eval_points=np.linspace(0, 1), degree=1),
63
+ ValueError,
64
+ "Please provide one of n or eval_points",
65
+ ),
66
+ (
67
+ dict(degree=1),
68
+ ValueError,
69
+ "Please provide one of n or eval_points",
70
+ ),
71
+ ],
72
+ )
73
+ def test_bad_calls(params):
74
+ kw, E, err = params
75
+ x = np.random.randn(10)
76
+ with pytest.raises(E, match=err):
77
+ pmx.utils.spline.bspline_interpolation(x, **kw)
tests/utils.py ADDED
@@ -0,0 +1,31 @@
1
+ from collections.abc import Sequence
2
+
3
+ from pytensor.compile import SharedVariable
4
+ from pytensor.graph import Constant, graph_inputs
5
+ from pytensor.graph.basic import Variable, equal_computations
6
+ from pytensor.tensor.random.type import RandomType
7
+
8
+
9
+ def equal_computations_up_to_root(
10
+ xs: Sequence[Variable], ys: Sequence[Variable], ignore_rng_values=True
11
+ ) -> bool:
12
+ # Check if graphs are equivalent even if root variables have distinct identities
13
+
14
+ x_graph_inputs = [var for var in graph_inputs(xs) if not isinstance(var, Constant)]
15
+ y_graph_inputs = [var for var in graph_inputs(ys) if not isinstance(var, Constant)]
16
+ if len(x_graph_inputs) != len(y_graph_inputs):
17
+ return False
18
+ for x, y in zip(x_graph_inputs, y_graph_inputs):
19
+ if x.type != y.type:
20
+ return False
21
+ if x.name != y.name:
22
+ return False
23
+ if isinstance(x, SharedVariable):
24
+ if not isinstance(y, SharedVariable):
25
+ return False
26
+ if isinstance(x.type, RandomType) and ignore_rng_values:
27
+ continue
28
+ if not x.type.values_eq(x.get_value(), y.get_value()):
29
+ return False
30
+
31
+ return equal_computations(xs, ys, in_xs=x_graph_inputs, in_ys=y_graph_inputs)