title: How to obtain a python scipy-type continuous rv distribution object that is bounded?
body: <p>I would like to define a bounded version of a continuous random variable distribution (say, an exponential, but I might want to use others as well). The bounds are 0 and 1. I would like to </p>
  
  <ol>
  <li>draw random variates (as done by <code>scipy.stats.rv_continuous.rvs</code>), </li>
  <li>use the ppf (percentage point function) (as done by <code>scipy.stats.rv_continuous.ppf</code>), and possibly</li>
  <li>use the cdf (cumulative density function) (as done by <code>scipy.stats.rv_continuous.cdf</code>)</li>
  </ol>
  
  <p><strong>Possible approaches I can think of:</strong></p>
  
  <ul>
  <li><p>Getting random variates in an ad hoc way is not difficult</p>
  
  <pre><code>import scipy.stats
  d = scipy.stats.expon(0, 3/10.)    # an exponential distribution as an example 
  rv = d.rvs(size=target_number_of_rv)
  rv = rv[0=&lt;rv]
  rv = rv[rv&lt;=1]
  while len(rv) &lt; target_number_of_rv:
      rv += d.rvs(1)
      rv = rv[0=&lt;rv]
      rv = rv[rv&lt;=1]
  </code></pre>
  
  <p>but 1) this is non-generic and potentially error-prone and 2) it does not help with the ppf or cdf.</p></li>
  <li><p>Subclassing scipy.stats.rv_continuous, as <a href="https://scicomp.stackexchange.com/a/1659">is done here</a> and <a href="https://stackoverflow.com/q/17093318/1735215">here</a>. Thereby, the ppf of scipy.stats.rv_continuous can be used. The drawback is that it requires the pdf (not just a pre-defined rv_continuous object or the pdf of the unbounded distribution and the bounds), and if this is wrong, cdf and ppf and everything else will be wrong as well. </p></li>
  <li><p>Designing a class that cares for applying the bounds to the rv generation and for correcting the value of the ppf obtained from the unbounded object in scipy.stats. A drawback is that this is non-generic and error-prone as well and that it may be difficult to correct the ppf. My feeling is that the value of the cdf of the unbounded distribution could be scaled by what share of probability mass is out of the bounds (in total, lower and upper), but I may be wrong. That would be for lower and upper bounds l and u and any valid quantile x (with l&lt;=x&lt;=u): (cdf(x)-cdf(l))/(cdf(u)-cdf(l)). Obtaining the ppf would, however, require to invert the resulting function.</p></li>
  </ul>
  
  <p>My feeling is that there might be a better and more generic way to do this. Is there? Maybe with sympy? Maybe by somehow obtaining the function object of the unbounded cdf and modifying it directly?  </p>
  
  <p>Python is version: 3.6.2, scipy is version 0.19.1.</p>