Calculates Variance for Xbar

@param flatDataCollection
@return
540229
public static double xbarVariance(FlatDataCollection flatDataCollection) {
        double n = flatDataCollection.size();
        if(n<=1) {
            throw new IllegalArgumentException("The provided collection must have more than 1 elements.");
        } 
        
        //As explained at http://www.fao.org/docrep/003/x6831e/x6831e12.htm
        //we take the first differences
        Double previousValue = null;
        double xbarVariance = 0.0;
        Iterator<Double> it = flatDataCollection.iteratorDouble();
        while(it.hasNext()) {
            Double yi = it.next();
            if(previousValue!=null) {
                double delta = yi - previousValue;
                xbarVariance+=delta*delta;
            }
            previousValue = yi;
        }
        xbarVariance/=(2*n*(n-1));

        return xbarVariance;
    }
966273
public double[] getStandardComponents(double[] x){
		if(x.length != originalN){
			throw new IllegalArgumentException("wrong array dimension: " + x.length);
		}
		double[] ret = new double[standardN];
		for(int i=0; i<x.length; i++){
			if (splittedVariablesList.contains(i)) {
				// this variable was splitted: x = xPlus-xMinus
				if(x[i] >= 0){
					//value for xPlus
					ret[standardS + i] = x[i];
				}else{
					int pos = -1;
					for(int k=0; k<splittedVariablesList.size(); k++){
						if(splittedVariablesList.get(k)==i){
							pos = k;
							break;
						}
					}
					//value for xMinus
					ret[standardS + x.length + pos] = -x[i];
				}
			}else{
				ret[standardS + i] = x[i];
			}
		}
		if(standardS>0){
			DoubleMatrix1D residuals = ColtUtils.zMult(standardA, F1.make(ret), standardB, -1);
			for(int i=0; i<standardS; i++){
				ret[i] = -residuals.get(i) + ret[i];
			}
		}
		return ret;
	}
170282
public static double xbarStd(TransposeDataCollection sampleDataCollection, AssociativeArray nh, AssociativeArray populationNh) {
        return Math.sqrt(xbarVariance(sampleDataCollection, nh, populationNh));
    }
249928
public static double sum(FlatDataCollection flatDataCollection) {
        double sum = 0.0;
        
        Iterator<Double> it = flatDataCollection.iteratorDouble();
        while(it.hasNext()) {
            Double value = it.next();
            if(value != null) {
                sum+= value;
            }
        }
        
        return sum;
    }
717184
public static double moment(FlatDataCollection flatDataCollection, int r, double mean) {
        int n = 0;
        double moment=0.0;
        
        Iterator<Double> it = flatDataCollection.iteratorDouble();
        while(it.hasNext()) {
            Double v = it.next();
            if(v != null) {
                ++n;
                moment+=Math.pow(v-mean, r);
            }
        }

        if(n<=1) {
            throw new IllegalArgumentException("The provided collection must have more than 1 elements.");
        }
        
        moment/=n;
        
        return moment;
    }
267966
function(datasetIndex) {
			var barIndex = 0;
			var meta, j;

			for (j = 0; j < datasetIndex; ++j) {
				meta = this.chart.getDatasetMeta(j);
				if (meta.bar && this.chart.isDatasetVisible(j)) {
					++barIndex;
				}
			}

			return barIndex;
		}
1185337
public double getXres() {
        Double xres = get(XRES);
        if (xres != null) {
            return xres;
        }
        return HMConstants.doubleNovalue;
    }
726878
public static double variance(TransposeDataCollection sampleDataCollection, AssociativeArray populationNh) {
        double variance = 0.0;
        
        int populationN = 0;
        
        double mean = mean(sampleDataCollection, populationNh);
        
        for(Map.Entry<Object, FlatDataCollection> entry : sampleDataCollection.entrySet()) {
            Object strata = entry.getKey();
            Integer strataPopulation = ((Number)populationNh.get(strata)).intValue();
        
            if(strataPopulation==null) {
                throw new IllegalArgumentException("Invalid strata population size.");
            }
            
            populationN+=strataPopulation;
            
            //Analysis of Variance
            
            //Within Strata
            variance+=(strataPopulation-1)*SimpleRandomSampling.variance(entry.getValue());
            //Between Strata
            variance+=strataPopulation*Math.pow(SimpleRandomSampling.mean(entry.getValue())-mean,2);
        }
        
        variance/=(populationN-1);

        return variance;
    }
