class_docstr: Stochastic model of asset price volatility over time.
  
  The model assumes that asset prices follow a random walk, in which the
  volatility---the variance of the day-to-day returns---is itself changing
  over time. The volatility is assumed to evolve as an AR(1) process with
  unknown coefficient and white noise shock scale.
  
  ```none
  
  persistence_of_volatility ~ 2 * Beta(concentration1=20,
                                       concentration0=1.5) - 1.
  mean_log_volatility ~ Cauchy(loc=0, scale=5)
  white_noise_shock_scale ~ HalfCauchy(loc=0, scale=2)
  
  # Order-1 autoregressive process on log volatility.
  log_volatility[0] ~ Normal(
      loc=0,
      scale=white_noise_shock_scale / sqrt(1 - persistence_of_volatility**2))
  for t in range(1, num_timesteps):
    log_volatility[t] ~ Normal(
        loc=persistence_of_volatility * log_volatility[t - 1],
        scale=white_noise_shock_scale)
  
  for t in range(num_timesteps):
    centered_returns[t] ~ Normal(
      loc=0, scale=sqrt(exp(mean_log_volatility + log_volatility[t])))
  ```
  
  #### References
  
  [1] <https://mc-stan.org/docs/2_23/stan-users-guide/
      stochastic-volatility-models.html>
class_docstr_tok: ["Stochastic","model","of","asset","price","volatility","over","time",".","The","model","assumes","that","asset","prices","follow","a","random","walk",",","in","which","the","volatility","--","-the","variance","of","the","day-to-day","returns","--","-is","itself","changing","over","time",".","The","volatility","is","assumed","to","evolve","as","an","AR","(","1",")","process","with","unknown","coefficient","and","white","noise","shock","scale",".","``","`","none","persistence_of_volatility","~","2","*","Beta","(","concentration1=20",",","concentration0=1.5",")","-","1.","mean_log_volatility","~","Cauchy","(","loc=0",",","scale=5",")","white_noise_shock_scale","~","HalfCauchy","(","loc=0",",","scale=2",")","#","Order-1","autoregressive","process","on","log","volatility",".","log_volatility","[","0","]","~","Normal","(","loc=0",",","scale=white_noise_shock_scale","\/","sqrt","(","1","-","persistence_of_volatility","*","*","2",")",")","for","t","in","range","(","1",",","num_timesteps",")",":","log_volatility","[","t","]","~","Normal","(","loc=persistence_of_volatility","*","log_volatility","[","t","-","1","]",",","scale=white_noise_shock_scale",")","for","t","in","range","(","num_timesteps",")",":","centered_returns","[","t","]","~","Normal","(","loc=0",",","scale=sqrt","(","exp","(","mean_log_volatility","+","log_volatility","[","t","]",")",")",")","``","`","#","#","#","#","References","[","1","]","<","https",":","\/\/mc-stan.org\/docs\/2_23\/stan-users-guide\/","stochastic-volatility-models.html",">"]
docstr: Construct the stochastic volatility model.
  
  Args:
    centered_returns: Float `Tensor` of shape `[num_timesteps]` giving the
      mean-adjusted return (change in asset price, minus the average change)
      observed at each step.
    use_markov_chain: Python `bool` indicating whether to use the
      `MarkovChain` distribution in place of separate random variables for
      each time step. The default of `False` is for backwards compatibility;
      setting this to `True` should significantly improve performance.
    name: Python `str` name prefixed to Ops created by this class.
    pretty_name: A Python `str`. The pretty name of this model.
