Results 1 to 14 of 14

Thread: Procedure tha shows if a series of numbers are increasing or decreasing

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    177

    Procedure tha shows if a series of numbers are increasing or decreasing

    Hi,
    does anyone knows of a procidure that
    can be use to tell if a siries of numbers
    are increasing or decreasing.

    Monti124

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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?
    Doctor Ed

  3. #3
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Procedure tha shows if a series of numbers are increasing or decreasing

    What exactly is the problem you're trying to solve ?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    177

    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

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Procedure tha shows if a series of numbers are increasing or decreasing

    Quote Originally Posted by Monti124 View Post
    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.
    Doctor Ed

  7. #7
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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
    Last edited by Spoo; Jul 24th, 2011 at 08:14 PM.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2009
    Posts
    177

    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

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Procedure tha shows if a series of numbers are increasing or decreasing

    Quote Originally Posted by Monti124 View Post
    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
    Statistics and thereby examining lots of numbers together is not your strongest suit, right?

    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?
    Doctor Ed

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.
    Last edited by Doogle; Jul 30th, 2011 at 06:58 AM.

  11. #11
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  12. #12
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

  13. #13
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  14. #14
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width