Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a variable in sympy to be a CONSTANT

from sympy import *
from sympy.stats import *
mu, Y = symbols('mu Y', real = True, constant = True)
sigma = symbols('sigma', real = True, positive=True)
X = Normal('X', mu, sigma)

When asking for:

E(X, evaluate=False)

I get:

∞                     
⌠                     
⎮                2    
⎮        -(X - μ)     
⎮        ──────────   
⎮              2      
⎮           2⋅σ       
⎮  √2⋅X⋅ℯ             
⎮  ──────────────── dX
⎮       2⋅√π⋅σ        
⌡                     
-∞ 

Which is what I expect. When asking for:

E(X, X>0, evaluate=False)
E(X, X>pi, evaluate=False)
E(X, X >-3, evaluate=False)

Using any constant, the result is as expected from the Normal Definition of conditional expectation. However, when trying to solve for:

E(X, X>Y)

I'm getting an error that has to do with roots. Is there a way to define a Y, such that sympy acknowledges that it is a constant, just like a 0 or a -3 or even pi, and shows the integration as expected? I'm assuming the problem with the request I have from sympy is that somehow the Y isn't acknowledges as a constant and therefore, when trying to solve this request, sympy is faced with a roots problem.

like image 935
Yevgeniy Loboda Avatar asked Sep 23 '16 16:09

Yevgeniy Loboda


People also ask

How do you define a constant in SymPy?

With the help of sympy. is_constant() method, we can check if element is constant or not and it will return a boolean value if found constant by using sympy. is_constant() method.

How do you define a constant variable?

A constant variable is one whose value cannot be updated or altered anywhere in your program. A constant variable must be initialized at its declaration.

How do you declare a constant variable in Python?

Assigning value to constant in Python In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.

How do you define a constant in Python module?

Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL . Save this answer.


1 Answers

Your problem appears to be a limitation in the current inequality solver: the algorithm that transforms a system of inequalities to a union of sets apparently needs to sort the boundary points determined by those inequalities (even if there's only one such point). Reduction of inequalities with symbolic limits has not been implemented yet.

I suggest a dirty trick to get around this limitation. Define:

class SymbolTrick(NumberSymbol):
    def __new__(self, name):
        obj = NumberSymbol.__new__(self)
        obj._name = name
        return obj

    _as_mpf_val = pi._as_mpf_val
    approximation_interval = pi.approximation_interval
    __str__ = lambda self: str(self._name)

This defines a subclass of NumberSymbol having the same numeric value of pi (it is necessary to specify one, as the inequality reduction algorithm needs to sort the list boundaries otherwise it will fail).

At this point:

In [7]: Y = SymbolTrick("Y")

In [8]: E(X, X > Y, evaluate=False)
Out[8]: 
∞                              
⌠                              
⎮                    2         
⎮            -(X - μ)          
⎮            ──────────        
⎮                  2           
⎮               2⋅σ            
⎮      √2⋅X⋅ℯ                  
⎮ ────────────────────────── dX
⎮        ∞                     
⎮        ⌠                     
⎮        ⎮             2       
⎮        ⎮     -(X - μ)        
⎮        ⎮     ──────────      
⎮        ⎮           2         
⎮        ⎮        2⋅σ          
⎮        ⎮ √2⋅ℯ                
⎮ 2⋅√π⋅σ⋅⎮ ────────────── dX   
⎮        ⎮     2⋅√π⋅σ          
⎮        ⌡                     
⎮        Y                     
⌡      
Y
like image 56
Francesco Bonazzi Avatar answered Nov 04 '22 10:11

Francesco Bonazzi