|
| 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 | +} |
0 commit comments