On Fri, Nov 18, 2011 at 7:49 AM, Matthew Rocklin <mrock...@gmail.com> wrote:
> The new sympy statistics module can solve this iteratively for you. It's not
> in yet however and it takes some time. The following was done from my rv2
> branch. It essentially tries all possibilities. You can also reinvent what
> it does using FiniteSets which are in master.
> === Using Random Variables ===
> --input--
> from sympy.statistics import *
> chickens, spiders, pigs = Die(20, 'chickens'), Die(20, 'spiders'), Die(20,
> 'pigs')
> Where(And(Eq(chickens+spiders+pigs, 20),
> Eq(2*chickens+4*pigs+8*spiders,56))).as_boolean()
> --output--
> (spiders = 1 ∧ chickens = 14 ∧ pigs = 5) ∨ (pigs = 2 ∧ chickens = 16 ∧
> spiders = 2)
> It's designed to solve a bigger problem where each outcome has an associated
> probability. I modeled chickens, spiders, and pigs as uniform discrete
> random variables (20 sided dice). The Where operator only cares about
> possibility though so these probabilities are forgotten in the answer, which
> is just a SymPy Boolean object.

This is a neat example.  Does this first one also just use for loops,
or does it solve the equations?

Aaron Meurer

> === Using Finite Sets ===
> If you wanted to do the iteration in a fancy and complex way you could
> iterate through cartesian products of FiniteSets
> I.e.
> chicken_possibilities = FiniteSet(Eq(chicken, i) for i in range(20))
> pig_possibilities = FiniteSet(Eq(pig, i) for i in range(20))
> spider_possibilities = FiniteSet(Eq(spider, i) for i in range(20))
> all_possibilities = [And(*possibility) for possibility in
> chicken_possiblities * pig_possiblities * spider_possibilities]
> all_possibilities[0]
> chicken = 4 ∧ pig = 13 ∧ spider = 14
> You could transform this to a dict and then subs it into a condition
> condition = And(Eq(chickens+pigs+spiders, 20),
> Eq(2*chickens+4*pigs+8*spiders,56))
> Really though this is just an obfuscated triple-for-loop. Really, a
> triple-for-loop with an if statement might be the clearest solution.
> -Matt
>
> On Fri, Nov 18, 2011 at 8:21 AM, Chris Smith <smi...@gmail.com> wrote:
>>
>> OK, it's not too bad to have it solve the relationships such that the
>> spider relationships are positive (since those relationships tell you
>> the number of pigs and chickens there are):
>>
>>    >>> var('spiders chickens pigs',integer=True, positive=True)
>>    (spiders, chickens, pigs)
>>    >>> solve([Eq(chickens+pigs+spiders, 20),
>> Eq(2*chickens+4*pigs+8*spiders,56)])
>>    {chickens: 2*spiders + 12, pigs: -3*spiders + 8}
>>    >>> solve([Ge(v,0) for v in _.values()])
>>    And(-6 <= spiders, spiders <= 8/3)
>>    >>> condition = _
>>
>> That And can be used to test values for spiders if desired:
>>
>>    >>> [i for i in range(8) if condition.subs(spiders, i)]
>>    [0, 1, 2]
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "sympy" group.
>> To post to this group, send email to sympy@googlegroups.com.
>> To unsubscribe from this group, send email to
>> sympy+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/sympy?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to sympy@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to