diff --git a/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java b/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java index b6c3abbd9e..65d4b16f16 100644 --- a/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java +++ b/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats.java @@ -1,12 +1,12 @@ /******************************************************************************************************* * - * msi.gaml.operators.Stats.java, in plugin msi.gama.core, is part of the source code of the GAMA modeling and - * simulation platform (v. 1.8.1) + * Stats.java, in ummisco.gaml.extensions.stats, is part of the source code of the + * GAMA modeling and simulation platform (v.1.8.2). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. - * + * ********************************************************************************************************/ package ummisco.gaml.extensions.stats; @@ -29,13 +29,9 @@ import com.google.common.collect.Ordering; -import msi.gama.common.interfaces.IKeyword; -import msi.gama.common.preferences.GamaPreferences; import msi.gama.metamodel.shape.GamaPoint; - import msi.gama.precompiler.GamlAnnotations.doc; import msi.gama.precompiler.GamlAnnotations.example; -import msi.gama.precompiler.GamlAnnotations.no_test; import msi.gama.precompiler.GamlAnnotations.operator; import msi.gama.precompiler.GamlAnnotations.test; import msi.gama.precompiler.GamlAnnotations.usage; @@ -57,10 +53,6 @@ import msi.gaml.operators.Cast; import msi.gaml.types.IType; import msi.gaml.types.Types; -import rcaller.RCaller; -import rcaller.RCode; -import rcaller.exception.ExecutionException; -import rcaller.exception.ParseException; /** * Written by drogoul Modified on 15 janv. 2011 @@ -71,41 +63,76 @@ @SuppressWarnings ({ "unchecked", "rawtypes" }) public class Stats { + /** + * The Class Instance. + */ public static class Instance extends DoublePoint { /** * */ private static final long serialVersionUID = 1L; + + /** The id. */ int id; + /** + * Instantiates a new instance. + * + * @param id the id + * @param point the point + */ public Instance(final int id, final double[] point) { super(point); this.id = id; } - public int getId() { - return id; - } + /** + * Gets the id. + * + * @return the id + */ + public int getId() { return id; } - public void setId(final int id) { - this.id = id; - } + /** + * Sets the id. + * + * @param id the new id + */ + public void setId(final int id) { this.id = id; } } + /** + * The Class DataSet. + */ private static class DataSet { + /** The Constant DEFAULT_CAPACITY. */ private static final int DEFAULT_CAPACITY = 50; + + /** The Constant GROWTH_RATE. */ private static final double GROWTH_RATE = 1.5d; + /** The data set. */ double[] dataSet; + + /** The data set size. */ int dataSetSize = 0; + /** The total. */ private double total = 0; + + /** The product. */ private double product = 1; + + /** The reciprocal sum. */ private double reciprocalSum = 0; + + /** The minimum. */ private double minimum = Double.MAX_VALUE; + + /** The maximum. */ private double maximum = Double.MIN_VALUE; /** @@ -146,6 +173,11 @@ public void addValue(final double value) { ++dataSetSize; } + /** + * Update stats with new value. + * + * @param value the value + */ private void updateStatsWithNewValue(final double value) { total += value; product *= value; @@ -159,9 +191,7 @@ private void updateStatsWithNewValue(final double value) { * * @return The size of the data set. */ - public final int getSize() { - return dataSetSize; - } + public final int getSize() { return dataSetSize; } /** * Determines the median value of the data set. @@ -187,9 +217,7 @@ public final double getMedian() { * @throws EmptyDataSetException * If the data set is empty. */ - public final double getProduct() { - return product; - } + public final double getProduct() { return product; } /** * The arithemthic mean of an n-element set is the sum of all the elements divided by n. The arithmetic mean is @@ -200,9 +228,7 @@ public final double getProduct() { * @throws EmptyDataSetException * If the data set is empty. */ - public final double getArithmeticMean() { - return total / dataSetSize; - } + public final double getArithmeticMean() { return total / dataSetSize; } /** * The geometric mean of an n-element set is the nth-root of the product of all the elements. The geometric mean @@ -214,9 +240,7 @@ public final double getArithmeticMean() { * @throws EmptyDataSetException * If the data set is empty. */ - public final double getGeometricMean() { - return Math.pow(product, 1.0d / dataSetSize); - } + public final double getGeometricMean() { return Math.pow(product, 1.0d / dataSetSize); } /** * The harmonic mean of an n-element set is {@literal n} divided by the sum of the reciprocals of the values @@ -230,9 +254,7 @@ public final double getGeometricMean() { * @throws EmptyDataSetException * If the data set is empty. */ - public final double getHarmonicMean() { - return dataSetSize / reciprocalSum; - } + public final double getHarmonicMean() { return dataSetSize / reciprocalSum; } /** * Calculates the mean absolute deviation of the data set. This is the average (absolute) amount that a single @@ -248,9 +270,7 @@ public final double getHarmonicMean() { public final double getMeanDeviation() { final double mean = getArithmeticMean(); double diffs = 0; - for (int i = 0; i < dataSetSize; i++) { - diffs += Math.abs(mean - dataSet[i]); - } + for (int i = 0; i < dataSetSize; i++) { diffs += Math.abs(mean - dataSet[i]); } return diffs / dataSetSize; } @@ -266,9 +286,7 @@ public final double getMeanDeviation() { * @throws EmptyDataSetException * If the data set is empty. */ - public final double getVariance() { - return sumSquaredDiffs() / getSize(); - } + public final double getVariance() { return sumSquaredDiffs() / getSize(); } /** * Helper method for variance calculations. @@ -298,16 +316,18 @@ private double sumSquaredDiffs() { * @throws EmptyDataSetException * If the data set is empty. */ - public final double getStandardDeviation() { - return Math.sqrt(getVariance()); - } + public final double getStandardDeviation() { return Math.sqrt(getVariance()); } + /** + * Gets the stops. + * + * @param nb the nb + * @return the stops + */ public double[] getStops(final int nb) { final double interval = (maximum - minimum) / nb; final double[] result = new double[nb - 1]; - for (int i = 1; i < nb; i++) { - result[i - 1] = minimum + i * interval; - } + for (int i = 1; i < nb; i++) { result[i - 1] = minimum + i * interval; } return result; } @@ -343,6 +363,13 @@ public double[] getStops(final int nb) { // } } + /** + * From. + * + * @param scope the scope + * @param values the values + * @return the data set + */ private static DataSet from(final IScope scope, final IContainer values) { final DataSet d = new DataSet(values.length(scope)); for (final Object o : values.iterable(scope)) { @@ -351,6 +378,14 @@ private static DataSet from(final IScope scope, final IContainer values) { return d; } + /** + * Split. + * + * @param the generic type + * @param scope the scope + * @param list the list + * @return the i list + */ @operator ( value = "split", can_be_const = true, @@ -373,6 +408,15 @@ public static IList> split(final IScope scope, final return split_in(scope, list, nb); } + /** + * Split in. + * + * @param the generic type + * @param scope the scope + * @param list the list + * @param nb the nb + * @return the i list + */ @operator ( value = "split_in", can_be_const = true, @@ -393,6 +437,16 @@ public static IList> split_in(final IScope scope, fi return split_in(scope, list, nb, true); } + /** + * Split in. + * + * @param the generic type + * @param scope the scope + * @param list the list + * @param nb the nb + * @param strict the strict + * @return the i list + */ @operator ( value = "split_in", can_be_const = true, @@ -421,6 +475,15 @@ public static IList> split_in(final IScope scope, fi return split_using(scope, list, stops); } + /** + * Split using. + * + * @param the generic type + * @param scope the scope + * @param list the list + * @param stops the stops + * @return the i list + */ @operator ( value = "split_using", can_be_const = true, @@ -442,6 +505,16 @@ public static IList> split_using(final IScope scope, return split_using(scope, list, stops, true); } + /** + * Split using. + * + * @param the generic type + * @param scope the scope + * @param list the list + * @param stops the stops + * @param strict the strict + * @return the i list + */ @operator ( value = "split_using", can_be_const = true, @@ -470,9 +543,7 @@ public static IList> split_using(final IScope scope, d.addValue(Double.MAX_VALUE); final IType numberType = list.getGamlType().getContentType(); final IList> result = GamaListFactory.createWithoutCasting(Types.LIST.of(numberType)); - for (int i = 0; i < d.dataSetSize; i++) { - result.add(GamaListFactory.createWithoutCasting(numberType)); - } + for (int i = 0; i < d.dataSetSize; i++) { result.add(GamaListFactory.createWithoutCasting(numberType)); } for (final T o : list) { for (int i = 0; i < d.dataSetSize; i++) { if (strict ? o.doubleValue() < d.dataSet[i] : o.doubleValue() <= d.dataSet[i]) { @@ -484,6 +555,13 @@ public static IList> split_using(final IScope scope, return result; } + /** + * Max. + * + * @param scope the scope + * @param l the l + * @return the object + */ @operator ( value = "max", can_be_const = true, @@ -542,6 +620,13 @@ public static Object max(final IScope scope, final IContainer l) { return maxNum == null ? maxPoint : maxNum; } + /** + * Min. + * + * @param scope the scope + * @param l the l + * @return the object + */ @operator ( value = "min", can_be_const = true, @@ -601,6 +686,13 @@ public static Object min(final IScope scope, final IContainer l) { return minNum == null ? minPoint : minNum; } + /** + * Product. + * + * @param scope the scope + * @param l the l + * @return the object + */ @SuppressWarnings ("null") @operator ( value = { "mul", "product" }, @@ -669,6 +761,13 @@ public static Object product(final IScope scope, final IContainer l) { // TODO Penser a faire ces calculs sur les points, egalement (et les entiers // ?) + /** + * Op median. + * + * @param scope the scope + * @param values the values + * @return the object + */ @operator ( value = "median", can_be_const = true, @@ -693,9 +792,7 @@ public static Object opMedian(final IScope scope, final IContainer values) { case IType.INT: case IType.FLOAT: final DataSet d2 = new DataSet(); - for (final Object o : values.iterable(scope)) { - d2.addValue(Cast.asFloat(scope, o)); - } + for (final Object o : values.iterable(scope)) { d2.addValue(Cast.asFloat(scope, o)); } final Number result = d2.getSize() == 0 ? 0.0 : d2.getMedian(); return contentType.cast(scope, result, null, false); case IType.POINT: @@ -724,15 +821,20 @@ public static Object opMedian(final IScope scope, final IContainer values) { return new GamaColor((int) r.getMedian(), (int) g.getMedian(), (int) b.getMedian(), 0); default: final DataSet d = new DataSet(); - for (final Object o : values.iterable(scope)) { - d.addValue(Cast.asFloat(scope, o)); - } + for (final Object o : values.iterable(scope)) { d.addValue(Cast.asFloat(scope, o)); } final Number n = d.getSize() == 0 ? 0.0 : d.getMedian(); return Cast.asFloat(scope, n); } } + /** + * Op st dev. + * + * @param scope the scope + * @param values the values + * @return the double + */ @operator ( value = "standard_deviation", can_be_const = true, @@ -755,6 +857,13 @@ public static Double opStDev(final IScope scope, final IContainer values) { return d.getStandardDeviation(); } + /** + * Op geom mean. + * + * @param scope the scope + * @param values the values + * @return the double + */ @operator ( value = "geometric_mean", can_be_const = true, @@ -776,6 +885,13 @@ public static Double opGeomMean(final IScope scope, final IContainer values) { return d.getGeometricMean(); } + /** + * Op harmonic mean. + * + * @param scope the scope + * @param values the values + * @return the double + */ @operator ( value = "harmonic_mean", can_be_const = true, @@ -797,6 +913,13 @@ public static Double opHarmonicMean(final IScope scope, final IContainer values) return d.getHarmonicMean(); } + /** + * Op variance. + * + * @param scope the scope + * @param values the values + * @return the double + */ @operator ( value = "variance", can_be_const = true, @@ -817,6 +940,13 @@ public static Double opVariance(final IScope scope, final IContainer values) { return d.getVariance(); } + /** + * Op mean deviation. + * + * @param scope the scope + * @param values the values + * @return the double + */ @operator ( value = "mean_deviation", can_be_const = true, @@ -837,6 +967,15 @@ public static Double opMeanDeviation(final IScope scope, final IContainer values return d.getMeanDeviation(); } + /** + * Frequency of. + * + * @param scope the scope + * @param original the original + * @param filter the filter + * @return the i map + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = { "frequency_of" }, can_be_const = true, @@ -869,124 +1008,134 @@ public static IMap frequencyOf(final IScope scope, final IContainer original, fi return result; } - @operator ( - value = { "corR", "R_correlation" }, - can_be_const = false, - type = IType.FLOAT, - category = { IOperatorCategory.STATISTICAL }, - concept = { IConcept.STATISTIC }) - @doc ( - value = "returns the Pearson correlation coefficient of two given vectors (right-hand operands)" - + " in given variable (left-hand operand).", - special_cases = "if the lengths of two vectors in the right-hand aren't equal, returns 0", - examples = { @example ( - value = "list X <- [1, 2, 3];", - isExecutable = false), - @example ( - value = "list Y <- [1, 2, 4];", - isExecutable = false), - @example ( - value = "corR(X, Y)", - equals = "0.981980506061966", - isExecutable = false) }) - @no_test // because require R to be installed. - - public static Object getCorrelationR(final IScope scope, final IContainer l1, final IContainer l2) - throws GamaRuntimeException, ParseException, ExecutionException { - if (l1.length(scope) == 0 || l2.length(scope) == 0) return Double.valueOf(0d); - - if (l1.length(scope) != l2.length(scope)) return Double.valueOf(0d); - - final RCaller caller = new RCaller(); - final RCode code = new RCode(); - - final String RPath = GamaPreferences.External.LIB_R.value(scope).getPath(scope); - caller.setRscriptExecutable(RPath); - // caller.setRscriptExecutable("\"" + RPath + "\""); - // if ( java.lang.System.getProperty("os.name").startsWith("Mac") ) { - // caller.setRscriptExecutable(RPath); - // } - - final double[] vectorX = new double[l1.length(scope)]; - final double[] vectorY = new double[l2.length(scope)]; - - int i = 0; - for (final Object o : l1.iterable(scope)) { - vectorX[i++] = Double.parseDouble(o.toString()); - } - - i = 0; - for (final Object o : l2.iterable(scope)) { - vectorY[i++] = Double.parseDouble(o.toString()); - } - - code.addDoubleArray("vectorX", vectorX); - code.addDoubleArray("vectorY", vectorY); - - code.addRCode("corCoef<-cor(vectorX, vectorY, method='pearson')"); - caller.setRCode(code); - caller.runAndReturnResult("corCoef"); - - double[] results; - try { - results = caller.getParser().getAsDoubleArray("corCoef"); - } catch (final Exception ex) { - return 0.0; - } - - return results[0]; - } - - @operator ( - value = { "meanR", "R_mean" }, - can_be_const = false, - type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, - category = { IOperatorCategory.STATISTICAL }, - concept = { IConcept.STATISTIC }) - @doc ( - value = "returns the mean value of given vector (right-hand operand) in given variable (left-hand operand).", - examples = { @example ( - value = "list X <- [2, 3, 1];", - isExecutable = false), - @example ( - value = "meanR(X)", - equals = "2", - returnType = IKeyword.INT, - isExecutable = false), - @example ( - value = "meanR([2, 3, 1])", - equals = "2", - isExecutable = false) }) - @no_test - public static Object getMeanR(final IScope scope, final IContainer l) - throws GamaRuntimeException, ParseException, ExecutionException { - if (l.length(scope) == 0) return Double.valueOf(0d); - - double[] results; - final RCaller caller = new RCaller(); - final RCode code = new RCode(); - - final String RPath = GamaPreferences.External.LIB_R.value(scope).getPath(scope); - caller.setRscriptExecutable(RPath); - // caller.setRscriptExecutable("\"" + RPath + "\""); - // if ( java.lang.System.getProperty("os.name").startsWith("Mac") ) { - // caller.setRscriptExecutable(RPath); - // } - - final double[] data = new double[l.length(scope)]; - int i = 0; - for (final Object o : l.iterable(scope)) { - data[i++] = Double.parseDouble(o.toString()); - } - - code.addDoubleArray("data", data); - code.addRCode("mean<-mean(data)"); - caller.setRCode(code); - caller.runAndReturnResult("mean"); - results = caller.getParser().getAsDoubleArray("mean"); - return results[0]; - } - + // @operator ( + // value = { "corR", "R_correlation" }, + // can_be_const = false, + // type = IType.FLOAT, + // category = { IOperatorCategory.STATISTICAL }, + // concept = { IConcept.STATISTIC }) + // @doc ( + // value = "returns the Pearson correlation coefficient of two given vectors (right-hand operands)" + // + " in given variable (left-hand operand).", + // special_cases = "if the lengths of two vectors in the right-hand aren't equal, returns 0", + // examples = { @example ( + // value = "list X <- [1, 2, 3];", + // isExecutable = false), + // @example ( + // value = "list Y <- [1, 2, 4];", + // isExecutable = false), + // @example ( + // value = "corR(X, Y)", + // equals = "0.981980506061966", + // isExecutable = false) }) + // @no_test // because require R to be installed. + // + // public static Object getCorrelationR(final IScope scope, final IContainer l1, final IContainer l2) + // throws GamaRuntimeException, ParseException, ExecutionException { + // if (l1.length(scope) == 0 || l2.length(scope) == 0) return Double.valueOf(0d); + // + // if (l1.length(scope) != l2.length(scope)) return Double.valueOf(0d); + // + // final RCaller caller = new RCaller(); + // final RCode code = new RCode(); + // + // final String RPath = GamaPreferences.External.LIB_R.value(scope).getPath(scope); + // caller.setRscriptExecutable(RPath); + // // caller.setRscriptExecutable("\"" + RPath + "\""); + // // if ( java.lang.System.getProperty("os.name").startsWith("Mac") ) { + // // caller.setRscriptExecutable(RPath); + // // } + // + // final double[] vectorX = new double[l1.length(scope)]; + // final double[] vectorY = new double[l2.length(scope)]; + // + // int i = 0; + // for (final Object o : l1.iterable(scope)) { + // vectorX[i++] = Double.parseDouble(o.toString()); + // } + // + // i = 0; + // for (final Object o : l2.iterable(scope)) { + // vectorY[i++] = Double.parseDouble(o.toString()); + // } + // + // code.addDoubleArray("vectorX", vectorX); + // code.addDoubleArray("vectorY", vectorY); + // + // code.addRCode("corCoef<-cor(vectorX, vectorY, method='pearson')"); + // caller.setRCode(code); + // caller.runAndReturnResult("corCoef"); + // + // double[] results; + // try { + // results = caller.getParser().getAsDoubleArray("corCoef"); + // } catch (final Exception ex) { + // return 0.0; + // } + // + // return results[0]; + // } + + // @operator ( + // value = { "meanR", "R_mean" }, + // can_be_const = false, + // type = ITypeProvider.CONTENT_TYPE_AT_INDEX + 1, + // category = { IOperatorCategory.STATISTICAL }, + // concept = { IConcept.STATISTIC }) + // @doc ( + // value = "returns the mean value of given vector (right-hand operand) in given variable (left-hand operand).", + // examples = { @example ( + // value = "list X <- [2, 3, 1];", + // isExecutable = false), + // @example ( + // value = "meanR(X)", + // equals = "2", + // returnType = IKeyword.INT, + // isExecutable = false), + // @example ( + // value = "meanR([2, 3, 1])", + // equals = "2", + // isExecutable = false) }) + // @no_test + // public static Object getMeanR(final IScope scope, final IContainer l) + // throws GamaRuntimeException, ParseException, ExecutionException { + // if (l.length(scope) == 0) return Double.valueOf(0d); + // + // double[] results; + // final RCaller caller = new RCaller(); + // final RCode code = new RCode(); + // + // final String RPath = GamaPreferences.External.LIB_R.value(scope).getPath(scope); + // caller.setRscriptExecutable(RPath); + // // caller.setRscriptExecutable("\"" + RPath + "\""); + // // if ( java.lang.System.getProperty("os.name").startsWith("Mac") ) { + // // caller.setRscriptExecutable(RPath); + // // } + // + // final double[] data = new double[l.length(scope)]; + // int i = 0; + // for (final Object o : l.iterable(scope)) { + // data[i++] = Double.parseDouble(o.toString()); + // } + // + // code.addDoubleArray("data", data); + // code.addRCode("mean<-mean(data)"); + // caller.setRCode(code); + // caller.runAndReturnResult("mean"); + // results = caller.getParser().getAsDoubleArray("mean"); + // return results[0]; + // } + + /** + * D bscan apache. + * + * @param scope the scope + * @param data the data + * @param eps the eps + * @param minPts the min pts + * @return the i list + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "dbscan", can_be_const = false, @@ -1010,9 +1159,7 @@ public static IList DBscanApache(final IScope scope, final IList data, fi for (int i = 0; i < data.size(); i++) { final IList d = (IList) data.get(i); final double point[] = new double[d.size()]; - for (int j = 0; j < d.size(); j++) { - point[j] = Cast.asFloat(scope, d.get(j)); - } + for (int j = 0; j < d.size(); j++) { point[j] = Cast.asFloat(scope, d.get(j)); } remainingData.add(i); instances.add(new Instance(i, point)); } @@ -1037,6 +1184,16 @@ public static IList DBscanApache(final IScope scope, final IList data, fi } } + /** + * K means plusplus apache. + * + * @param scope the scope + * @param data the data + * @param k the k + * @param maxIt the max it + * @return the i list + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "kmeans", can_be_const = false, @@ -1062,9 +1219,7 @@ public static IList KMeansPlusplusApache(final IScope scope, final IList for (int i = 0; i < data.size(); i++) { final IList d = (IList) data.get(i); final double point[] = new double[d.size()]; - for (int j = 0; j < d.size(); j++) { - point[j] = Cast.asFloat(scope, d.get(j)); - } + for (int j = 0; j < d.size(); j++) { point[j] = Cast.asFloat(scope, d.get(j)); } instances.add(new Instance(i, point)); } final KMeansPlusPlusClusterer kmeans = @@ -1073,15 +1228,21 @@ public static IList KMeansPlusplusApache(final IScope scope, final IList try (final Collector.AsList results = Collector.getList()) { for (final Cluster cl : clusters) { final IList clG = GamaListFactory.create(); - for (final DoublePoint pt : cl.getPoints()) { - clG.addValue(scope, ((Instance) pt).getId()); - } + for (final DoublePoint pt : cl.getPoints()) { clG.addValue(scope, ((Instance) pt).getId()); } results.add(clG); } return results.items(); } } - + + /** + * T test P value. + * + * @param scope the scope + * @param seq1 the seq 1 + * @param seq2 the seq 2 + * @return the double + */ @operator ( value = "tTest", can_be_const = false, @@ -1096,16 +1257,26 @@ public static IList KMeansPlusplusApache(final IScope scope, final IList equals = "0.01") }) public static Double tTestPValue(final IScope scope, final IList seq1, final IList seq2) { TTest t = new TTest(); - + double[] s1 = new double[seq1.length(scope)]; for (int i = 0; i < seq1.length(scope); i++) { s1[i] = Cast.asFloat(scope, seq1.get(i)); } - + double[] s2 = new double[seq2.length(scope)]; for (int i = 0; i < seq2.length(scope); i++) { s2[i] = Cast.asFloat(scope, seq2.get(i)); } - - return t.tTest(s1,s2); + + return t.tTest(s1, s2); } + /** + * Op dynamic time warping. + * + * @param scope the scope + * @param vals1 the vals 1 + * @param vals2 the vals 2 + * @param radius the radius + * @return the double + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "dtw", can_be_const = false, @@ -1126,9 +1297,7 @@ public static Double OpDynamicTimeWarping(final IScope scope, final IList vals1, table[0][0] = 0; - for (int i = 1; i <= n2; i++) { - table[0][i] = Double.POSITIVE_INFINITY; - } + for (int i = 1; i <= n2; i++) { table[0][i] = Double.POSITIVE_INFINITY; } for (int i = 1; i <= n1; i++) { final int start = Math.max(1, i - radius); @@ -1159,6 +1328,15 @@ public static Double OpDynamicTimeWarping(final IScope scope, final IList vals1, } + /** + * Op dynamic time warping. + * + * @param scope the scope + * @param vals1 the vals 1 + * @param vals2 the vals 2 + * @return the double + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "dtw", can_be_const = false, @@ -1178,9 +1356,7 @@ public static Double OpDynamicTimeWarping(final IScope scope, final IList vals1, table[0][0] = 0; - for (int i = 1; i <= n2; i++) { - table[0][i] = Double.POSITIVE_INFINITY; - } + for (int i = 1; i <= n2; i++) { table[0][i] = Double.POSITIVE_INFINITY; } for (int i = 1; i <= n1; i++) { table[1][0] = Double.POSITIVE_INFINITY; @@ -1206,6 +1382,14 @@ public static Double OpDynamicTimeWarping(final IScope scope, final IList vals1, return table[0][n2]; } + /** + * Skewness. + * + * @param scope the scope + * @param data the data + * @return the double + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "skewness", can_be_const = false, @@ -1221,12 +1405,18 @@ public static Double OpDynamicTimeWarping(final IScope scope, final IList vals1, public static Double skewness(final IScope scope, final IList data) throws GamaRuntimeException { final Skewness sk = new Skewness(); final double[] values = new double[data.length(scope)]; - for (int i = 0; i < values.length; i++) { - values[i] = Cast.asFloat(scope, data.get(i)); - } + for (int i = 0; i < values.length; i++) { values[i] = Cast.asFloat(scope, data.get(i)); } return sk.evaluate(values); } + /** + * Kurtosis. + * + * @param scope the scope + * @param data the data + * @return the double + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "kurtosis", can_be_const = false, @@ -1243,13 +1433,20 @@ public static Double skewness(final IScope scope, final IList data) throws GamaR public static Double kurtosis(final IScope scope, final IList data) throws GamaRuntimeException { final Kurtosis k = new Kurtosis(); final double[] values = new double[data.length(scope)]; - for (int i = 0; i < values.length; i++) { - values[i] = Cast.asFloat(scope, data.get(i)); - } + for (int i = 0; i < values.length; i++) { values[i] = Cast.asFloat(scope, data.get(i)); } // java.lang.System.out.println("KURT: " + k.evaluate(values, 0, values.length)); return k.evaluate(values); } + /** + * K means plusplus apache. + * + * @param scope the scope + * @param data the data + * @param k the k + * @return the i list + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "kmeans", can_be_const = false, @@ -1271,6 +1468,14 @@ public static IList KMeansPlusplusApache(final IScope scope, final IList return KMeansPlusplusApache(scope, data, k, -1); } + /** + * Builds the regression. + * + * @param scope the scope + * @param data the data + * @return the gama regression + * @throws GamaRuntimeException the gama runtime exception + */ @operator ( value = "build", can_be_const = false, @@ -1294,6 +1499,14 @@ public static GamaRegression buildRegression(final IScope scope, final GamaMatri } } + /** + * Predict from regression. + * + * @param scope the scope + * @param regression the regression + * @param instance the instance + * @return the double + */ @operator ( value = "predict", can_be_const = false, @@ -1312,6 +1525,13 @@ public static Double predictFromRegression(final IScope scope, final GamaRegress return regression.predict(scope, instance); } + /** + * Gini index. + * + * @param scope the scope + * @param vals the vals + * @return the double + */ @operator ( value = "gini", category = { IOperatorCategory.SPATIAL, IOperatorCategory.STATISTICAL }, @@ -1328,7 +1548,7 @@ public static Double predictFromRegression(final IScope scope, final GamaRegress public static double giniIndex(final IScope scope, final IList vals) { final int N = vals.size(); - Double G = 0.0; + double G = 0.0; double sumXi = 0.0; for (int i = 0; i < N; i++) { final double xi = vals.get(i); @@ -1342,6 +1562,14 @@ public static double giniIndex(final IScope scope, final IList vals) { return G; } + /** + * Product of. + * + * @param scope the scope + * @param container the container + * @param filter the filter + * @return the object + */ @operator ( value = { "product_of" }, type = ITypeProvider.TYPE_AT_INDEX + 2, @@ -1365,6 +1593,14 @@ public static Object product_of(final IScope scope, final IContainer container, return product(scope, collect(scope, container, filter)); } + /** + * Variance of. + * + * @param scope the scope + * @param container the container + * @param filter the filter + * @return the object + */ @operator ( value = { "variance_of" }, type = ITypeProvider.TYPE_AT_INDEX + 2, diff --git a/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats2.java b/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats2.java index bed692c9bd..cdafb7a72f 100644 --- a/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats2.java +++ b/ummisco.gaml.extensions.stats/src/ummisco/gaml/extensions/stats/Stats2.java @@ -1,14 +1,13 @@ -/********************************************************************************************* +/******************************************************************************************************* * - * 'Stats2.java, in plugin ummisco.gaml.extensions.stats, is part of the source code of the GAMA modeling and simulation - * platform. (v. 1.8.1) + * Stats2.java, in ummisco.gaml.extensions.stats, is part of the source code of the + * GAMA modeling and simulation platform (v.1.8.2). * - * (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners + * (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU) * - * Visit https://github.com/gama-platform/gama for license information and developers contact. - * - * - **********************************************************************************************/ + * Visit https://github.com/gama-platform/gama for license information and contacts. + * + ********************************************************************************************************/ package ummisco.gaml.extensions.stats; import cern.colt.list.DoubleArrayList; @@ -27,9 +26,19 @@ import msi.gaml.operators.Containers; import msi.gaml.types.IType; +/** + * The Class Stats2. + */ @SuppressWarnings ({ "rawtypes" }) public class Stats2 { + /** + * From. + * + * @param scope the scope + * @param values the values + * @return the double array list + */ static DoubleArrayList from(final IScope scope, final IContainer values) { final DoubleArrayList d = new DoubleArrayList(values.length(scope)); for (final Object o : values.iterable(scope)) { @@ -39,12 +48,11 @@ static DoubleArrayList from(final IScope scope, final IContainer values) { return d; } + /** + * The Class DescriptiveStatistics. + */ public static abstract class DescriptiveStatistics { - // A list of agents among the left-operand list that are located at a - // distance <= the right operand from the - // caller agent (in its topology) - /** * Returns the auto-correlation of a data sequence. * @@ -441,6 +449,13 @@ public static Double opSkew(final IScope scope, final Double moment3, final Doub return Descriptive.skew(moment3, standardDeviation); } + /** + * Op variance. + * + * @param scope the scope + * @param standardDeviation the standard deviation + * @return the double + */ @operator ( value = "variance", can_be_const = true, @@ -537,9 +552,7 @@ public static Double opPvalueForFstat(final IScope scope, final Double fstat, fi final double x = dfd / (dfd + dfn * fstat); try { return Gamma.incompleteBeta(dfd / 2.0, dfn / 2.0, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .incompleteBeta reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .incompleteBeta reports: " + ex, scope); } } @@ -576,9 +589,7 @@ public static Double opPvalueForTstat(final IScope scope, final Double tstat, fi try { final double p = Probability.studentT(df, -x); return 2.0 * p; - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .studentT reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .studentT reports: " + ex, scope); } } @@ -610,9 +621,7 @@ public static Double opStudentArea(final IScope scope, final Double x, final Int // with the given degrees of freedom. try { return Probability.studentT(df, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .studentT reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .studentT reports: " + ex, scope); } } @@ -645,9 +654,7 @@ public static Double opNormalArea(final IScope scope, final Double x, final Doub // with the given mean and standard deviation. try { return Probability.normal(mean, sd, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .normal reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .normal reports: " + ex, scope); } } @@ -684,9 +691,7 @@ public static Double opStudentTInverse(final IScope scope, final Double x, final final double a = 2.0 * (1.0 - x); try { return Probability.studentTInverse(a, df); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .studentTInverse reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .studentTInverse reports: " + ex, scope); } } @@ -723,9 +728,7 @@ public static Double opNormalInverse(final IScope scope, final Double area, fina try { final double x = Probability.normalInverse(area); return (x + mean) * sd; - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .normalInverse reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .normalInverse reports: " + ex, scope); } } @@ -789,9 +792,7 @@ public static Double opBinomialCoeff(final IScope scope, final Integer n, final // the double return value. try { return Math.rint(Arithmetic.binomial(n, k)); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .Arithmetic.binomial reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .Arithmetic.binomial reports: " + ex, scope); } } @@ -865,9 +866,7 @@ public static Double opGamma(final IScope scope, final Double a, final Double b, // density function. try { return Probability.gamma(a, b, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .gamma reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .gamma reports: " + ex, scope); } } @@ -903,9 +902,7 @@ public static Double opGammaComplemented(final IScope scope, final Double a, fin // density function. try { return Probability.gammaComplemented(a, b, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .gamma reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .gamma reports: " + ex, scope); } } @@ -941,9 +938,7 @@ public static Double opBinomialComplemented(final IScope scope, final Integer n, // the probability of success in the range 0 to 1. try { return Probability.binomialComplemented(k, n, p); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .binomialComplement reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .binomialComplement reports: " + ex, scope); } } @@ -978,9 +973,7 @@ public static Double opChiSquare(final IScope scope, final Double x, final Doubl // freedom. try { return Probability.chiSquare(df, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .chiSquare reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .chiSquare reports: " + ex, scope); } } @@ -1017,14 +1010,15 @@ public static Double opChiSquareComplemented(final IScope scope, final Double x, // of freedom. try { return Probability.chiSquareComplemented(df, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .chiSquareComplemented reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .chiSquareComplemented reports: " + ex, scope); } } } + /** + * The Class GammaFunction. + */ public static abstract class GammaFunction { /** @@ -1053,9 +1047,7 @@ public static Double opGamma(final IScope scope, final Double x) { // Returns the value of the Gamma function at x. try { return Gamma.gamma(x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .gamma reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .gamma reports: " + ex, scope); } } @@ -1085,9 +1077,7 @@ public static Double opLogGamma(final IScope scope, final Double x) { // Returns the log of the value of the Gamma function at x. try { return Gamma.logGamma(x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .logGamma reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .logGamma reports: " + ex, scope); } } @@ -1122,9 +1112,7 @@ public static Double opIncompleteGamma(final IScope scope, final Double a, final // a to the integration end point x. try { return Gamma.incompleteGamma(a, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .incompleteGamma reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .incompleteGamma reports: " + ex, scope); } } @@ -1158,9 +1146,7 @@ public static Double opIncompleteGammaComplement(final IScope scope, final Doubl // argument a and integration start point x. try { return Gamma.incompleteGammaComplement(a, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .incompleteGammaComplement reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .incompleteGammaComplement reports: " + ex, scope); } } @@ -1191,9 +1177,7 @@ public static Double opBeta(final IScope scope, final Double a, final Double b) // Returns the beta function with arguments a, b. try { return Gamma.beta(a, b); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .beta reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .beta reports: " + ex, scope); } } @@ -1227,9 +1211,7 @@ public static Double opIncompleteBeta(final IScope scope, final Double a, final // a and b, from zero to x. try { return Gamma.incompleteBeta(a, b, x); - } catch (final IllegalArgumentException ex) { - throw GamaRuntimeException.error("colt .incompleteBeta reports: " + ex, scope); - } catch (final ArithmeticException ex) { + } catch (final IllegalArgumentException | ArithmeticException ex) { throw GamaRuntimeException.error("colt .incompleteBeta reports: " + ex, scope); } }