Search code examples
pythonsympysolvernonlinear-optimizationnonlinear-equation

true odds: sympy solver with multiple equations and variables


I'm trying to calculate the 3-way true odds based on given odds according to this methodology: enter image description here

I'm trying to do this with the python sympy module with the following code:

x, y, z, p, q, r, n = symbols('x, y, z, p, q, r, n', real=True)
odds = [1.41,5.09,8.4]
x = 1/odds[0]
y = 1/odds[1]
z = 1/odds[2]
nonlinsolve([Eq(p**n, x), Eq(q**n, y), Eq(r**n, z) 
            ,Eq(p**n + q**n + r**n, x + y + z)
            ,Eq(x**(1/n) + y**(1/n) + z**(1/n), p + q + r)
            ,Eq(p + q + r, 1)
             ],[p,q,r,n])

It should yield (numeric) probabilities slightly lower than x, y, z but just returns {(-q - r + 1, q, r, n)} I thought about specifying that x>p, y>q, z>r but am not sure how to include these inequalities. However I think there is more that is going wrong, as the return can be derived from the last given equation only. Any ideas on how I can get what I want here?


Solution

  • For a numerical system, nsolve is your ticket:

    ...
    >>> [re(i).n(3) for i in nsolve([Eq(p**n, x), Eq(q**n, y), Eq(r**n, z) 
                ,Eq(p**n + q**n + r**n, x + y + z)
                ,Eq(x**(1/n) + y**(1/n) + z**(1/n), p + q + r)
                ,Eq(p + q + r, 1)
                 ],[p,q,r,n],[.1]*4)]
    [0.702, 0.187, 0.111, 0.970]