Skip to content

Commit bdc9eff

Browse files
committed
Add Statistics!ChiSquare operator implemented with Apache Commons
Math3. [Feature]
1 parent da543f2 commit bdc9eff

8 files changed

+124
-9
lines changed

.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
<classpathentry kind="lib" path="lib/jgrapht-core-1.5.1.jar"/>
1515
<classpathentry kind="lib" path="lib/slf4j-api-1.7.30.jar"/>
1616
<classpathentry kind="lib" path="lib/commons-lang3-3.12.0.jar"/>
17+
<classpathentry kind="lib" path="lib/commons-math3-3.6.1.jar"/>
1718
<classpathentry kind="output" path="build"/>
1819
</classpath>

build.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</target>
2929

3030
<target name="compile" depends="download" description="compile the java module overwrites">
31-
<javac srcdir="${src}" destdir="${build}/modules" classpath="${tlc}/tla2tools.jar:${lib}/gson-2.8.6.jar:${lib}/jgrapht-core-1.5.1.jar:${lib}/jungrapht-layout-1.4-SNAPSHOT.jar:${lib}/slf4j-api-1.7.30.jar:${lib}/slf4j-nop-1.7.30.jar:${lib}/commons-lang3-3.12.0.jar"
31+
<javac srcdir="${src}" destdir="${build}/modules" classpath="${tlc}/tla2tools.jar:${lib}/gson-2.8.6.jar:${lib}/jgrapht-core-1.5.1.jar:${lib}/jungrapht-layout-1.4-SNAPSHOT.jar:${lib}/slf4j-api-1.7.30.jar:${lib}/slf4j-nop-1.7.30.jar:${lib}/commons-lang3-3.12.0.jar:${lib}/commons-math3-3.6.1.jar"
3232
source="1.8"
3333
target="1.8"
3434
includeantruntime="false"/>
@@ -79,6 +79,11 @@
7979
<include name="**/*.class"/>
8080
</patternset>
8181
</unzip>
82+
<unzip src="lib/commons-math3-3.6.1.jar" dest="${build}/deps">
83+
<patternset>
84+
<include name="**/*.class"/>
85+
</patternset>
86+
</unzip>
8287
<jar jarfile="${dist}/CommunityModules-deps-${timestamp}.jar">
8388
<fileset dir="${build}/modules/"
8489
includes="**/*.class"/>
@@ -123,6 +128,7 @@
123128
<pathelement location="${lib}/slf4j-api-1.7.30.jar" />
124129
<pathelement location="${lib}/slf4j-nop-1.7.30.jar" />
125130
<pathelement location="${lib}/commons-lang3-3.12.0.jar" />
131+
<pathelement location="${lib}/commons-math3-3.6.1.jar" />
126132
<!-- The jar that has just been built by the dist target. -->
127133
<pathelement location="${dist}/CommunityModules-${timestamp}.jar" />
128134
</classpath>

lib/commons-math3-3.6.1.jar

2.11 MB
Binary file not shown.

