CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Added Joint_Handmade
and MarginalDistribution
in stats
#14989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Added `JointRV`, which allows the user to create a joint random variable with each of its component varying over the real line. Added `JointDistributionHandmade` for this purpose. Also made minor modification in core/function.py to allow `Lambda` to accept indexed symbols. Added tests.
Fix in `JointRV` to order indexed symbols extracted by their index, instead of using the set as it is. Also replaced symbols by indexed symbols in `marginal_distribution`. `Indexed` are not used directly and instead replace `Symbol` towards the end of the function because `integrate` raised error in some cases when the variable of integration was of type `Indexed`.
Added class `MarginalDistribution` and the ability to handle compound distributions using this.
Modified some tests, and added `evaluate` flag for marginal distributions.
Fixed some of the previously failing tests in stats module, by constructing marginal distribution with existing distributions instead of PDFs.
Fixed `domain` for JointPSpace and made some corrections to `pdf` in `MarginalDistribution`.
Removed trailing whitespaces, corrected import statements in stats.
âś… Hi, I am the SymPy bot (v120). I'm here to make sure this pull request has a release notes entry. Please read the guide on how to write release notes. Click here to see the pull request description that was parsed.
Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.2.1. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Update The release notes on the wiki have been updated. |
Joint_Handmade
and MarginalDistribution
in stats
When I look at In [11]: x = Symbol('x')
...: l = Symbol('l', positive=True)
...: rate = Beta(l, 2, 3)
...: X = Poisson(x, rate)
In [12]: X
Out[12]: x
In [13]: X.pspace
Out[13]: JointPSpace(x, MarginalDistribution(PoissonDistribution(l), (x,))) I mean, In [23]: pd
Out[23]: PoissonDistribution(l)
In [24]: type(pd)
Out[24]: sympy.stats.drv_types.PoissonDistribution
In [25]: type(pd).__mro__
Out[25]:
(sympy.stats.drv_types.PoissonDistribution,
sympy.stats.drv.SingleDiscreteDistribution,
sympy.core.basic.Basic,
sympy.stats.rv.NamedArgsMixin,
object) |
Can we represent the joint distribution of a compound random variable? |
Such distributions would need to go into either |
The problem remains that I will think a bit about this problem. |
I guess the best way to represent a compound distribution is by having a |
Added class `CompoundDistribution`, which requires a distribution with a random parameter for construction.
In case the user creates a compound random variable, the distribution is of the type `CompoundDistribution`. Added and modified some tests.
sympy/stats/joint_rv.py
Outdated
@@ -46,26 +57,49 @@ def value(self): | |||
|
|||
@property | |||
def component_count(self): | |||
return len(self.distribution.set.args) | |||
_set = self.distribution.set | |||
if isinstance(_set, ProductSet): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use ternary operator here.
return len(_set.args) if isinstance(_set, ProductSet) else 1
Doubt would affect execution time though.
sympy/stats/joint_rv.py
Outdated
distributed according to some given distribution. | ||
""" | ||
def __new__(cls, dist): | ||
assert isinstance(dist, (ContinuousDistribution, DiscreteDistribution)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be beneficial for end-user to get a a better explicit error message if instantiation is attempted to be done from an illegal distribution.
Such as:
if not isinstance(dist, (ContinuousDistribution, DiscreteDistribution)):
raise ValueError('CompoundDistribution can only be initialized from ContinuousDistribution or DiscreteDistribution')
or something similar.
sympy/stats/joint_rv.py
Outdated
class MarginalDistribution(Basic): | ||
|
||
def __new__(cls,dist, rvs): | ||
assert all([isinstance(rv, (Indexed, RandomSymbol))] for rv in rvs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as discussed earlier. Better for end-user to get a proper error message.
sympy/stats/tests/test_joint_rv.py
Outdated
x1, x2 = (Indexed('x', i) for i in (1, 2)) | ||
pdf = exp(-x1**2/2 + x1 - x2**2/2 - S(1)/2)/(2*pi) | ||
X = JointRV('x', pdf) | ||
density(X)(1, 2) == exp(-1)/(2*pi) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you miss an assert
here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that was an incorrect test. Fixed it now.
Added error messages and fixed one incorrect test in test_joint_rv.py
|
||
def test_compound_distribution(): | ||
Y = Poisson('Y', 1) | ||
Z = Poisson('Z', Y) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried Z.pspace.distribution.pdf(x)
but I get an exception.
References to other Issues or PRs
Brief description of what is fixed or changed
Added
JointRV
, which allows the user to create a joint random variablewith each of its component varying over the real line. Also added
JointDistributionHandmade
for this purpose.Added
MarginalDistribution
to allow random variables with components marginalized out. This also adds the ability to handle compound random variables by marginalizing over latent distributions, with or without a given condition.Other comments
Same code as #14951 is added, which has to be closed due to some extra commits showing up.
Release Notes
JointRV
to allow user to create Joint random variablesMarginalDistribution
,CompoundDistribution