歪度 Skewness 計算 java

googleコードで調べたら二つあってapacheのほうがEXCELと値が同じだったので、 apacheのソースコードをシステムに合わせて改変した。

	   /**
	     * Returns the Skewness of the entries in the specifed portion of the
	     * input array.
	     * <p>
	     * See {@link Skewness} for the definition used in the computation.
	     * <p>
	     * Throws <code>IllegalArgumentException</code> if the array is null.
	     * @param values 配列
		 * @param stdDev 標準偏差
		 * @param mean 平均値
		 * @return the skewness of the values or Double.NaN if length is less than
	     * 3
	     * @throws IllegalArgumentException if the array is null or the array index
	     *  parameters are not valid
		 */
	    //public double evaluate(final double[] values,final int begin, final int length) {
		public double evaluateSkew(final Double[] values,double stdDev,double mean) {
		
	        // Initialize the skewness
	        double skew = Double.NaN;
	        int length = values.length;
	
			// first make sure that array has enough elements
			if (length < 3) {
				String s = "Array with less than three elements passed to DescriptiveStatistics.skewness";
				System.out.println(s);
				return Double.NaN;
			} // end if
			
	        double m = mean;
	
	        // Calc the std, this is implemented here instead
	        // of using the standardDeviation method eliminate
	        // a duplicate pass to get the mean
	        double accum = 0.0;
	        double accum2 = 0.0;
	        for (int i = 0; i <  length; i++) {
	            accum += Math.pow((values[i] - m), 2.0);
	            accum2 += (values[i] - m);
	        }
	
	        double accum3 = 0.0;
	        for (int i = 0; i <  length; i++) {
	            accum3 += Math.pow(values[i] - m, 3.0d);
	        }
	        accum3 /= Math.pow(stdDev, 3.0d);
	
	        // Get N
	        double n0 = length;
	
	        // Calculate skewness
	        skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
	
	        return skew;
	    }

参考リンク

計算:http://cse.naro.affrc.go.jp/takezawa/r-tips/r/59.html

コード:http://www.google.co.jp/codesearch?hl=ja&q=skewness+lang:java+show:yaIDEIeT03M:pfhWDDj4swc:yaIDEIeT03M&sa=N&cd=1&ct=rc&cs_p=http://svn.apache.org/repos/asf/commons/proper/math/&cs_f=tags/MATH_1_0/src/java/org/apache/commons/math/stat/descriptive/moment/Skewness.java

	    /**
	     * Returns the Skewness of the entries in the specifed portion of the
	     * input array.
	     * <p>
	     * See {@link Skewness} for the definition used in the computation.
	     * <p>
	     * Throws <code>IllegalArgumentException</code> if the array is null.
	     *
	     * @param values the input array
	     * @param begin the index of the first array element to include
	     * @param length the number of elements to include
	     * @return the skewness of the values or Double.NaN if length is less than
	     * 3
	     * @throws IllegalArgumentException if the array is null or the array index
	     *  parameters are not valid
	     */
	    public double evaluate(final double[] values,final int begin,
	            final int length) {
	
	        // Initialize the skewness
	        double skew = Double.NaN;
	
	        if (test(values, begin, length) && length > 2 ){
	            Mean mean = new Mean();
	            // Get the mean and the standard deviation
	            double m = mean.evaluate(values, begin, length);
	
	            // Calc the std, this is implemented here instead
	            // of using the standardDeviation method eliminate
	            // a duplicate pass to get the mean
	            double accum = 0.0;
	            double accum2 = 0.0;
	            for (int i = begin; i < begin + length; i++) {
	                accum += Math.pow((values[i] - m), 2.0);
	                accum2 += (values[i] - m);
	            }
	            double stdDev = Math.sqrt((accum - (Math.pow(accum2, 2) / ((double) length))) /
	                    (double) (length - 1));
	
	            double accum3 = 0.0;
	            for (int i = begin; i < begin + length; i++) {
	                accum3 += Math.pow(values[i] - m, 3.0d);
	            }
	            accum3 /= Math.pow(stdDev, 3.0d);
	
	            // Get N
	            double n0 = length;
	
	            // Calculate skewness
	            skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
	        }
	        return skew;
	    }

コード:http://www.google.co.jp/codesearch?hl=ja&q=skewness+lang:java+show:CC8nQS3oiXM:gTavB_bEI4I:CC8nQS3oiXM&sa=N&cd=10&ct=rc&cs_p=http://geoviz.googlecode.com/svn&cs_f=trunk/common/src/main/java/geovista/common/data/DescriptiveStatistics.java

/**
	 * Returns the skewness for a given array. Skewness indicates if data are
	 * slanted one way or the other around the mean. A negative skewness
	 * indicates that there are more values greater than the mean, and the
	 * distribution is skewed to the right. A positive skewness indicates that
	 * there are more values less than the mean and the distribution is skewed
	 * to the left.
	 */
	public static double skewness(double[] doubleArray, boolean sample) {
		// first make sure that array has enough elements
		if (doubleArray.length < 3) {
			String s = "Array with less than three elements passed to DescriptiveStatistics.skewness";
			logger.fine(s);
			return Double.NaN;
		} // end if
	
		double temp = 0;
		double sum = 0;
		double n = doubleArray.length;
		double stdDev = DescriptiveStatistics.stdDev(doubleArray, sample);
		double mean = DescriptiveStatistics.mean(doubleArray);
	
		// now make sure that standard deviation is not zero
		if (stdDev == 0) {
			String s = "Array with standard devation of zero passed to DescriptiveStatistics.skewness";
			logger.fine(s);
			return Double.NaN;
		} // end if
	
		for (int i = 0; i < n; i++) {
			temp = doubleArray[i];
			temp -= mean;
			sum += (temp * temp * temp);
		} // end for
	
		double skewness = sum / (n * stdDev * stdDev * stdDev);
	
		return skewness;
	} // end skewness
  • これを改変したやつ
    	/**
    	 * Returns the skewness for a given array. Skewness indicates if data are
    	 * slanted one way or the other around the mean. A negative skewness
    	 * indicates that there are more values greater than the mean, and the
    	 * distribution is skewed to the right. A positive skewness indicates that
    	 * there are more values less than the mean and the distribution is skewed
    	 * to the left.
    	 * @param doubleArray 数列
    	 * @param stdDev 標準偏差
    	 * @param mean 平均値
    	 * @return 歪度
    	 */
    	public static double skewness(ArrayList<Double> doubleArray,double stdDev,double mean) {
    		// first make sure that array has enough elements
    		if (doubleArray.size() < 3) {
    			String s = "Array with less than three elements passed to DescriptiveStatistics.skewness";
    			System.out.println(s);
    			return Double.NaN;
    		} // end if
    	
    		double temp = 0;
    		double sum = 0;
    		double n = doubleArray.size();
    		//double stdDev = DescriptiveStatistics.stdDev(doubleArray, sample);
    		//double mean = DescriptiveStatistics.mean(doubleArray);
    	
    		// now make sure that standard deviation is not zero
    		if (stdDev == 0) {
    			String s = "Array with standard devation of zero passed to DescriptiveStatistics.skewness";
    			System.out.println(s);
    			return Double.NaN;
    		} // end if
    	
    		for (int i = 0; i < n; i++) {
    			temp = doubleArray.get(i);
    			temp -= mean;
    			sum += (temp * temp * temp);
    		} // end for
    	
    		double skewness = sum / (n * stdDev * stdDev * stdDev);
    	
    		return skewness;
    	} // end skewness

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2008-11-21 (金) 18:46:47 (3861d)