finding the nth value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?YjF1Y2VyZWU=?=

    #1

    finding the nth value

    Hi, still very new to programming but am a little stumped by how to do this
    idea i have.

    I need to make a moving average which takes every nth value in a data series
    to build a running total which can then be divided by the length of the
    moving average. The data series will be gaining a new value every x amount of
    time t so the average has to be made from the last added value. And i also
    need a way to use this same moving average but from different start point ie
    t-1, t-2 etc What is the best way to do this?

    Any help would be greatly appreciated.
  • Martin CLAVREUIL

    #2
    Re: finding the nth value

    Hi,

    Try this:

    ------- CODE TO PASTE IN VS ------
    public class AvgSerie
    {
    List<int_InnerD ata = new List<int>();

    /// <summary>
    /// Appends a new value to the serie
    /// </summary>
    /// <param name="value">ne w value</param>
    public void AddValue(int value)
    {
    _InnerData.Add( value);
    }

    /// <summary>
    /// gets the last n-sized average
    /// </summary>
    /// <param name="length">n-size value</param>
    /// <returns></returns>
    public int GetLastMovingAv erage(int length)
    {
    return GetMovingAVG(_I nnerData.Count - length, length);
    }

    /// <summary>
    /// Retrieve a moving average
    /// </summary>
    /// <param name="startinde x">0 based index of the first measure
    to use</param>
    /// <param name="length">N umber of measures to use</param>
    /// <returns></returns>
    public int GetMovingAVG(in t startindex, int length)
    {
    int response = 0;
    int stopindex = startindex + length-1;
    if (stopindex <= _InnerData.Coun t)
    {
    for (int i = startindex; i <= stopindex; i++)
    {
    response += _InnerData[i];
    }
    response /= length;
    }
    else
    {
    throw new Exception("The index provided exceeds the
    size of the serie");
    }
    return response;
    }
    }

    ------- END OF CODE TO PASTE IN VS ------

    b1uceree wrote:
    Hi, still very new to programming but am a little stumped by how to do this
    idea i have.
    >
    I need to make a moving average which takes every nth value in a data series
    to build a running total which can then be divided by the length of the
    moving average. The data series will be gaining a new value every x amount of
    time t so the average has to be made from the last added value. And i also
    need a way to use this same moving average but from different start point ie
    t-1, t-2 etc What is the best way to do this?
    >
    Any help would be greatly appreciated.

    Comment

    • Peter Duniho

      #3
      Re: finding the nth value

      b1uceree wrote:
      Hi, still very new to programming but am a little stumped by how to do this
      idea i have.
      >
      I need to make a moving average which takes every nth value in a data series
      to build a running total which can then be divided by the length of the
      moving average. The data series will be gaining a new value every x amount of
      time t so the average has to be made from the last added value.
      If you want the moving average to be exactly the average of the last N
      samples, you need to save N samples, dropping one off the end when you
      add a new one to the beginning.

      For example:

      const int kcvalueMoving = 20;
      double valueTotal = 0;
      Queue<doubleval uesPrevious = new Queue<double>(k cvalueMoving);

      double MovingAverage(d ouble valueNew)
      {
      if (valuesPrevious .Count == kcvalueMoving)
      {
      valueTotal -= valuesPrevious. Dequeue();
      }

      valueTotal += valueNew;
      valuesPrevious. Enqueue(valueNe w);

      return valuesTotal / valuesPrevious. Count;
      }

      If you don't want to keep track of all the previous values, you can just
      keep the sum, adding a weighted value to it with each iteration. As
      each iteration weights the previous sum at less than 100%, trailing
      values will become less and less significant. However, it's not exactly
      a moving average and you'll have to watch out for the divisor (count of
      samples) eventually reaching the maximum the variable can contain.

      IMHO, if you want a true moving average, the code above is probably want
      you want.
      And i also
      need a way to use this same moving average but from different start point ie
      t-1, t-2 etc What is the best way to do this?
      I'm not exactly clear on this part of your requirement. If you have to
      be able to arbitrarily pick any point in the sequence of data and return
      a moving average based on that sequence, you will have to save _all_ of
      the samples in the sequence of data, and then simply perform a regular
      average of the previous N elements for a given position within the sequence.

      If you have this requirement and the number of samples for a given
      moving average is relatively small (less than a hundred or so, for
      example), then you may not want to bother with the implementation I
      included above. It's slower to recalculate the average from scratch
      each time, but if you have to do that anyway in some cases, you might as
      well keep the code simple and just have a single implementation that
      works for both scenarios.

      If you have a known subset of "different start points", then you could
      just cache the result of the primary moving average calculation. For
      example, if you always only want to allow access to the last M averages,
      you could keep a list of length M. There are a number of ways you could
      implement this while allowing both easy adding of new averages and
      removal of old ones, while still having access to all of the averages:
      * You could use an actual LinkedList and enumerate when you need a
      specific entry,
      * You could use the Queue class and use the ToArray() to get at a
      specific entry within the Queue, or
      * You could use a different queue implementation (Jon Skeet has a
      RandomAccessQue ue that would serve this purpose nicely, in his MiscUtil
      library: http://www.yoda.arachsys.com/csharp/miscutil/)

      If you mean something different, then perhaps you could clarify.

      Pete

      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: finding the nth value

        Both Martin and Peter have provide answers so that you get the average of the
        last N entries in your list, which can be added to as data comes into the
        list. It appeared to me that you were asking a slightly different question,
        which was that the list grows, but to step through the list as the index
        increases by N. For example, if the list is size 10, and N = 2, you want the
        average of list [0, 2, 4, 6, 8]. If this is what you want, then your average
        routine would be something like this:

        public double RunningAverage( List<doubleAllD ata, int StartIndex, int
        StepSize)
        {
        int DataPoints = 0;
        double Sum = 0.0;

        for(int Index = StartIndex; Index < AllData.Count; Index += StepSize)
        {
        Sum += AllData[Index];
        ++DataPoints;
        }

        if (DataPoints == 0) return 0;
        return (Sum / (double) DataPoints);
        }

        "b1uceree" wrote:
        Hi, still very new to programming but am a little stumped by how to do this
        idea i have.
        >
        I need to make a moving average which takes every nth value in a data series
        to build a running total which can then be divided by the length of the
        moving average. The data series will be gaining a new value every x amount of
        time t so the average has to be made from the last added value. And i also
        need a way to use this same moving average but from different start point ie
        t-1, t-2 etc What is the best way to do this?
        >
        Any help would be greatly appreciated.

        Comment

        • =?Utf-8?B?YjF1Y2VyZWU=?=

          #5
          RE: finding the nth value

          Thanks Mike, Martin and Peter,

          You have all given me good ideas to structure the code and you are correct
          Peter in pointing out that it is in effect a picking 0,2,4,6,8 at time t and
          1, 3, 5, 7, 9 at t+1 but what i didnt explain correctly is that there will be
          first a simple moving average then i will be taking a moving averages using
          the step through approach. These basically the first two steps that you need
          to take in creating a time adjusted or seasonal moving average.

          "Family Tree Mike" wrote:
          Both Martin and Peter have provide answers so that you get the average of the
          last N entries in your list, which can be added to as data comes into the
          list. It appeared to me that you were asking a slightly different question,
          which was that the list grows, but to step through the list as the index
          increases by N. For example, if the list is size 10, and N = 2, you want the
          average of list [0, 2, 4, 6, 8]. If this is what you want, then your average
          routine would be something like this:
          >
          public double RunningAverage( List<doubleAllD ata, int StartIndex, int
          StepSize)
          {
          int DataPoints = 0;
          double Sum = 0.0;
          >
          for(int Index = StartIndex; Index < AllData.Count; Index += StepSize)
          {
          Sum += AllData[Index];
          ++DataPoints;
          }
          >
          if (DataPoints == 0) return 0;
          return (Sum / (double) DataPoints);
          }
          >
          "b1uceree" wrote:
          >
          Hi, still very new to programming but am a little stumped by how to do this
          idea i have.

          I need to make a moving average which takes every nth value in a data series
          to build a running total which can then be divided by the length of the
          moving average. The data series will be gaining a new value every x amount of
          time t so the average has to be made from the last added value. And i also
          need a way to use this same moving average but from different start point ie
          t-1, t-2 etc What is the best way to do this?

          Any help would be greatly appreciated.

          Comment

          Working...