1213610
public static double mean(TransposeDataCollection sampleDataCollection) {
        double mean = 0.0;
        
        int totalSampleN = 0;
        for(Map.Entry<Object, FlatDataCollection> entry : sampleDataCollection.entrySet()) {
            mean += Descriptives.sum(entry.getValue());
            
            totalSampleN += entry.getValue().size();
        }
        
        mean/=totalSampleN;
        
        return mean;
    }
805773
public static double getPvalue(FlatDataCollection flatDataCollection, double median) {
        int n=0;
        AssociativeArray Di = new AssociativeArray();
        Iterator<Double> it = flatDataCollection.iteratorDouble();
        while(it.hasNext()) {
            double delta=it.next()-median;

            if(delta==0.0) {
                continue; //don't count it at all
            }

            String key="+";
            if(delta<0) {
                key="-";
            }
            Di.put(key+String.valueOf(n), Math.abs(delta));
            ++n;
        }
        if(n<=0) {
            throw new IllegalArgumentException("The provided collection can't be empty.");
        }

        //converts the values of the table with its Ranks
        Ranks.getRanksFromValues(Di);
        double W=0.0;
        for(Map.Entry<Object, Object> entry : Di.entrySet()) {
            String key = entry.getKey().toString();
            Double rank = TypeInference.toDouble(entry.getValue());
        
            if(key.charAt(0)=='+') {
                W+=rank;
            }
        }

        double pvalue= scoreToPvalue(W, n);

        return pvalue;
    }
1282239
static double usingXArrAndYStride(
      final double[] xArr, final double yStride, final double x) {
    final int xArrLen = xArr.length;
    final int xArrLenM1 = xArrLen - 1;

    final int offset;
    assert ((xArrLen >= 4) && (x >= xArr[0]) && (x <= xArr[xArrLenM1]));

    if (x ==  xArr[xArrLenM1]) { /* corner case */
      return (yStride * (xArrLenM1));
    }

    offset = findStraddle(xArr, x); //uses recursion
    final int xArrLenM2 = xArrLen - 2;
    assert ((offset >= 0) && (offset <= (xArrLenM2)));

    if (offset == 0) { /* corner case */
      return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 0), x));
    }
    else if (offset == xArrLenM2) { /* corner case */
      return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 2), x));
    }
    /* main case */
    return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 1), x));
  }
887109
public static double moment(FlatDataCollection flatDataCollection, int r) {
        double mean = mean(flatDataCollection);
        return moment(flatDataCollection, r, mean);
    }
306931
public static double getPvalue(TransposeDataList transposeDataList) {
        Object[] keys = transposeDataList.keySet().toArray();
        if(keys.length!=2) {
            throw new IllegalArgumentException("The collection must contain observations from 2 groups.");
        }
        
        Object keyX = keys[0];
        Object keyY = keys[1];
        
        FlatDataList flatDataListX = transposeDataList.get(keyX);
        FlatDataList flatDataListY = transposeDataList.get(keyY);

        int n = flatDataListX.size();
        if(n<=0 || n!=flatDataListY.size()) {
            throw new IllegalArgumentException("The number of observations in each group must be equal and larger than 0.");
        }

        int Tplus=0;
        
        for(int j=0;j<n;++j) {
            double delta= flatDataListX.getDouble(j) - flatDataListY.getDouble(j);
            if(delta==0) {
                continue; //don't count it at all
            }
            if(delta>0) {
                ++Tplus;
            }
        }

        double pvalue = scoreToPvalue(Tplus, n);

        return pvalue;
    }
