Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
 /// <summary>
 /// Do reporting of individuals
 /// </summary>
 /// <returns></returns>
 private void ReportHerd()
 {
     // warning if the same individual is in multiple filter groups it will be considered more than once
     foreach (var fgroup in this.FindAllChildren <RuminantGroup>())
     {
         ListStatistics listStatistics = SummariseAttribute(AttributeTag, true, fgroup);
         if (listStatistics != null)
         {
             LastStatistics = new RuminantAttributeStatisticsEventArgs()
             {
                 GroupName  = fgroup.Name,
                 Statistics = listStatistics
             };
             ReportItemGenerated(LastStatistics);
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// Return the mean and standard deviation of an attribute value
        /// </summary>
        public int SummariseAttribute(string tag, bool ignoreNotFound)
        {
            LastListStatistics = new ListStatistics();
            var values = Herd.Where(a => (ignoreNotFound & a.GetAttributeValue(tag) == null) ? false : true).Select(a => Convert.ToDouble(a.GetAttributeValue(tag)?.storedValue));

            if (values.Count() == 0)
            {
                return(0);
            }
            double sd   = 0;
            double mean = values.Average();
            double sum  = values.Sum(d => Math.Pow(d - mean, 2));

            sd = Math.Sqrt((sum) / values.Count() - 1);
            LastListStatistics.Average           = mean;
            LastListStatistics.StandardDeviation = sd;
            LastListStatistics.Count             = values.Count();
            LastListStatistics.Total             = Herd.Count();
            return(Herd.Count());
        }
Beispiel #4
0
        /// <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);
        }