public static double variance(double[] data,int opt){
  double mean=0;
  double S=0;
  double numData=0;
  for (int i=0; i < data.length; i++) {
    if (!Double.isNaN(data[i])) {
      double delta=data[i] - mean;
      mean+=delta / (numData + 1);
      S+=delta * (data[i] - mean);
      numData++;
    }
  }
  if (opt == 0)   return (S / (numData - 1));
 else   return (S / numData);
}

public static double variance ( double [ ] data , int opt ) { double mean = 0 ; double S = 0 ; double numData = 0 ; for ( int i = 0 ; i < data . length ; i ++ ) { if ( ! Double . isNaN ( data [ i ] ) ) { double delta = data [ i ] - mean ; mean += delta / ( numData + 1 ) ; S += delta * ( data [ i ] - mean ) ; numData ++ ; } } if ( opt == 0 ) return ( S / ( numData - 1 ) ) ; else return ( S / numData ) ; }
compute the variance in the array . this function can deal with nans
