/// <summary> /// Calculates Statistics /// </summary> /// <param name="USGSID">USGSID parameter</param> /// <returns>Returns conductivity statastics</returns> public ConductivityStatistics CalculateStatistics(int sourceId) { try { ConductivityStatistics CondStats = new ConductivityStatistics(); long USGSID = this.modifiedConductivityRepository.GetAgentId(sourceId); var AllData = this.modifiedwaterdataRepository.GetAll(); var data = from r in AllData where r.sourceID == USGSID select r; int NumberOfDataPoints = data.Count(); double SumOfSourceOneData = 0, Mean, sum_X_minus_μ_squared = 0, StdDev; double sourceMax, sourceMin, sourceMedian, sourceMode, currentCond, percentAboveMean; double[] condDataToDouble; double[] X_minus_μ_squared; condDataToDouble = new double[NumberOfDataPoints]; X_minus_μ_squared = new double[NumberOfDataPoints]; foreach (var element in data) { SumOfSourceOneData = SumOfSourceOneData + Convert.ToDouble(element.cond); } Mean = SumOfSourceOneData / NumberOfDataPoints; int i = 0; foreach (var element in data) { X_minus_μ_squared[i] = (Convert.ToInt32(element.cond) - Mean) * (Convert.ToInt32(element.cond) - Mean); condDataToDouble[i] = Convert.ToDouble(element.cond); i++; } for (int j = 0; j < X_minus_μ_squared.Count(); j++) { sum_X_minus_μ_squared = sum_X_minus_μ_squared + X_minus_μ_squared[j]; } sum_X_minus_μ_squared = sum_X_minus_μ_squared / NumberOfDataPoints; StdDev = Math.Sqrt(sum_X_minus_μ_squared); sourceMax = condDataToDouble.Length > 0 ? Math.Round(condDataToDouble.Max(), 2) : 0; sourceMin = condDataToDouble.Length > 0 ? Math.Round(condDataToDouble.Min(), 2) : 0; sourceMedian = Math.Round(((sourceMax + sourceMin) / 2), 2); Dictionary<double, double> counts = new Dictionary<double, double>(); foreach (double a in condDataToDouble) { if (counts.ContainsKey(a)) { counts[a] = counts[a] + 1; } else { counts[a] = 1; } } sourceMode = double.MinValue; double maxVal = double.MinValue; foreach (double key in counts.Keys) { if (counts[key] > maxVal) { maxVal = counts[key]; sourceMode = key; } } currentCond = condDataToDouble.Length > 0 ? condDataToDouble.Last() : 0; percentAboveMean = Mean != double.NaN ? ((currentCond - Mean) / Mean) * 100 : 0; CondStats.ArithmeticMean = Mean != double.NaN ? Math.Round(Mean, 2).ToString() : "0"; CondStats.CurrentConductivity = currentCond.ToString(); CondStats.MaxConductivity = sourceMax.ToString(); CondStats.MinConductivity = sourceMin.ToString(); CondStats.MedianValue = sourceMedian.ToString(); CondStats.ModalValue = sourceMode.ToString(); CondStats.PercentAboveMean = Math.Round(percentAboveMean).ToString(); CondStats.StandardDeviation = Math.Round(StdDev, 2).ToString(); CondStats.SourceId = USGSID.ToString(); return CondStats; } catch { throw; } }
/// <summary> /// Calculates Statistics /// </summary> /// <param name="USGSID">USGSID parameter</param> /// <returns>Returns conductivity statastics</returns> public ConductivityStatistics CalculateStatistics(int sourceId) { try { ConductivityStatistics CondStats = new ConductivityStatistics(); long USGSID = this.modifiedConductivityRepository.GetAgentId(sourceId); var AllData = this.modifiedwaterdataRepository.GetAll(); var data = from r in AllData where r.sourceID == USGSID select r; int NumberOfDataPoints = data.Count(); double SumOfSourceOneData = 0, Mean, sum_X_minus_μ_squared = 0, StdDev; double sourceMax, sourceMin, sourceMedian, sourceMode, currentCond, percentAboveMean; double[] condDataToDouble = new double[NumberOfDataPoints]; double[] X_minus_μ_squared = new double[NumberOfDataPoints]; foreach (var element in data) { SumOfSourceOneData = SumOfSourceOneData + Convert.ToDouble(element.cond); } Mean = SumOfSourceOneData / NumberOfDataPoints; int i = 0; foreach (var element in data) { X_minus_μ_squared[i] = (Convert.ToInt32(element.cond) - Mean) * (Convert.ToInt32(element.cond) - Mean); condDataToDouble[i] = Convert.ToDouble(element.cond); i++; } for (int j = 0; j < X_minus_μ_squared.Count(); j++) { sum_X_minus_μ_squared = sum_X_minus_μ_squared + X_minus_μ_squared[j]; } sum_X_minus_μ_squared = sum_X_minus_μ_squared / NumberOfDataPoints; StdDev = Math.Sqrt(sum_X_minus_μ_squared); sourceMax = condDataToDouble.Length > 0 ? Math.Round(condDataToDouble.Max(), 2) : 0; sourceMin = condDataToDouble.Length > 0 ? Math.Round(condDataToDouble.Min(), 2) : 0; // Sort Conductivity Data in Asscending Order: var condSorted = condDataToDouble.OrderBy(n => n); int halfIndex = NumberOfDataPoints/2; // Calculate the Median Properly: if((NumberOfDataPoints % 2) == 0){ // Even Data Set Length sourceMedian = (condSorted.ElementAt(halfIndex) + condSorted.ElementAt(halfIndex + 1)) / 2; // Average the Two Middle Points } else{ // Odd Data Set Length sourceMedian = condSorted.ElementAt(halfIndex); // Just get the middle point of the data set } //sourceMedian = Math.Round(((sourceMax + sourceMin) / 2), 2); // This is not the median value! // Calculate the Modal Value Dictionary<double, double> counts = new Dictionary<double, double>(); foreach (double cond_value in condDataToDouble) { if (counts.ContainsKey(cond_value)) { counts[cond_value] = counts[cond_value] + 1; } else { counts[cond_value] = 1; } } sourceMode = double.MinValue; // Get the lowest value a double can represent as not to interfere with finding the max value. double maxVal = double.MinValue; foreach (double key in counts.Keys) { if (counts[key] > maxVal) { maxVal = counts[key]; sourceMode = key; } } currentCond = condDataToDouble.Length > 0 ? condDataToDouble.Last() : 0; percentAboveMean = Mean != double.NaN ? ((currentCond - Mean) / Mean) * 100 : 0; CondStats.ArithmeticMean = Mean != double.NaN ? Math.Round(Mean, 2).ToString() : "0"; CondStats.CurrentConductivity = currentCond.ToString(); CondStats.MaxConductivity = sourceMax.ToString(); CondStats.MinConductivity = sourceMin.ToString(); CondStats.MedianValue = sourceMedian.ToString(); CondStats.ModalValue = sourceMode.ToString(); CondStats.PercentAboveMean = Math.Round(percentAboveMean).ToString(); CondStats.StandardDeviation = Math.Round(StdDev, 2).ToString(); CondStats.SourceId = USGSID.ToString(); return CondStats; } catch { throw; } }