001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math3.stat.descriptive; 018 019import java.io.Serializable; 020 021import org.apache.commons.math3.util.FastMath; 022import org.apache.commons.math3.util.MathUtils; 023import org.apache.commons.math3.util.Precision; 024 025/** 026 * Value object representing the results of a univariate statistical summary. 027 * 028 */ 029public class StatisticalSummaryValues implements Serializable, 030 StatisticalSummary { 031 032 /** Serialization id */ 033 private static final long serialVersionUID = -5108854841843722536L; 034 035 /** The sample mean */ 036 private final double mean; 037 038 /** The sample variance */ 039 private final double variance; 040 041 /** The number of observations in the sample */ 042 private final long n; 043 044 /** The maximum value */ 045 private final double max; 046 047 /** The minimum value */ 048 private final double min; 049 050 /** The sum of the sample values */ 051 private final double sum; 052 053 /** 054 * Constructor 055 * 056 * @param mean the sample mean 057 * @param variance the sample variance 058 * @param n the number of observations in the sample 059 * @param max the maximum value 060 * @param min the minimum value 061 * @param sum the sum of the values 062 */ 063 public StatisticalSummaryValues(double mean, double variance, long n, 064 double max, double min, double sum) { 065 super(); 066 this.mean = mean; 067 this.variance = variance; 068 this.n = n; 069 this.max = max; 070 this.min = min; 071 this.sum = sum; 072 } 073 074 /** 075 * @return Returns the max. 076 */ 077 public double getMax() { 078 return max; 079 } 080 081 /** 082 * @return Returns the mean. 083 */ 084 public double getMean() { 085 return mean; 086 } 087 088 /** 089 * @return Returns the min. 090 */ 091 public double getMin() { 092 return min; 093 } 094 095 /** 096 * @return Returns the number of values. 097 */ 098 public long getN() { 099 return n; 100 } 101 102 /** 103 * @return Returns the sum. 104 */ 105 public double getSum() { 106 return sum; 107 } 108 109 /** 110 * @return Returns the standard deviation 111 */ 112 public double getStandardDeviation() { 113 return FastMath.sqrt(variance); 114 } 115 116 /** 117 * @return Returns the variance. 118 */ 119 public double getVariance() { 120 return variance; 121 } 122 123 /** 124 * Returns true iff <code>object</code> is a 125 * <code>StatisticalSummaryValues</code> instance and all statistics have 126 * the same values as this. 127 * 128 * @param object the object to test equality against. 129 * @return true if object equals this 130 */ 131 @Override 132 public boolean equals(Object object) { 133 if (object == this ) { 134 return true; 135 } 136 if (object instanceof StatisticalSummaryValues == false) { 137 return false; 138 } 139 StatisticalSummaryValues stat = (StatisticalSummaryValues) object; 140 return Precision.equalsIncludingNaN(stat.getMax(), getMax()) && 141 Precision.equalsIncludingNaN(stat.getMean(), getMean()) && 142 Precision.equalsIncludingNaN(stat.getMin(), getMin()) && 143 Precision.equalsIncludingNaN(stat.getN(), getN()) && 144 Precision.equalsIncludingNaN(stat.getSum(), getSum()) && 145 Precision.equalsIncludingNaN(stat.getVariance(), getVariance()); 146 } 147 148 /** 149 * Returns hash code based on values of statistics 150 * 151 * @return hash code 152 */ 153 @Override 154 public int hashCode() { 155 int result = 31 + MathUtils.hash(getMax()); 156 result = result * 31 + MathUtils.hash(getMean()); 157 result = result * 31 + MathUtils.hash(getMin()); 158 result = result * 31 + MathUtils.hash(getN()); 159 result = result * 31 + MathUtils.hash(getSum()); 160 result = result * 31 + MathUtils.hash(getVariance()); 161 return result; 162 } 163 164 /** 165 * Generates a text report displaying values of statistics. 166 * Each statistic is displayed on a separate line. 167 * 168 * @return String with line feeds displaying statistics 169 */ 170 @Override 171 public String toString() { 172 StringBuffer outBuffer = new StringBuffer(); 173 String endl = "\n"; 174 outBuffer.append("StatisticalSummaryValues:").append(endl); 175 outBuffer.append("n: ").append(getN()).append(endl); 176 outBuffer.append("min: ").append(getMin()).append(endl); 177 outBuffer.append("max: ").append(getMax()).append(endl); 178 outBuffer.append("mean: ").append(getMean()).append(endl); 179 outBuffer.append("std dev: ").append(getStandardDeviation()) 180 .append(endl); 181 outBuffer.append("variance: ").append(getVariance()).append(endl); 182 outBuffer.append("sum: ").append(getSum()).append(endl); 183 return outBuffer.toString(); 184 } 185 186}