Skip to content
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

Generating Functions for sympy.stats #6323

Closed
mrocklin opened this issue Apr 14, 2012 · 12 comments · Fixed by #19724
Closed

Generating Functions for sympy.stats #6323

mrocklin opened this issue Apr 14, 2012 · 12 comments · Fixed by #19724
Labels

Comments

@mrocklin
Copy link
Member

Is there any benefit to reasoning about generating functions (moment, cumulant, etc...) for random variables? Are there problems that we could solve more easily with this extra formalism? http://en.wikipedia.org/wiki/Cumulant_generating_function http://en.wikipedia.org/wiki/Moment_generating_function

Original issue for #6323: http://code.google.com/p/sympy/issues/detail?id=3224
Original author: https://code.google.com/u/109882876523836932473/

@mrocklin
Copy link
Member Author

http://en.wikipedia.org/wiki/Probability-generating_function http://en.wikipedia.org/wiki/Characteristic_function_(probability_theory)

Original comment: http://code.google.com/p/sympy/issues/detail?id=3224#c1
Original author: https://code.google.com/u/109882876523836932473/

@aaaagrawal
Copy link

@mrocklin : Now that we have functions to calculate moments after https://github.com/sympy/sympy/pull/1875 , do we need these functions ? If yes I would like to work on them.

Ankit.

Original comment: http://code.google.com/p/sympy/issues/detail?id=3224#c2
Original author: https://code.google.com/u/110118527174861881051/

@mrocklin
Copy link
Member Author

Do we need these?  No.
Would they be useful?  Maybe, I don't know enough about them.
Would they be nice to have in any event?  Sure.

If you want to work on them then that's great.  If nothing else it'd be nice to have their formal definition in SymPy.  As you're working on them it might be interesting to think about problems that these bits of formalism solve that our current system finds difficult.

Original comment: http://code.google.com/p/sympy/issues/detail?id=3224#c3
Original author: https://code.google.com/u/109882876523836932473/

@aaaagrawal
Copy link

Sure, I will think of the cases where these generating/characteristics
functions can be used, as it would serve as better examples in docs too.
Also I have some submission deadlines so I will commit tomorrow. Thanks.

Original comment: http://code.google.com/p/sympy/issues/detail?id=3224#c4
Original author: https://code.google.com/u/110118527174861881051/

@asmeurer
Copy link
Member

asmeurer commented Mar 5, 2014

We have moved issues to GitHub https://github.com/sympy/sympy/issues .

**Labels:** Restrict-AddIssueComment-Commit  

Original comment: http://code.google.com/p/sympy/issues/detail?id=3224#c5
Original author: https://code.google.com/u/asmeurer@gmail.com/

@asmeurer
Copy link
Member

asmeurer commented Mar 5, 2014

We have moved issues to GitHub https://github.com/sympy/sympy/issues .

Original comment: http://code.google.com/p/sympy/issues/detail?id=3224#c6
Original author: https://code.google.com/u/asmeurer@gmail.com/

@Smit-create
Copy link
Member

Currently sympy.stats support, moment, cmoment, smoment, factorial_moment, characteristic_function, and moment_generating_function

def moment(X, n, c=0, condition=None, **kwargs):
"""
Return the nth moment of a random expression about c i.e. E((X-c)**n)
Default value of c is 0.

def cmoment(X, n, condition=None, **kwargs):
"""
Return the nth central moment of a random expression about its mean
i.e. E((X - E(X))**n)

def smoment(X, n, condition=None, **kwargs):
"""
Return the nth Standardized moment of a random expression i.e.
E(((X - mu)/sigma(X))**n)

def factorial_moment(X, n, condition=None, **kwargs):
"""
The factorial moment is a mathematical quantity defined as the expectation
or average of the falling factorial of a random variable.
factorial_moment(X, n) = E(X*(X - 1)*(X - 2)*...*(X - n + 1))

sympy/sympy/stats/rv.py

Lines 969 to 973 in 3b4c1a6

def characteristic_function(expr, condition=None, evaluate=True, **kwargs):
"""
Characteristic function of a random expression, optionally given a second condition
Returns a Lambda

sympy/sympy/stats/rv.py

Lines 1002 to 1006 in 3b4c1a6

def moment_generating_function(expr, condition=None, evaluate=True, **kwargs):
if condition is not None:
return moment_generating_function(given(expr, condition, **kwargs), **kwargs)
result = pspace(expr).compute_moment_generating_function(expr, **kwargs)

IMHO, this can be safely closed. Should we close this @Upabjojr @czgdp1807 ?

@Upabjojr
Copy link
Contributor

Upabjojr commented Jul 6, 2020

Currently sympy.stats support, moment, cmoment, smoment, factorial_moment, characteristic_function, and moment_generating_function

Do we have the symbolic class for all of them? It's important to have a class to express unevaluated functions, so we can leave the unevaluated Moment(X, 1) in case functions like moment(X, 1) are unable to evaluate.

@Smit-create
Copy link
Member

For unevaluated results, we can use evaluate=False:

In [1]: from sympy.stats import *

In [2]: X = Normal('X', 1, 3)

In [3]: moment(X, 4, evaluate=False)
Out[3]:
∞
⌠
⎮                 2-(X - 1)
⎮         ──────────
⎮      4      18
⎮  √2X
⎮  ───────────────── dX6⋅√π-In [4]: moment(X, 4)
Out[4]: 298

In [5]: cmoment(X, 3, evaluate=False)
Out[5]:
∞
⌠
⎮                                 3
⎮     ⎛    ∞                     ⎞
⎮     ⎜    ⌠                     ⎟
⎮     ⎜    ⎮                2    ⎟
⎮     ⎜    ⎮        -(X - 1)     ⎟           2
⎮     ⎜    ⎮        ──────────   ⎟   -(X - 1)
⎮     ⎜    ⎮            18       ⎟   ──────────
⎮     ⎜    ⎮  √2X18
⎮  √2⋅⎜X - ⎮  ──────────────── dX⎟ ⋅

⎮     ⎜    ⎮        6⋅√π         ⎟
⎮     ⎜    ⌡                     ⎟
⎮     ⎝    -∞                    ⎠
⎮  ──────────────────────────────────────────── dX6⋅√π-In [6]: cmoment(X, 3)
Out[6]: 0

In [7]: factorial_moment(X, 2)
Out[7]: 9

In [8]: factorial_moment(X, 2, evaluate=False)
Out[8]:

∞
⌠
⎮                        2-(X - 1)
⎮                ──────────
⎮                    18
⎮  √2X⋅(X - 1)⋅
⎮  ──────────────────────── dX6⋅√π-

@oscarbenjamin
Copy link
Contributor

Is there a Moment class rather than Integral?

@Upabjojr
Copy link
Contributor

Upabjojr commented Jul 7, 2020

For unevaluated results, we can use evaluate=False

That is returning the unevaluated integral, that is not the unevaluated Moment. We have already classes Probability, Expectation, Variance, Covariance for unevaluated expressions... but not for the other ones.

@Smit-create
Copy link
Member

Sure I get it, It Will be nice additions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants