forked from hoxo-m/densratio_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_RuLSIF.py
61 lines (50 loc) · 2.54 KB
/
test_RuLSIF.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import unittest
from scipy.stats import norm, multivariate_normal
from numpy import linspace, mgrid
from .context import densratio
class BasicTestSuite(unittest.TestCase):
"""Basic test cases."""
def test_alphadensratio_1d_1(self):
x = norm.rvs(size=200, loc=0, scale=1./8, random_state=71)
y = norm.rvs(size=200, loc=0, scale=1./2, random_state=71)
result = densratio(x, y, alpha=0)
self.assertIsNotNone(result)
density_ratio = result.compute_density_ratio(linspace(-1, 3))
self.assertTrue((density_ratio >= 0).all())
def test_alphadensratio_1d_2(self):
x = norm.rvs(size=200, loc=0, scale=1./8, random_state=71)
y = norm.rvs(size=200, loc=0, scale=1./2, random_state=71)
result = densratio(x, y, alpha=0.5)
self.assertIsNotNone(result)
density_ratio = result.compute_density_ratio(linspace(-1, 3))
self.assertTrue((density_ratio >= 0).all())
def test_alphadensratio_1d_2(self):
x = norm.rvs(size=200, loc=0, scale=1./8, random_state=71)
y = norm.rvs(size=200, loc=0, scale=1./2, random_state=71)
result = densratio(x, y, alpha=0.5)
self.assertIsNotNone(result)
density_ratio = result.compute_density_ratio(linspace(-1, 3))
self.assertTrue((density_ratio >= 0).all())
def test_alphadensratio_1d_3(self):
x = norm.rvs(size=200, loc=0, scale=1./8, random_state=71)
y = norm.rvs(size=200, loc=0, scale=1./2, random_state=71)
result = densratio(x, y, alpha=1)
self.assertIsNotNone(result)
density_ratio = result.compute_density_ratio(linspace(-1, 3))
self.assertTrue((density_ratio >= 0).all())
def test_alphadensratio_2d(self):
x = multivariate_normal.rvs(size=300, mean=[1, 1], cov=[[1./8, 0], [0, 2]], random_state=71)
y = multivariate_normal.rvs(size=300, mean=[1, 1], cov=[[1./2, 0], [0, 2]], random_state=71)
result = densratio(x, y, alpha=0.5)
self.assertIsNotNone(result)
space_range = slice(-1, 3, 50j)
space_2d = mgrid[space_range, space_range].reshape(2, -1).T
density_ratio = result.compute_density_ratio(space_2d)
self.assertTrue((density_ratio >= 0).all())
def test_densratio_dimension_error(self):
x = norm.rvs(size=200, loc=0, scale=1./8, random_state=71)
y = multivariate_normal.rvs(size=300, mean=[1, 1], cov=[[1./2, 0], [0, 2]], random_state=71)
with self.assertRaises(ValueError):
densratio(x, y, alpha=0.7)
if __name__ == '__main__':
unittest.main()