Currently for Binomial distribution under sympy/stats/frv_types.py we have the following code for returning density as dict:
def dict(self):
n, p, succ, fail = self.n, self.p, self.succ, self.fail
n = as_int(n)
return dict((k*succ + (n - k)*fail,
binomial(n, k) * p**k * (1 - p)**(n - k)) for k in range(0, n + 1))
This forces n to be an Integer. Is there a way to construct a dictionary with symbolic n?
If I understand correctly this dict is never exposed directly to users and is used in two different ways:
for v, p in density.items().density[v].For the second of those it would be possible to make a Python object that supports __getitem__ without being a concrete dict. For example, a density "dict" for the geometric distribution (which has an infinite domain) can be created with:
from sympy import *
class GeometricProbabilityDict:
def __init__(self, p):
self.p = p
def __getitem__(self, n):
return p*(1-p)**n
p = Symbol('p')
gpd = GeometricProbabilityDict(p)
for i in range(10):
print(gpd[i])
This wouldn't work if you tried to iterate over it though since that would lead to an infinite loop.
Changes would be needed in FinitePSpace to support something like this.
There is a problem with ArrayComprehension. Dict works fine with list. It can be corrected though. If you are not working on it then I will correct it asap.
Thank you @czgdp1807. Let me give it a try and if I need any help, I will ask you.
The issue as described here is misguided. A symbolic Dict will not solve the actual problem since it won't be usable in the ways that FinitePSpace expects.
I suggest to close this issue and open a new one with the title "FinitePSpace doesn't support symbolically sized sample spaces" and the motivating example:
In [21]: from sympy.stats import Binomial, E
In [22]: from sympy import symbols
In [23]: n, p = symbols('n, p')
In [24]: X = Binomial('X', n, p)
In [25]: E(X)
...
ValueError: n is not an integer
New issue has been created. Please close this one.