title: The generalized Student-T probability distribution I coded in Python doesn't integrate to 1 (in some cases)
body: <p>I've been trying to implement the skewed generalized <em>t</em> distribution in Python to model some financial returns. I based my code on formulas found on <a href="https://en.wikipedia.org/wiki/Skewed_generalized_t_distribution" rel="nofollow noreferrer">Wikipedia</a>, and I used the Beta distribution from scipy.</p>
  
  <pre><code>from scipy.special import beta
  import numpy as np
  from math import sqrt
  
  def sgt(x, params):
  # This function accepts an array of 5 parameters [mu, sigma, lambda, p, q]  
      mu, sigma, lam, p, q = params
  
      v = (q**(-1/p)) / (sqrt((3*lam*lam + 1)*beta(3/p, q-2/p)/beta(1/p, q) - 4*lam*lam*(beta(2/p, q-1/p)/(beta(1/p, q)))**2))
      m = 2*v*sigma*lam*q**(1/p)*beta(2/p, q - 1/p) / beta(1/p, q)
      fx = p / (2*v*sigma*(q**(1/p))*beta(1/p, q)*((abs(x-mu+m)**p/(q*(v*sigma)**p*(lam*np.sign(x-mu+m)+1)**p + 1)+1)**(1/p + q)))
  
      return fx
  </code></pre>
  
  <p>Now, the function seems to work perfectly fine for some sets of parameters, but terribly for other sets of parameters.</p>
  
  <p>For example:</p>
  
  <pre><code>dx = 0.001
  x_axis = np.arange(-10, 10, dx)
  
  ok_parameters = [0, 2, 0, 3, 8]
  bad_parameters = [0, 2, 0, 1.05, 2.1]
  
  ok_distribution = sgt(x_axis, ok_parameters)
  bad_distribution = sgt(x_axis, bad_parameters)
  </code></pre>
  
  <p>If I try to compute the integrals of those two numbers:</p>
  
  <pre><code>a = np.sum(ok_distribution*dx)
  b = np.sum(bad_distribution*dx)
  </code></pre>
  
  <p>I obtain the results a = 1.0013233154393804 and b = 2.2799746093533346.
  Now, in theory both of these should be 1, but I assume since I approximated the integral the value won't always be exactly 1. In the second case however I don't understand why the value is so high.</p>
  
  <p>Does anyone know what the issue is?</p>
  
  <p><a href="https://i.stack.imgur.com/pFZgA.png" rel="nofollow noreferrer">These are the graphs of the ok distribution (blue) and bad distribution (orange)</a></p>