modules/Statistics.tla

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
------------------------- MODULE Statistics -------------------------
2+
EXTENDS Naturals
3+
4+
ChiSquare(expected, actual, alpha) ==
5+
(***************************************************************************)
6+
(* Performs a Chi-square goodness of fit test evaluating the null *)
7+
(* hypothesis that the observed counts conform to the frequency *)
8+
(* distribution described by the expected counts, with significance level *)
9+
(* alpha. *)
10+
(* From: *)
11+
(* https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/ *)
12+
(* org/apache/commons/math3/stat/inference/ChiSquareTest.html *)
13+
(***************************************************************************)
14+
\* TODO: specify the ChiSquare test
15+
TRUE
16+
17+
=========================================================================
18+
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2022 Microsoft Research. All rights reserved.
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+
* of the Software, and to permit persons to whom the Software is furnished to do
11+
* so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20+
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*
23+
* Contributors:
24+
* Markus Alexander Kuppe - initial API and implementation
25+
******************************************************************************/
26+
package tlc2.overrides;
27+
28+
import java.util.Arrays;
29+
30+
import org.apache.commons.math3.stat.inference.ChiSquareTest;
31+
32+
import tlc2.output.EC;
33+
import tlc2.tool.EvalException;
34+
import tlc2.value.Values;
35+
import tlc2.value.impl.BoolValue;
36+
import tlc2.value.impl.FcnRcdValue;
37+
import tlc2.value.impl.IntValue;
38+
import tlc2.value.impl.StringValue;
39+
import tlc2.value.impl.Value;
40+
41+
public class Statistics {
42+
43+
@TLAPlusOperator(identifier = "ChiSquare", module = "Statistics", warn = false)
44+
public static Value chiSquare(Value expected, Value actual, final Value alpha) {
45+
expected = expected.normalize().toFcnRcd();
46+
if (!(expected instanceof FcnRcdValue)) {
47+
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
48+
new String[] { "first", "ChiSquare", "function", Values.ppr(expected.toString()) });
49+
}
50+
actual = actual.normalize().toFcnRcd();
51+
if (!(actual instanceof FcnRcdValue)) {
52+
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR,
53+
new String[] { "second", "ChiSquare", "function", Values.ppr(actual.toString()) });
54+
}
55+
if (!(alpha instanceof StringValue)) {
56+
throw new EvalException(EC.TLC_MODULE_ARGUMENT_ERROR, new String[] { "third", "ChiSquare",
57+
"StringValue representing a double in (0, .5]", Values.ppr(alpha.toString()) });
58+
}
59+
60+
final double[] exp = Arrays.asList(((FcnRcdValue) expected).values).stream().map(v -> (IntValue) v)
61+
.mapToDouble(i -> Double.valueOf(i.val)).toArray();
62+
63+
final long[] act = Arrays.asList(((FcnRcdValue) actual).values).stream().map(v -> (IntValue) v)
64+
.mapToLong(i -> Long.valueOf(i.val)).toArray();
65+
66+
final double a = Double.valueOf(((StringValue) alpha).val.toString());
67+
68+
final ChiSquareTest chiSquareTest = new ChiSquareTest();
69+
if (chiSquareTest.chiSquareTest(exp, act, a)) {
70+
return BoolValue.ValFalse;
71+
}
72+
return BoolValue.ValTrue;
73+
}
74+
}

modules/tlc2/overrides/TLCOverrides.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ public class TLCOverrides implements ITLCOverrides {
4141
@Override
4242
public Class[] get() {
4343
try {
44-
// Remove `Json.resolves();` call when this Class is moved to `TLC`.
44+
// Remove `Json.resolves();` call when this Class is moved to `TLC`.
4545
Json.resolves();
4646
return new Class[] { IOUtils.class, SVG.class, SequencesExt.class, Json.class, Bitwise.class,
47-
FiniteSetsExt.class, Functions.class, CSV.class, Combinatorics.class, BagsExt.class, DyadicRationals.class };
47+
FiniteSetsExt.class, Functions.class, CSV.class, Combinatorics.class, BagsExt.class,
48+
DyadicRationals.class, Statistics.class };
4849
} catch (NoClassDefFoundError e) {
49-
// Remove this catch when this Class is moved to `TLC`.
50-
System.out.println(
51-
"gson dependencies of Json overrides not found, Json module won't work unless "
50+
// Remove this catch when this Class is moved to `TLC`.
51+
System.out.println("gson dependencies of Json overrides not found, Json module won't work unless "
5252
+ "the libraries in the lib/ folder of the CommunityModules have been added to the classpath of TLC.");
5353
}
54-
return new Class[] { IOUtils.class, SVG.class, SequencesExt.class, Bitwise.class,
55-
FiniteSetsExt.class, Functions.class, CSV.class, Combinatorics.class, BagsExt.class, DyadicRationals.class };
54+
return new Class[] { IOUtils.class, SVG.class, SequencesExt.class, Bitwise.class, FiniteSetsExt.class,
55+
Functions.class, CSV.class, Combinatorics.class, BagsExt.class, DyadicRationals.class,
56+
Statistics.class };
5657
}
5758
}

tests/AllTests.tla

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ EXTENDS SequencesExtTests,
1616
FoldsTests,
1717
GraphsTests,
1818
BagsExtTests,
19-
DyadicRationalsTests
19+
DyadicRationalsTests,
20+
StatisticsTests
2021

2122
===========================================

tests/StatisticsTests.tla

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
------------------------- MODULE StatisticsTests -------------------------
2+
EXTENDS Statistics
3+
4+
ASSUME LET T == INSTANCE TLC IN T!PrintT("StatisticsTests")
5+
6+
A ==
7+
[ i \in 1..6 |-> i ]
8+
9+
ASSUME( ChiSquare(A, A, "0.001"))
10+
ASSUME( ChiSquare(A, A, "0.01"))
11+
ASSUME( ChiSquare(A, A, "0.5"))
12+
ASSUME(~ChiSquare([ i \in 1..6 |-> 6 ], A, "0.45"))
13+
ASSUME(~ChiSquare([ i \in 1..6 |-> 6 ], A, "0.5"))
14+
=============================================================================

0 commit comments

Comments
 (0)