A Simple Moving Average Algorithm
I am looking for a way to find the moving average for customers over a 30 day period. I am new to the concept of moving averages so I started with a Google search and found a lot of good information. However I was not able to find any sample VB code to help get me started. I did find this C# sample on Code Project but my attempts at conversion have not been successfull.
Does anybody have an existing VB class they would like to share or do you know of a sample that I could use to build my own?
Re: A Simple Moving Average Algorithm
You mean this?
Code:
Imports System
Imports System.Collections.Generic
Imports System.Text
Namespace AveragerTest
Public Class BruteForce
Private samples() As Single
Private idx As Integer
Public ReadOnly Property Average() As Single
Get
Dim total As Single = 0.0!
For i As Integer = 0I To samples.Length - 1I
total += samples(i)
Next i
Return total / samples.Length
End Get
End Property
Public Sub New(ByVal numSamples As Integer)
If numSamples <= 0I Then
Throw New ArgumentOutOfRangeException("numSamples can't be negative or 0.")
Else
ReDim samples(numSamples - 1I)
idx = 0I
End If
End Sub
Public Sub AddSample(ByVal val As Single)
samples(idx) = val
If System.Threading.Interlocked.Increment(idx) = samples.Length Then
idx = 0I
End If
End Sub
End Class
End Namespace
Re: A Simple Moving Average Algorithm
I tried converting the whole thing (with the bells and whistles) that is farther down the page with a "web based" converter and it didn't convert correctly for me. I did get the same section that you were able to convert but I don't think that is everything I need to get the correct moving averages.
I am sure that it is something I don't understand about the original code but I am just not sure what it is. Any ideas?
Re: A Simple Moving Average Algorithm
1. Try Instant VB for converting C# code to VB.
2. Do you understand how to calculate a moving average on paper? If so then you should be able to implement it in VB without any examples, or at least make an attempt.
3. VB code is very similar to C# code so you should be able to convert the vast majority of what's in that Code Project article. If you were to tell us what specific parts you're having issues with then we could help you with those and you're done.
Re: A Simple Moving Average Algorithm
this should give you an idea
Code:
Private Sub MovingAVG()
Dim rndm As New Random 'used to generate random data
Dim someData(999) As Byte 'place to store random data
rndm.NextBytes(someData) 'generate 1000 values randomly
Const maDuration As Integer = 30 'length of moving average
Dim ma As New List(Of Integer), theAvg As Integer 'storage for values and average
For x As Integer = 0 To someData.Length - 1 'iterate through random data
ma.Insert(0, someData(x)) 'insert into list newest will be at index 0
theAvg = 0 'calculate the average
For y = 0 To ma.Count - 1
theAvg += ma(y) 'add up the values
Next
theAvg = theAvg \ ma.Count 'divide by the count
'Debug.WriteLine(theAvg.ToString)
If ma.Count = maDuration Then 'do we have a durations worth of data?
ma.RemoveAt(ma.Count - 1) 'remove the oldest
End If
Next
End Sub