stochastic_process.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from sympy.core.basic import Basic
  2. from sympy.stats.joint_rv import ProductPSpace
  3. from sympy.stats.rv import ProductDomain, _symbol_converter, Distribution
  4. class StochasticPSpace(ProductPSpace):
  5. """
  6. Represents probability space of stochastic processes
  7. and their random variables. Contains mechanics to do
  8. computations for queries of stochastic processes.
  9. Explanation
  10. ===========
  11. Initialized by symbol, the specific process and
  12. distribution(optional) if the random indexed symbols
  13. of the process follows any specific distribution, like,
  14. in Bernoulli Process, each random indexed symbol follows
  15. Bernoulli distribution. For processes with memory, this
  16. parameter should not be passed.
  17. """
  18. def __new__(cls, sym, process, distribution=None):
  19. sym = _symbol_converter(sym)
  20. from sympy.stats.stochastic_process_types import StochasticProcess
  21. if not isinstance(process, StochasticProcess):
  22. raise TypeError("`process` must be an instance of StochasticProcess.")
  23. if distribution is None:
  24. distribution = Distribution()
  25. return Basic.__new__(cls, sym, process, distribution)
  26. @property
  27. def process(self):
  28. """
  29. The associated stochastic process.
  30. """
  31. return self.args[1]
  32. @property
  33. def domain(self):
  34. return ProductDomain(self.process.index_set,
  35. self.process.state_space)
  36. @property
  37. def symbol(self):
  38. return self.args[0]
  39. @property
  40. def distribution(self):
  41. return self.args[2]
  42. def probability(self, condition, given_condition=None, evaluate=True, **kwargs):
  43. """
  44. Transfers the task of handling queries to the specific stochastic
  45. process because every process has their own logic of handling such
  46. queries.
  47. """
  48. return self.process.probability(condition, given_condition, evaluate, **kwargs)
  49. def compute_expectation(self, expr, condition=None, evaluate=True, **kwargs):
  50. """
  51. Transfers the task of handling queries to the specific stochastic
  52. process because every process has their own logic of handling such
  53. queries.
  54. """
  55. return self.process.expectation(expr, condition, evaluate, **kwargs)