Skip to content

Instantly share code, notes, and snippets.

@mrocklin
Created December 2, 2012 02:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrocklin/4186685 to your computer and use it in GitHub Desktop.
Save mrocklin/4186685 to your computer and use it in GitHub Desktop.
An example using SymPy.stats to compute the characteristic function of a general Cauchy random variable. This is run in isympy (hence the pretty printing).
In [1]: from sympy.stats import *
In [2]: x0 = Symbol('x0', real=True, bounded=True)
In [3]: gamma = Symbol('gamma', positive=True)
In [4]: X = Cauchy('X', x0, gamma)
In [5]: density(X)(x)
Out[5]:
1
───────────────────
⎛ 2⎞
⎜ (x - x₀) ⎟
π⋅γ⋅⎜1 + ─────────⎟
⎜ 2 ⎟
⎝ γ ⎠
In [6]: t = Symbol('t', positive=True)
In [7]: E(exp(I*t*X))
Out[7]:
⎛ ⎛ π⎞ ⎛ ⅈ⋅π⎞ ⎞ ⅈ⋅t⋅x₀ ⎛ ⎛ π⎞ ⎛ ⅈ⋅π⎞ ⎞ ⅈ⋅t⋅x₀
⎜2⋅⎜-ⅈ⋅Shi(γ⋅t) - ─⎟⋅cosh(γ⋅t) + 2⋅ⅈ⋅⎜Chi(γ⋅t) - ───⎟⋅sinh(γ⋅t)⎟⋅ℯ ⎜2⋅⎜ⅈ⋅Shi(γ⋅t) - ─⎟⋅cosh(γ⋅t) - 2⋅ⅈ⋅⎜Chi(γ⋅t) + ───⎟⋅sinh(γ⋅t)⎟⋅ℯ
⎝ ⎝ 2⎠ ⎝ 2 ⎠ ⎠ ⎝ ⎝ 2⎠ ⎝ 2 ⎠ ⎠
- ──────────────────────────────────────────────────────────────────────── - ───────────────────────────────────────────────────────────────────────
2⋅π 2⋅π
In [8]: simplify(E(exp(I*t*X)))
Out[8]:
ⅈ⋅t⋅x₀
(-sinh(γ⋅t) + cosh(γ⋅t))⋅ℯ
@Abrahammar997
Copy link

Abrahammar997 commented Jul 23, 2021

What sympy version do you use? I am trying the same thing but I got

In [9]: simplify(E(exp(I*t*X)))
Out[9]: 
  ∞                           
  ⌠                           
  ⎮           Xt                
γ⋅⎮  ────────────────────── dX2             2     2X  - 2Xx+ γ  + x₀    
  ⌡                           
  -∞                          
──────────────────────────────
              π      

which is not as nice as yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment