79138473

Date: 2024-10-29 18:17:28
Score: 2
Natty:
Report link

A possible solution to the problem could be the following:

We create the series:

var numNames = new SeriesBuilder<int, string>() {
  { 1, "one" }, { 2, "two" },{ 3, "three" }, { 4, "four" }, { 5, "five" }, { 6, "six" }, {7, seven,}, {8, "eight"}, {9, "nine"}, {10, "ten"} }.Series;

Calculate the averague using the SeriesModule, for a rolling of 5 elements and period of one element.

SeriesModule.WindowSize(5, Boundary.AtBeginning, numNames).Select(win=>win.Value.Mean());

The problem is that if we want to calculate the average with a window of 15 elements, it would give us an exception because there are not so many elements.

I have created at the end a class to calculate the average of a series of elements of n elements with a minimum period, simulating ptyhon.

    public class RollingAverage
    {
        private Series<DateTime, double> _serie;
        private SeriesBuilder<DateTime, double> _serie_builder = new SeriesBuilder<DateTime, double>();
        private SeriesBuilder<DateTime, double> _serie_builder_resultado = new SeriesBuilder<DateTime, double>();
        private readonly int _period;
        private int _min_period;
        private double _sum;

        /// <summary>
        /// Calculamos la media aritmética de una serie para una ventana de n periodos, con un mínimo periodo.
        /// </summary>
        /// <param name="serie"></param>
        /// <param name="window"></param>
        /// <param name="min_period"></param>
        /// <exception cref="ArgumentException"></exception>
        public RollingAverage(Series<DateTime, double> serie,int window, int min_period=1)
        {
            if (window <= 0)
            {
                throw new ArgumentException("Period must be greater than zero.", nameof(window));
            }
            _period = window;
            _min_period = min_period;
            _serie = serie;

        }

        private void Add(DateTime time,double value)
        {
            double elemento;

            _serie_builder.Add(time, value);

            if (_serie_builder.Count() > _min_period)
            {
                _sum = _serie_builder.Series.Sum();
            }

            if (_serie_builder.Count() > _period)
            {
                _serie_builder.Remove(_serie_builder.FirstOrDefault().Key,out elemento);
                _sum -= _serie_builder.FirstOrDefault().Value;
            }
        }

        public Series<DateTime, double> Mean()
        {
            if (_serie.ValuesAll.Count() == 0)
            {
                throw new InvalidOperationException("No values to average.");
            }

            foreach (var item in _serie.Observations)
            {
                Add(item.Key, item.Value);

                if (_serie_builder.Count() <= _min_period)

                    _serie_builder_resultado.Add(item.Key, 0.0);

                else
                
                    _serie_builder_resultado.Add(item.Key, (_sum / _serie_builder.Count()));
                
            }

            return _serie_builder_resultado.Series;

        
        }
    }
}

If anyone has any contribution, they would be grateful.

Reasons:
  • RegEx Blacklisted phrase (2): would be grateful
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: mhern0749