Skip to content

Instantly share code, notes, and snippets.

@mrocklin
Created December 2, 2012 02:52
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/4186709 to your computer and use it in GitHub Desktop.
Save mrocklin/4186709 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]: nu = Symbol("nu", positive=True)
In [3]: X = StudentT("x", nu)
In [4]: density(X)(x)
Out[4]:
ν 1
- ─ - ─
2 2
⎛ 2⎞
⎜ x ⎟ ⎛ν 1⎞
⎜1 + ──⎟ ⋅Γ⎜─ + ─⎟
⎝ ν ⎠ ⎝2 2⎠
────────────────────────
___ ___ ⎛ν⎞
╲╱ π ⋅╲╱ ν ⋅Γ⎜─⎟
⎝2⎠
In [5]: t = Symbol('t', positive=True)
In [6]: simplify(E(exp(I*t*X)))
Out[6]:
ν ν ⎛ ⅈ⋅π⋅ν ⎞ -ⅈ⋅π⋅ν
─ - 1 ⎛4 ___ ___⎞ ⎜ ───── ⎟ ──────
2 ⎜╲╱ ν ⋅╲╱ t ⎟ ⎜ ν 2 ⎛π⋅ν⎞ ⎛-ν ___ ⎞ ν ⅈ⋅π⋅ν ⎛ν ___ ⎞ ν ⎛ν ___ ⎞⎟ 2
2 ⋅π⋅⎜───────────⎟ ⋅⎜2⋅4 ⋅ℯ ⋅cos⎜───⎟⋅besseli⎜──, ╲╱ ν ⋅t⎟ - 4 ⋅ℯ ⋅besseli⎜─, ╲╱ ν ⋅t⎟ - 4 ⋅besseli⎜─, ╲╱ ν ⋅t⎟⎟⋅ℯ
⎝ 8 ⎠ ⎝ ⎝ 2 ⎠ ⎝2 ⎠ ⎝2 ⎠ ⎝2 ⎠⎠
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
⎛π⋅ν⎞ ⎛π⋅ν⎞ ⎛ν⎞
sin⎜───⎟⋅cos⎜───⎟⋅Γ⎜─⎟
⎝ 2 ⎠ ⎝ 2 ⎠ ⎝2⎠
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment