Procedure tha shows if a series of numbers are increasing or decreasing
Hi, :wave:
does anyone knows of a procidure that
can be use to tell if a siries of numbers
are increasing or decreasing.:confused:
Monti124
Re: Procedure tha shows if a series of numbers are increasing or decreasing
The problem is that all of the first N - 1 numbers could getting either larger or smaller in sequence and the Nth number could go the opposite. What then do you conclude? :ehh:
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Monti
Doc raises an interesting point. So, you may not always
have a definitive answer.
Here is some pseudo code that could at least tell you
"up, down, or dunno" ..
Code:
' assumes MyArray() has been pre-populated
' MyArray(0) = a
' MyArray(1) = b
' MyArray(2) = c
' ...
' MyArray(nn) = zz
'
direc = Empty
For ii = 1 to nn
' 1. set direction based on first pair
If ii = 1 then
If MyArray(1) > MyArray(0) Then
direc = "up"
ElseIf MyArray(1) < MyArray(0) Then
direc = "down"
Else
direc = "dunno"
End If
' 2. check that same direction is maintained
Else
If (direc = "up" And MyArray(ii) > MyArray(ii - 1) _
Or (direc = "down" And MyArray(ii) < MyArray(ii - 1) Then
direc = direc ' ie, no change in direction
Else
direc = "dunno"
End If
End If
Next ii
If you want to be more "forgiving", then you can easily
change the "check" algo.
Hope that gets you started.
Spoo
Re: Procedure tha shows if a series of numbers are increasing or decreasing
What exactly is the problem you're trying to solve ?
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Thank you all for the reply,
What Iam trying to acomplish is the following:
I have a number, for example 1.255
then I have about 30 mesurements made
against that number, so What Iam trying
to do use a code that can tell me if the
mesurements are increasing or decending from
base value of 1.255
I will try Spoo's code and see how thats comes out.
Thanks
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Quote:
Originally Posted by
Monti124
Thank you all for the reply,
What Iam trying to acomplish is the following:
I have a number, for example 1.255
then I have about 30 mesurements made
against that number, so What Iam trying
to do use a code that can tell me if the
mesurements are increasing or decending from
base value of 1.255
I will try Spoo's code and see how thats comes out.
Thanks
Go back to my post. You have to define the meaning of increasing and decreasing based on the measurements. Otherwise, you are grasping at straws. Consider a trend analysis of data, and that means least squares regression. A mathematician named Gauss first defined it. He was indeed a genius:
Y = aX + b
where a is the slope of the line (up or down) depending on the measurements and then estimated by least squares.
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Monti
Doc is onto something ... also known as linear regression.
See this Wiki page .. it has a nice chart example
http://en.wikipedia.org/wiki/Linear_regression
EDIT:
Keep in mind that the code snippet I provided is extremely
simple-minded .. it requires every subsequent value to be
larger than the previous to be deemed "up".
If your actual data is more like that shown in the Wiki chart,
then you'll need to do what Doc suggests, ie, linear regression.
I've done a fair amount of it in the past (and I'm sure Doc has
too). Holler if you need some guidance in that regard.
Spoo
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Thank you all once more, but
analyzing the situation I came to a simple solution.
I Subtracted the number A - B , A being the first Number
and B the secound number, if A - B = (-) my number is
acending since B will always be grater then A. Else if A - B = (+)
My number is decending since A will allways be grater then B.
A and B varialbles are use for all the Readings ub to 30.
Thank you all for the response
Monti:wave:
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Quote:
Originally Posted by
Monti124
Thank you all once more, but after
analyzing the situation, I came to a simple solution.
I subtracted the number A - B , A being the first number
and B the second number, if A - B = (-), my sequence is
ascending since B will always be greater then A. Else if A - B = (+),
my number is descending since A will allways be greater then B.
A and B variables are used for all the readings up to 30.
Thank you all for the response.
Monti:wave:
Statistics and thereby examining lots of numbers together is not your strongest suit, right? :ehh:
When trying to establish a trend, if you consider only the first two numbers in a sequence of several numbers, you have only scratched the surface. The rest of the numbers in the sequence are just as important. Understand?
Re: Procedure tha shows if a series of numbers are increasing or decreasing
As I had a bit of time and found my Probability and Statistics book, thought I'd show you an exmple of what Code Doc and Spoo were talking about:
Code:
Option Explicit
'
' Simple Regression using the Method of Least Squares
'
' Requires:
' PictureBox named picPlot
' Data File containing:
' Record 1 - Number of pairs of values
' Record 2 - X values (comma separated)
' Record 3 - Y values (comma separated)
' eg.
' 10
' 45,42,56,48,42,35,58,40,39,50
' 6.53,6.3,9.52,7.5,6.99,5.90,9.49,6.2,6.55,8.72
'
Private sngX() As Single
Private sngY() As Single
Private intN As Integer
Private sngXMax As Single
Private sngXMin As Single
Private sngYMax As Single
Private sngYMin As Single
Private sngSlope As Single
Private sngIntercept As Single
Private Sub Form_Load()
Dim intFile As Integer
Dim intI As Integer
Dim boSlope As Boolean
intFile = FreeFile
Open "C:\DataPoints.txt" For Input As intFile
Input #intFile, intN
ReDim sngX(intN - 1)
ReDim sngY(intN - 1)
For intI = 0 To intN - 1
Input #intFile, sngX(intI)
If intI = 0 Then
sngXMin = sngX(intI)
sngXMax = sngX(intI)
Else
If sngX(intI) < sngXMin Then sngXMin = sngX(intI)
If sngX(intI) > sngXMax Then sngXMax = sngX(intI)
End If
Next intI
For intI = 0 To intN - 1
Input #intFile, sngY(intI)
If intI = 0 Then
sngYMin = sngY(intI)
sngYMax = sngY(intI)
Else
If sngY(intI) < sngYMin Then sngYMin = sngY(intI)
If sngY(intI) > sngYMax Then sngYMax = sngY(intI)
End If
Next intI
Close intFile
boSlope = DoRegression
Call DoPlot
End Sub
Private Function DoRegression() As Boolean
Dim sngSumX As Single
Dim sngSumY As Single
Dim sngSumXY As Single
Dim sngSumXSquared As Single
Dim sngSumYSquared As Single
Dim sngXMean As Single
Dim sngYMean As Single
Dim sngSlopeDividend As Single
Dim sngSlopeDivisor As Single
Dim intI As Integer
For intI = 0 To UBound(sngX)
sngSumX = sngSumX + sngX(intI)
sngSumY = sngSumY + sngY(intI)
sngSumXY = sngSumXY + (sngX(intI) * sngY(intI))
sngSumXSquared = sngSumXSquared + (sngX(intI) ^ 2)
sngSumYSquared = sngSumYSquared + (sngY(intI) ^ 2)
Next intI
sngXMean = sngSumX / intN
sngYMean = sngSumY / intN
sngSlopeDividend = sngSumXY - (intN * sngXMean * sngYMean)
sngSlopeDivisor = sngSumXSquared - (intN * (sngXMean ^ 2))
sngSlope = sngSlopeDividend / sngSlopeDivisor
sngIntercept = sngYMean - (sngSlope * sngXMean)
DoRegression = sngSlope >= 0
End Function
Private Sub DoPlot()
Dim sngXRange As Single
Dim sngYRange As Single
Dim sngXScaleMax As Single
Dim sngYScaleMax As Single
Dim sngXScaleMin As Single
Dim sngYScaleMin As Single
Dim sngXPos As Single
Dim sngYPos As Single
Dim sngXPos1 As Single
Dim sngYPos1 As Single
Dim intI As Integer
picPlot.ScaleMode = 3
picPlot.FillStyle = 0
picPlot.FillColor = RGB(255, 0, 0)
sngYScaleMin = 10
sngXScaleMin = 10
sngXScaleMax = picPlot.ScaleWidth - (2 * sngXScaleMin)
sngYScaleMax = picPlot.ScaleHeight - (2 * sngYScaleMin)
sngXRange = sngXMax - sngXMin
sngYRange = sngYMax - sngYMin
For intI = 0 To intN - 1
sngXPos = ((sngX(intI) - sngXMin) * sngXScaleMax / sngXRange) + sngXScaleMin
sngYPos = (sngYScaleMax - ((sngY(intI) - sngYMin) * sngYScaleMax / sngYRange)) + sngYScaleMin
picPlot.Circle (sngXPos, sngYPos), 2, RGB(255, 0, 0)
Next intI
sngXPos = ((sngXMin - sngXMin) * sngXScaleMax / sngXRange) + sngXScaleMin
sngYPos = ((sngXMin) * sngSlope) + sngIntercept
sngYPos = (sngYScaleMax - ((sngYPos - sngYMin) * sngYScaleMax / sngYRange)) + sngYScaleMin
sngXPos1 = ((sngXMax - sngXMin) * sngXScaleMax / sngXRange) + sngXScaleMin
sngYPos1 = (sngXMax * sngSlope) + sngIntercept
sngYPos1 = (sngYScaleMax - ((sngYPos1 - sngYMin) * sngYScaleMax / sngYRange)) + sngYScaleMin
picPlot.Line (sngXPos, sngYPos)-(sngXPos1, sngYPos1), RGB(0, 0, 255)
picPlot.CurrentX = sngXScaleMin
picPlot.CurrentY = sngYScaleMin
picPlot.Print "Line of Regression is Y = " & Format(sngSlope, "0.00") & _
" * X" & IIf(sngIntercept >= 0, " + ", " - ") & _
Format(Abs(sngIntercept), "0.00")
End Sub
It will produce a Scatter Diagram and the Line of Regression.
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Doogle
Nice.
Extra points if you attach an image of your sample data .. :)
Spoo
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Sample data is in the 'eg' bit at the top of the code
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Doogle
Yes, I got that part.
My comment, however, was ambiguous.
Corrected:
Extra points if you attach an image of the Scatter Diagram
and Line of Regression resulting from your sample data.
Spoo
Re: Procedure tha shows if a series of numbers are increasing or decreasing
Oh, you mean a screen-shot of the results. No, I don't normally bother with things like that, if people are interested in seeing the results, it's a simple matter of creating a Project, copying and pasting the code, creating the data file from the example data, and running it. In fact if you look at most of my posts 'Debug.Print' is my normal user interface. :)