/// <summary> /// Return the mean and standard deviation of an attribute value /// </summary> public ListStatistics SummariseAttribute(string tag, bool ignoreNotFound, RuminantGroup herdGroup = null) { ListStatistics listStatistics = new ListStatistics(); if (ruminantHerd is null) { return(listStatistics); } IEnumerable <Ruminant> herd = null; if (herdGroup != null) { herd = herdGroup.Filter(ruminantHerd.Herd); } else { herd = ruminantHerd.Herd; } var values = herd.Where(a => (ignoreNotFound & a.Attributes.GetValue(tag) == null) ? false : true).Select(a => new Tuple <float, float>(Convert.ToSingle(a.Attributes.GetValue(tag)?.StoredValue), Convert.ToSingle(a.Attributes.GetValue(tag)?.StoredMateValue))).ToList(); if (values.Count() == 0) { return(listStatistics); } double sd = 0; Single mean = values.Average(a => a.Item1); Single sum = values.Sum(d => Convert.ToSingle(Math.Pow(d.Item1 - mean, 2))); sd = Math.Sqrt((sum) / values.Count() - 1); listStatistics.Average = mean; listStatistics.StandardDeviation = sd; mean = values.Average(a => a.Item2); sum = values.Sum(d => Convert.ToSingle(Math.Pow(d.Item2 - mean, 2))); sd = (sum == 0)?0:Math.Sqrt((sum) / values.Count() - 1); listStatistics.AverageMate = mean; listStatistics.StandardDeviationMate = sd; listStatistics.Count = values.Count(); listStatistics.Total = herd.Count(); return(listStatistics); }
/// <summary> /// Return the mean and standard deviation of an attribute value /// </summary> public ListStatistics SummariseAttribute(string tag, bool ignoreNotFound, RuminantGroup herdGroup = null) { ListStatistics listStatistics = new ListStatistics(); if (ruminantHerd is null) { return(listStatistics); } IEnumerable <Ruminant> herd = null; if (herdGroup != null) { herd = herdGroup.Filter(ruminantHerd.Herd); } else { herd = ruminantHerd.Herd; } // do not report mate if greater than max months since conception // if not valid report NAN that is filtered out in calculations below var values = herd.Where(a => (ignoreNotFound & a.Attributes.GetValue(tag) == null) ? false : true).Select(a => new Tuple <float, float>( (a.Attributes.GetValue(tag)?.StoredValue is null) ? Single.NaN: Convert.ToSingle(a.Attributes.GetValue(tag)?.StoredValue), (a.Sex == Sex.Female && a.Age - (a as RuminantFemale).AgeAtLastConception <= MaxMonthsToReportMate) ? Single.NaN : (a.Attributes.GetValue(tag)?.StoredMateValue is null) ? Single.NaN : Convert.ToSingle(a.Attributes.GetValue(tag)?.StoredMateValue)) ).ToList(); if (values.Count() == 0) { return(listStatistics); } double sd = 0; Single mean = 0; Single sum = 0; var valuesPresent = values.Where(a => !float.IsNaN(a.Item1)); if (valuesPresent.Count() > 0) { mean = valuesPresent.Average(a => a.Item1); sum = valuesPresent.Sum(d => Convert.ToSingle(Math.Pow(d.Item1 - mean, 2))); sd = Math.Sqrt((sum) / valuesPresent.Count() - 1); listStatistics.Average = mean; listStatistics.StandardDeviation = sd; listStatistics.Total = valuesPresent.Count(); } valuesPresent = values.Where(a => !float.IsNaN(a.Item2)); if (valuesPresent.Count() > 0) { mean = valuesPresent.Average(a => a.Item2); sum = valuesPresent.Sum(d => Convert.ToSingle(Math.Pow(d.Item2 - mean, 2))); sd = (sum == 0) ? 0 : Math.Sqrt((sum) / valuesPresent.Count() - 1); listStatistics.AverageMate = mean; listStatistics.StandardDeviationMate = sd; listStatistics.TotalMate = valuesPresent.Count(); } listStatistics.Count = values.Count(); return(listStatistics); }