Returns the value corresponding to the probability from the cumulative distribution function. Parameters ---------- p : array-like or float The probability value, must be between 0 and 1. Returns
(self, p)
| 134 | return prob |
| 135 | |
| 136 | def sample(self, p): |
| 137 | """ Returns the value corresponding to the probability from the cumulative |
| 138 | distribution function. |
| 139 | |
| 140 | Parameters |
| 141 | ---------- |
| 142 | p : array-like or float |
| 143 | The probability value, must be between 0 and 1. |
| 144 | |
| 145 | Returns |
| 146 | ------- |
| 147 | xval : array-like or float |
| 148 | The corresponding x-value. |
| 149 | |
| 150 | Raises |
| 151 | ------ |
| 152 | ValueError |
| 153 | If p is outside the range [0, 1]. |
| 154 | |
| 155 | Example |
| 156 | ------- |
| 157 | Use this method to sample the distribution with randoms numbers. For |
| 158 | example:: |
| 159 | |
| 160 | import numpy as np |
| 161 | import matplotlib.pyplot as plt |
| 162 | x = np.linspace(0, 200, 200) |
| 163 | y = np.exp(-((x - 50.0)/20)**2) |
| 164 | dist = Distribution(x, y) |
| 165 | drawn = dist.sample(np.random.uniform(0, 1, 10000)) |
| 166 | plt.hist(drawn) |
| 167 | """ |
| 168 | if not allinrange(p, (0.0, 1.0)): |
| 169 | raise ValueError("p is outside valid range.") |
| 170 | |
| 171 | if self.hist: |
| 172 | idx = np.searchsorted(self._cdf, p) |
| 173 | try: |
| 174 | return self._x[idx] |
| 175 | except IndexError: |
| 176 | return self._x[-1] |
| 177 | else: |
| 178 | xval = np.interp(p, self._cdf, self._x, left=np.nan, right=np.nan) |
| 179 | if xval.size == 1: |
| 180 | xval = xval.tolist() # actually a float |
| 181 | return xval |
| 182 | |
| 183 | @classmethod |
| 184 | def from_functions(cls, x, callables, hist=False): |