docstr_tok: ["Construct","the","stochastic","volatility","model",".","Args",":","centered_returns",":","Float","`","Tensor","`","of","shape","`","[","num_timesteps","]","`","giving","the","mean-adjusted","return","(","change","in","asset","price",",","minus","the","average","change",")","observed","at","each","step",".","use_markov_chain",":","Python","`","bool","`","indicating","whether","to","use","the","`","MarkovChain","`","distribution","in","place","of","separate","random","variables","for","each","time","step",".","The","default","of","`","False","`","is","for","backwards","compatibility",";","setting","this","to","`","True","`","should","significantly","improve","performance",".","name",":","Python","`","str","`","name","prefixed","to","Ops","created","by","this","class",".","pretty_name",":","A","Python","`","str","`",".","The","pretty","name","of","this","model","."]
code: def __init__(
      self,
      centered_returns,
      use_markov_chain=False,
      name="stochastic_volatility",
      pretty_name="Stochastic Volatility",
  ):
      """Construct the stochastic volatility model.
  
      Args:
        centered_returns: Float `Tensor` of shape `[num_timesteps]` giving the
          mean-adjusted return (change in asset price, minus the average change)
          observed at each step.
        use_markov_chain: Python `bool` indicating whether to use the
          `MarkovChain` distribution in place of separate random variables for
          each time step. The default of `False` is for backwards compatibility;
          setting this to `True` should significantly improve performance.
        name: Python `str` name prefixed to Ops created by this class.
        pretty_name: A Python `str`. The pretty name of this model.
      """
      with tf.name_scope(name):
          num_timesteps = ps.size0(centered_returns)
          if tf.is_tensor(num_timesteps):
              raise ValueError(
                  "Returns series length must be static, but saw "
                  "shape {}.".format(centered_returns.shape)
              )
  
          self._prior_dist = tfd.JointDistributionCoroutine(
              functools.partial(
                  stochastic_volatility_prior_fn,
                  num_timesteps=num_timesteps,
                  use_markov_chain=use_markov_chain,
              )
          )
  
          self._log_likelihood_fn = functools.partial(
              stochastic_volatility_log_likelihood_fn,
              centered_returns=centered_returns,
              use_markov_chain=use_markov_chain,
          )
  
          def _ext_identity(params):
              res = collections.OrderedDict()
              res[
                  "persistence_of_volatility"
              ] = params.persistence_of_volatility
              res["mean_log_volatility"] = params.mean_log_volatility
              res[
                  "white_noise_shock_scale"
              ] = params.white_noise_shock_scale
              res["log_volatility"] = (
                  params.log_volatility
                  if use_markov_chain
                  else tf.stack(params.log_volatility, axis=-1)
              )
              return res
  
          sample_transformations = {
              "identity": model.Model.SampleTransformation(
                  fn=_ext_identity,
                  pretty_name="Identity",
                  dtype=collections.OrderedDict(
                      persistence_of_volatility=tf.float32,
                      mean_log_volatility=tf.float32,
                      white_noise_shock_scale=tf.float32,
                      log_volatility=tf.float32,
                  ),
              )
          }
  
      if use_markov_chain:
          log_volatility_bijector = tfb.Identity()
      else:
          log_volatility_bijector = type(self._prior_dist.dtype[-1])(
              *((tfb.Identity(),) * num_timesteps)
          )
      super(StochasticVolatility, self).__init__(
          default_event_space_bijector=type(self._prior_dist.dtype)(
              tfb.Sigmoid(-1.0, 1.0),
              tfb.Identity(),
              tfb.Softplus(),
              log_volatility_bijector,
          ),
          event_shape=self._prior_dist.event_shape,
          dtype=self._prior_dist.dtype,
          name=name,
          pretty_name=pretty_name,
          sample_transformations=sample_transformations,
      )
  
imports: import collections
  import functools
  import tensorflow.compat.v2
  import tensorflow_probability
  from tensorflow_probability.python.internal import prefer_static
  from inference_gym.internal import data
  from inference_gym.targets import bayesian_model
  from inference_gym.targets import model
  from inference_gym.targets.ground_truth import stochastic_volatility_sp500
  from inference_gym.targets.ground_truth import stochastic_volatility_sp500_small
label_desc: Use image node_id 1 to create a new StochasticVolatility object from inherited base classes: bayesian_model with example: obj = StochasticVolatility(centered_returns, use_markov_chain, name, pretty_name)