Skip to content

Commit 8352cdd

Browse files
davmretensorflower-gardener
authored andcommitted
Add MarkovChain meta distribution for time series modeling.
My original motivation for this was to unify the two stochastic volatility models in the inference gym (https://github.com/tensorflow/probability/blob/master/spinoffs/inference_gym/inference_gym/targets/stochastic_volatility.py and https://github.com/tensorflow/probability/blob/master/spinoffs/inference_gym/inference_gym/targets/vectorized_stochastic_volatility.py). Currently, we have a non-vectorized model that exposes the graphical model structure needed for ASVI, but is very slow, and a vectorized version that is fast but does not expose any structure. Representing the volatility series as a MarkovChain would expose the graphical structure *and* support vectorized log-prob evaluation. More generally we could think of MarkovChain as a new flavor of joint distribution, since (like TransformedDistribution) it can represent multipart events. It proposes an answer to the question, "How can a JD efficiently define random variables in a loop?" Currently the only way to do this is to write an explicit Python loop in a JDCoroutine model, as the naive stochastic volatility model does, but this is obviously suboptimal. MarkovChains might be useful both as joint distributions over time series and to represent time-series components in larger joint distribution models. Another potential use is as a bridge between the particle filter API and the Distributions API. Our particle filtering code expects a time series model to be specified in terms of an initial prior, a transition function, and an observation function. It's natural to want to represent the distribution on the latent sequences implied by the prior and transition model, e.g., for prior predictive checks; MarkovChain allows us to do this. I'm not totally sure if MarkovChain is the right name. Throwing out some other possibilities: Markov, AutoregressiveMarkov, TimeSeries, Loop, Scan, <your suggestion here>? PiperOrigin-RevId: 377150791
1 parent 0679f1b commit 8352cdd

File tree

5 files changed

+807
-0
lines changed

5 files changed

+807
-0
lines changed

tensorflow_probability/python/distributions/BUILD

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ multi_substrate_py_library(
105105
":logitnormal",
106106
":loglogistic",
107107
":lognormal",
108+
":markov_chain",
108109
":masked",
109110
":matrix_normal_linear_operator",
110111
":matrix_t_linear_operator",
@@ -1305,6 +1306,27 @@ multi_substrate_py_library(
13051306
],
13061307
)
13071308

1309+
multi_substrate_py_library(
1310+
name = "markov_chain",
1311+
srcs = ["markov_chain.py"],
1312+
deps = [
1313+
":distribution",
1314+
# numpy dep,
1315+
# tensorflow dep,
1316+
"//tensorflow_probability/python/bijectors:chain",
1317+
"//tensorflow_probability/python/bijectors:invert",
1318+
"//tensorflow_probability/python/bijectors:joint_map",
1319+
"//tensorflow_probability/python/bijectors:split",
1320+
"//tensorflow_probability/python/internal:assert_util",
1321+
"//tensorflow_probability/python/internal:distribution_util",
1322+
"//tensorflow_probability/python/internal:parameter_properties",
1323+
"//tensorflow_probability/python/internal:prefer_static",
1324+
"//tensorflow_probability/python/internal:samplers",
1325+
"//tensorflow_probability/python/internal:tensor_util",
1326+
"//tensorflow_probability/python/internal:tensorshape_util",
1327+
],
1328+
)
1329+
13081330
multi_substrate_py_library(
13091331
name = "matrix_normal_linear_operator",
13101332
srcs = ["matrix_normal_linear_operator.py"],
@@ -3218,6 +3240,20 @@ multi_substrate_py_test(
32183240
],
32193241
)
32203242

3243+
multi_substrate_py_test(
3244+
name = "markov_chain_test",
3245+
size = "medium",
3246+
srcs = ["markov_chain_test.py"],
3247+
deps = [
3248+
# numpy dep,
3249+
# scipy dep,
3250+
# tensorflow dep,
3251+
"//tensorflow_probability/python/distributions",
3252+
"//tensorflow_probability/python/internal:samplers",
3253+
"//tensorflow_probability/python/internal:test_util",
3254+
],
3255+
)
3256+
32213257
multi_substrate_py_test(
32223258
name = "matrix_normal_linear_operator_test",
32233259
size = "medium",

tensorflow_probability/python/distributions/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
from tensorflow_probability.python.distributions.loglogistic import LogLogistic
8686
from tensorflow_probability.python.distributions.lognormal import LogNormal
8787
from tensorflow_probability.python.distributions.logitnormal import LogitNormal
88+
from tensorflow_probability.python.distributions.markov_chain import MarkovChain
8889
from tensorflow_probability.python.distributions.masked import Masked
8990
from tensorflow_probability.python.distributions.matrix_normal_linear_operator import MatrixNormalLinearOperator
9091
from tensorflow_probability.python.distributions.matrix_t_linear_operator import MatrixTLinearOperator
@@ -230,6 +231,7 @@
230231
'LogLogistic',
231232
'LogNormal',
232233
'LogitNormal',
234+
'MarkovChain',
233235
'Moyal',
234236
'NegativeBinomial',
235237
'Normal',

tensorflow_probability/python/distributions/hypothesis_testlib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
'LambertWDistribution',
8181
'MatrixNormalLinearOperator',
8282
'MatrixTLinearOperator',
83+
'MarkovChain',
8384
'Masked', # (has strategy)
8485
'Mixture', # (has strategy)
8586
'MixtureSameFamily', # (has strategy)

0 commit comments

Comments
 (0)