545786
public static TransposeDataCollection randomSampling(TransposeDataList strataIdList, AssociativeArray nh, boolean withReplacement) {
        TransposeDataCollection sampledIds = new TransposeDataCollection();
    
        for(Map.Entry<Object, FlatDataList> entry : strataIdList.entrySet()) {
            Object strata = entry.getKey();
            
            Number sampleN =  ((Number)nh.get(strata));
            if(sampleN==null) {
                continue;
            }
            
            sampledIds.put(strata, SimpleRandomSampling.randomSampling(entry.getValue(), sampleN.intValue(), withReplacement));
        }
        
        return sampledIds;
    }
84482
@Override
	public final void flatMap(IN value, Collector<OUT> out) throws Exception {
		for (Iterator<OUT> iter = flatMap(value); iter.hasNext(); ) {
			out.collect(iter.next());
		}
	}
112945
public static double mean(FlatDataCollection flatDataCollection) {
        int n = 0;
        double mean = 0.0;
        Iterator<Double> it = flatDataCollection.iteratorDouble();
        while(it.hasNext()) {
            Double value = it.next();
            if(value != null) {
                ++n;
                mean += value;
            }
        }

        if(n==0) {
            throw new IllegalArgumentException("No not null values where found in the collection.");
        }
        mean /= n;
        
        return mean;
    }
959563
public static double getPvalue(TransposeDataCollection transposeDataCollection) {
        //flatten the original internalData table
        AssociativeArray associativeArray = new AssociativeArray();
        
        for(Map.Entry<Object, FlatDataCollection> entry : transposeDataCollection.entrySet()) {
            Object i = entry.getKey();
            FlatDataCollection row = entry.getValue();
            
            Integer j=0;
            for (Object value : row) {
                Map.Entry<Object, Object> i_j = new AbstractMap.SimpleEntry<>(i, (Object)j);
                associativeArray.put(i_j, value);
                ++j;
            }
        }
        
        //converts the values of the flatDataCollection with their Ranks
        AssociativeArray tiesCounter = Ranks.getRanksFromValues(associativeArray);
        int n = associativeArray.size();
        
        double C=0;
        //Correct for ties
        if(!tiesCounter.isEmpty()) {
            for(Object value : tiesCounter.values()) {
                double Ti = TypeInference.toDouble(value);
                C+=((Ti*Ti-1)*Ti); //faster than using pow()
            }
            C/=((n*n-1.0)*n); //faster than using pow()
        }
        //tiesCounter = null;

        //Important note! Remember that the "i" value is the number of the group while the j is the number of observation within the group and NOT the other way around.
        Map<Object, Integer> ni = new HashMap<>(); //stores the total number of observations in each group
        Map<Object, Double> Ridot = new HashMap<>(); //stores the sum of Ranks for each group
        
        for(Map.Entry<Object, Object> entry : associativeArray.entrySet()) {
            @SuppressWarnings("unchecked")
            Map.Entry<Object, Object> i_j = (Map.Entry<Object, Object>)entry.getKey();
            
            Object i = i_j.getKey(); //get i and j values
            Double rank = TypeInference.toDouble(entry.getValue());
            
            if(Ridot.containsKey(i)==false) { //if this "i" value is found for first time then define 
                Ridot.put(i, rank);
                ni.put(i, 1);
            }
            else {
                Ridot.put(i, Ridot.get(i)+rank);
                ni.put(i, ni.get(i)+1);
            }
        }
        //associativeArray=null;

        int k=ni.size();

        //Calculate Kruskal Wallis scrore based on the above
        double KWscore=0.0;
        
        for(Map.Entry<Object, Double> entry : Ridot.entrySet()) {
            Object i = entry.getKey();
            Double value = entry.getValue();
            
            KWscore+=value*value/ni.get(i);
        }
        //Ridot=null;
        //ni=null;


        KWscore=(12.0/(n*(n+1.0)))*KWscore - 3.0*(n+1.0);

        KWscore/=(1.0-C); //correction for ties

        double pvalue= scoreToPvalue(KWscore, k);

        return pvalue;
    }