| 1 |
import numpy |
| 2 |
import warnings |
| 3 |
|
| 4 |
import csb.test as test |
| 5 |
|
| 6 |
from csb.numeric import exp, log_sum_exp, log |
| 7 |
from csb.statistics.rand import truncated_gamma, truncated_normal, sample_from_histogram |
| 8 |
from csb.statistics.pdf import Normal |
| 9 |
from csb.statistics import density |
| 10 |
|
| 11 |
|
| 12 |
@test.functional |
| 13 |
class TestRand(test.Case): |
| 14 |
|
| 15 |
def testTruncatedGamma(self): |
| 16 |
alpha = 2. |
| 17 |
beta = 1. |
| 18 |
x_min = 0.1 |
| 19 |
x_max = 5. |
| 20 |
|
| 21 |
x = truncated_gamma(10000, alpha, beta, x_min, x_max) |
| 22 |
|
| 23 |
self.assertTrue((x <= x_max).all()) |
| 24 |
self.assertTrue((x >= x_min).all()) |
| 25 |
|
| 26 |
hy, hx = density(x, 100) |
| 27 |
hx = 0.5 * (hx[1:] + hx[:-1]) |
| 28 |
hy = hy.astype('d') |
| 29 |
|
| 30 |
with warnings.catch_warnings(record=True) as warning: |
| 31 |
warnings.simplefilter("always") |
| 32 |
|
| 33 |
hy /= (hx[1] - hx[0]) * hy.sum() |
| 34 |
|
| 35 |
self.assertLessEqual(len(warning), 1) |
| 36 |
|
| 37 |
if len(warning) == 1: |
| 38 |
warning = warning[0] |
| 39 |
self.assertEqual(warning.category, RuntimeWarning) |
| 40 |
self.assertTrue(str(warning.message).startswith('divide by zero encountered')) |
| 41 |
|
| 42 |
x = numpy.linspace(x_min, x_max, 1000) |
| 43 |
p = (alpha - 1) * log(x) - beta * x |
| 44 |
p -= log_sum_exp(p) |
| 45 |
p = exp(p) / (x[1] - x[0]) |
| 46 |
|
| 47 |
def testTruncatedNormal(self): |
| 48 |
|
| 49 |
mu = 2. |
| 50 |
sigma = 1. |
| 51 |
x_min = -1. |
| 52 |
x_max = 5. |
| 53 |
|
| 54 |
x = truncated_normal(10000, mu, sigma, x_min, x_max) |
| 55 |
|
| 56 |
self.assertAlmostEqual(numpy.mean(x), mu, delta=1e-1) |
| 57 |
self.assertAlmostEqual(numpy.var(x), sigma, delta=1e-1) |
| 58 |
|
| 59 |
self.assertTrue((x <= x_max).all()) |
| 60 |
self.assertTrue((x >= x_min).all()) |
| 61 |
|
| 62 |
hy, hx = density(x, 100) |
| 63 |
hx = 0.5 * (hx[1:] + hx[:-1]) |
| 64 |
hy = hy.astype('d') |
| 65 |
|
| 66 |
with warnings.catch_warnings(record=True) as warning: |
| 67 |
warnings.simplefilter("always") |
| 68 |
|
| 69 |
hy /= (hx[1] - hx[0]) * hy.sum() |
| 70 |
|
| 71 |
self.assertLessEqual(len(warning), 1) |
| 72 |
|
| 73 |
if len(warning) == 1: |
| 74 |
warning = warning[0] |
| 75 |
self.assertEqual(warning.category, RuntimeWarning) |
| 76 |
self.assertTrue(str(warning.message).startswith('divide by zero encountered')) |
| 77 |
|
| 78 |
x = numpy.linspace(mu - 5 * sigma, mu + 5 * sigma, 1000) |
| 79 |
|
| 80 |
p = -0.5 * (x - mu) ** 2 / sigma ** 2 |
| 81 |
p -= log_sum_exp(p) |
| 82 |
p = exp(p) / (x[1] - x[0]) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
def testSampleFromHistogram(self): |
| 87 |
mu = 5. |
| 88 |
sigma = 1. |
| 89 |
|
| 90 |
normal = Normal(mu, sigma) |
| 91 |
|
| 92 |
x = normal.random(10000) |
| 93 |
hx, p = density(x, 100) |
| 94 |
|
| 95 |
samples = hx[sample_from_histogram(p, n_samples=10000)] |
| 96 |
|
| 97 |
self.assertAlmostEqual(mu, numpy.mean(samples), delta=0.5) |
| 98 |
self.assertAlmostEqual(sigma, numpy.std(samples), delta=0.5) |
| 99 |
|
| 100 |
|
| 101 |
if __name__ == '__main__': |
| 102 |
|
| 103 |
test.Console() |