1 | package tudelft.mentalhealth.motivatepersisting;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import org.junit.Before;
|
---|
11 | import org.junit.Test;
|
---|
12 |
|
---|
13 | import tudelft.utilities.statistic.WeightedSet;
|
---|
14 |
|
---|
15 | public class ProbOfCategoriesPerSituationTest {
|
---|
16 | private ProbOfCategoriesPerSituation probs;
|
---|
17 | private int NTESTS = 3000;
|
---|
18 |
|
---|
19 | @Before
|
---|
20 | public void before() throws IOException {
|
---|
21 | probs = ProbOfCategoriesPerSituation.instance();
|
---|
22 |
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Test
|
---|
26 | public void smokeTest() throws IOException {
|
---|
27 | System.out.println(probs);
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Test
|
---|
31 | public void testSomeTableEntries() throws IOException {
|
---|
32 | assertEquals((Double) 0.77, probs.get(Category.MOTIVATION,
|
---|
33 | Subcategory.ALL, new Situation(PclTrend.DROPPING, Trust.HIGH)));
|
---|
34 | assertEquals((Double) 0.01, probs.get(Category.FUTURE, Subcategory.PCL,
|
---|
35 | new Situation(PclTrend.DROPPING, Trust.MEDIUM)));
|
---|
36 | assertEquals((Double) 0.61, probs.get(Category.GIVEPERSPECTIVE,
|
---|
37 | Subcategory.ALL, new Situation(PclTrend.RISING, Trust.HIGH)));
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Test
|
---|
41 | public void getRandomSubTest() {
|
---|
42 | // EMPATHY has only 1 subcat so this should be trivial
|
---|
43 | assertEquals(Subcategory.NEGATIVE,
|
---|
44 | probs.getweightedRandomSubcategory(Category.EMPATHY,
|
---|
45 | new Situation(PclTrend.DROPPING, Trust.HIGH)));
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Test
|
---|
49 | public void getRandomSubTest2() {
|
---|
50 | int compl = 0, goon = 0, its = 0, hold = 0;
|
---|
51 | for (int n = 0; n < NTESTS; n++) {
|
---|
52 | Subcategory subcat = probs.getweightedRandomSubcategory(
|
---|
53 | Category.MOTIVATION,
|
---|
54 | new Situation(PclTrend.RISING, Trust.MEDIUM));
|
---|
55 | switch (subcat) {
|
---|
56 | case COMPLIMENT:
|
---|
57 | compl++;
|
---|
58 | break;
|
---|
59 | case GOONLIKETHIS:
|
---|
60 | goon++;
|
---|
61 | break;
|
---|
62 | case ITSGOINGWELL:
|
---|
63 | its++;
|
---|
64 | break;
|
---|
65 | case HOLDON:
|
---|
66 | hold++;
|
---|
67 | break;
|
---|
68 | default:
|
---|
69 | throw new IllegalStateException(
|
---|
70 | "Unexpected subcategory received " + subcat);
|
---|
71 | }
|
---|
72 |
|
---|
73 | }
|
---|
74 | // check against table: 0.03, 0.01, 0.04, 0.15 (sums to 0.23)
|
---|
75 | isExpected(Subcategory.COMPLIMENT.name(), compl, 0.03 / 0.23);
|
---|
76 | isExpected(Subcategory.GOONLIKETHIS.name(), goon, 0.01 / 0.23);
|
---|
77 | isExpected(Subcategory.ITSGOINGWELL.name(), its, 0.04 / 0.23);
|
---|
78 | isExpected(Subcategory.HOLDON.name(), hold, 0.15 / 0.23);
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Test
|
---|
83 | public void testGetRandom2() {
|
---|
84 |
|
---|
85 | Map<String, Double> values = new HashMap<>();
|
---|
86 | values.put("a", 0.2);
|
---|
87 | values.put("b", 0.3);
|
---|
88 | values.put("c", 0.5);
|
---|
89 | WeightedSet<String> map1 = new WeightedSet<String>(values);
|
---|
90 |
|
---|
91 | int as = 0, bs = 0, cs = 0;
|
---|
92 | for (int n = 0; n < NTESTS; n++) {
|
---|
93 | switch (map1.getRandom()) {
|
---|
94 | case "a":
|
---|
95 | as++;
|
---|
96 | break;
|
---|
97 | case "b":
|
---|
98 | bs++;
|
---|
99 | break;
|
---|
100 | case "c":
|
---|
101 | cs++;
|
---|
102 | break;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | isExpected("a", as, 0.2);
|
---|
106 | isExpected("b", bs, 0.3);
|
---|
107 | isExpected("c", cs, 0.5);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Check that the number of accurences of value is with 99% confidence range
|
---|
112 | *
|
---|
113 | * @param value the value that was tested.
|
---|
114 | * @param amount the number of occurences of value
|
---|
115 | * @param p the probability that value occured in [0,1]
|
---|
116 | */
|
---|
117 | private void isExpected(String value, int amount, double p) {
|
---|
118 | // approximate binomial with normal distri.
|
---|
119 | // mean = n p (n=#samples, p=probability of drawing X)
|
---|
120 | // variance sigma^2 = n p (1-p)
|
---|
121 | // 99% of the trials should be within mean - 2sigma, mean+2sigma.
|
---|
122 | double mean = NTESTS * p;
|
---|
123 | double sigma2 = 2 * Math.sqrt(NTESTS * p * (1 - p));
|
---|
124 | assertTrue(
|
---|
125 | "random draws of " + value
|
---|
126 | + " were out of 99% confidence interval. Expected ["
|
---|
127 | + (mean - sigma2) + "," + (mean + sigma2)
|
---|
128 | + "], actual " + amount,
|
---|
129 | Math.abs(amount - mean) < sigma2);
|
---|
130 |
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Test
|
---|
134 | public void validateTableTest() {
|
---|
135 | // all categories that have p>0 for some Situation must have at least
|
---|
136 | // one
|
---|
137 | // subcategory with p>0
|
---|
138 | for (PclTrend pcl : PclTrend.values()) {
|
---|
139 | for (Trust trust : Trust.values()) {
|
---|
140 | Situation situation = new Situation(pcl, trust);
|
---|
141 | for (Category category : Category.values()) {
|
---|
142 | if (!category.getSubCategories().isEmpty() && probs
|
---|
143 | .get(category, Subcategory.ALL, situation) > 0) {
|
---|
144 | checkHasSelectableSubcategory(category, situation);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Check at least one of the subcategories of given category has p>0
|
---|
154 | *
|
---|
155 | * @param category the {@link Category} to check
|
---|
156 | * @param situation the {@link Situation} to check
|
---|
157 | */
|
---|
158 | private void checkHasSelectableSubcategory(Category category,
|
---|
159 | Situation situation) {
|
---|
160 | for (Subcategory subcat : category.getSubCategories()) {
|
---|
161 | if (probs.get(category, subcat, situation) > 0.)
|
---|
162 | return;
|
---|
163 | }
|
---|
164 | throw new IllegalStateException(
|
---|
165 | "Category " + category + " in situation " + situation
|
---|
166 | + " has no selectable subcategories");
|
---|
167 | }
|
---|
168 | }
|
---|