Announcement

Collapse
No announcement yet.

Standard Deviation code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Standard Deviation code

    Hello,

    Can anyone help me with the code for calculating a standard deviation for an array of type double? I'm no good with maths at all!

    inputarray(0-20) = Double type values to calculate S.D on (eg: 2.0120)
    outputarray(0-20) = Would be the values for 1 S.D of inputarray

    Many thanks for any help you can provide,
    Alex

  • #2
    Alex,
    look here:


    It gives very simple steps for finding standard deviation of a set of numbers.

    Paul.

    Comment


    • #3
      Thanks Paul - I think I've got it now.

      Alex

      Comment


      • #4
        This sub was posted to the forums previously. Apologies but I was unable to find the original author. It calcs stats of data stored in double array a.

        Code:
        sub pResults(a() as double)
         local i,j,n as long
         local s,ave,adev,sdev,var,skew,curt,p as double
         
         n=ubound(a())+1
         s = 0#
         FOR j = 0 TO n-1
          s = s + a(j)
         NEXT j
         ' calculate mean
         ave = s / n
         adev = 0#
         var = 0#
         skew = 0#
         curt = 0#
         FOR j = 0 TO n-1
          s = a(j) - ave
          adev = adev + ABS(s)
          p = s * s
          var = var + p
          p = p * s
          skew = skew + p
          p = p * s
          skew = skew + p
          curt = curt + p
         NEXT j
         adev = adev / n
         var = var / (n - 1)
         sdev = SQR(var)
         IF var <> 0# THEN
          skew = skew / (n * sdev ^ 3)
          curt = curt / (n * var ^ 2) - 3#
         ELSE
          stdout " No skew or kurtosis when zero variance."
         END IF
         ' print results
         stdout
         stdout " Samples n ........: " + str$(n)
         stdout " Average ..........: " + str$(ave)
         stdout " Average  Deviation: " + str$(adev)
         stdout " Standard Deviation: " + str$(sdev)
         stdout " Variance .........: " + str$(var)
         stdout " Skewness .........: " + str$(skew)
         stdout " Kurtosis .........: " + str$(curt)
         stdout
        END sub

        Comment


        • #5
          Thanks also Andre.

          I had already figured the solution out from the Wikipedia link. Here's my code for 1 S.D - I've tested it thoroughly and it works a charm

          ====================================================

          REGISTER sm AS EXT
          REGISTER aa AS EXT
          REGISTER mn AS EXT
          REGISTER stdv AS EXT

          REGISTER idx AS LONG
          REGISTER i1 AS LONG

          FOR idx = datastart TO dataend

          ' First calculate moving average up to this point (20 periods)

          sm = 0
          FOR i1 = idx - 19 TO idx
          sm = sm + inputarray(i1)
          NEXT i1
          mn = sm / 20

          ' Now calculate 1 S.D for last 20 periods

          sm = 0
          FOR i1 = idx - 19 TO idx
          aa = mn - inputarray(i1)
          sm = sm + (aa * aa)
          NEXT i1
          sm = sm / 20

          outputarray(idx) = SQR(sm) ' This is 1 S.D


          NEXT idx

          Comment


          • #6
            I had purchased a book a while back(many years ago) in statistics that was really a nice find.
            I believe it was called either "Statistics for the Beginner" or "Simple Statistics".
            I will try to find it in the next couple of days, but it was the best and easiest book on the subject of statistics i had ever read.
            If i remember, the book was paperback, with large pages, black background on the cover with some bright colorful characters used to spell out the book's title.
            it was cheap too.

            paul
            p purvis

            Comment


            • #7
              Probably the most useful and intelligible single book I have encountered for computing or writing programs in statistics is:

              James L. Bruning & B.L. Kintz, Computational handbook of statistics, 3rd ed., 1987. Scott, Foresman Company

              An extended appendix lists the BASIC source code for every statistic that is discussed, and a companion diskette can (or could, in days of yore) be ordered from the second author. The programs are written in GWBasic and in my experience they are 100% compatible with PBDOS & easily translatable into other varieties of PB.

              Comment


              • #8
                Here is what I use here:
                Code:
                '  ----------------------------------------------
                '  "STDDEV"
                '  Function returns the standard deviation calculation for a
                '  single dimension data series.  The calculation used is
                '  determined as follows:
                '
                '  Population Method:
                '     Assign Zero To the Sample variable Or ignore it
                '        eg.  Sample = 0
                '  Sample Method:
                '     Any value other than Zero will invoke this method
                '        eg.  Sample = 1
                '
                FUNCTION StdDev( x() AS DOUBLE, OPTIONAL BYVAL Sample AS LONG) AS DOUBLE
                   LOCAL Itm AS LONG, Count_Start AS LONG, Count_End AS LONG, Item_Count AS LONG
                   Local Sum, SumSqr, MeanVal As Double
                
                   LOCAL x_Ptr AS DOUBLE PTR
                   LOCAL Sum_Ptr AS DOUBLE PTR
                   LOCAL MeanVal_Ptr AS DOUBLE PTR
                   LOCAL SumSqr_Ptr AS DOUBLE PTR
                
                   '  Array Boundary Values
                   Count_Start = LBOUND(x)
                   Count_End = UBOUND(x)
                
                   '  Be sure there is something to Count
                   IF Count_Start < Count_End THEN
                
                     '  Calcuate the number of array elements
                      Item_Count = Count_End - Count_Start
                
                      '  Obtain address locations
                      x_Ptr = VarPtr(x(Count_Start))
                      Sum_Ptr = VarPtr(Sum)
                      SumSqr_Ptr = VarPtr(SumSqr)
                      MeanVal_Ptr = VarPtr(MeanVal)
                
                      For Itm = 0 To Item_Count
                         @Sum_Ptr = @Sum_Ptr + @x_Ptr[Itm]
                      Next Itm
                
                      '  Determine Mean of the data series
                      @MeanVal_Ptr = @Sum_Ptr / (Item_Count + 1)
                
                      '  Sum the Squared values of the data - Mean
                      For Itm = 0 To Item_Count
                         @SumSqr_Ptr = @SumSqr_Ptr + (@x_Ptr[Itm] - @MeanVal_Ptr)^2
                      Next Itm
                
                      '  Select the Calculation method
                      If Sample = 0 Then
                         '  Population Calculation
                         Function = Sqr( @SumSqr_Ptr / (Item_Count + 1))
                      Else
                         '  Sample Calculation
                         Function = Sqr( @SumSqr_Ptr / (Item_Count ))
                      End If
                   ELSE
                      '  When No values or just 1 element is
                      '  in the array, the variance must be Zero
                      FUNCTION = 0
                   END IF
                END FUNCTION   '  StdDev
                
                '  ----------------------------------------------
                Roger...

                Comment

                Working...
                X