How to resolve the algorithm Cumulative standard deviation step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Cumulative standard deviation step by step in the Action! programming language

Table of Contents

Problem Statement

Write a stateful function, class, generator or co-routine that takes a series of floating point numbers, one at a time, and returns the running standard deviation of the series. The task implementation should use the most natural programming style of those listed for the function in the implementation language; the task must state which is being used. Do not apply Bessel's correction; the returned standard deviation should always be computed as if the sample seen so far is the entire population.

Use this to compute the standard deviation of this demonstration set,

{ 2 , 4 , 4 , 4 , 5 , 5 , 7 , 9 }

{\displaystyle {2,4,4,4,5,5,7,9}}

, which is

2

{\displaystyle 2}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Cumulative standard deviation step by step in the Action! programming language

Source code in the action! programming language

INCLUDE "H6:REALMATH.ACT"

REAL sum,sum2
INT count

PROC Calc(REAL POINTER x,sd)
  REAL tmp1,tmp2,tmp3

  RealAdd(sum,x,tmp1)       ;tmp1=sum+x
  RealAssign(tmp1,sum)      ;sum=sum+x
  RealMult(x,x,tmp1)        ;tmp1=x*x
  RealAdd(sum2,tmp1,tmp2)   ;tmp2=sum2+x*x
  RealAssign(tmp2,sum2)     ;sum2=sum2+x*x
  count==+1
  IF count=0 THEN
    IntToReal(0,sd)         ;sd=0
  ELSE
    IntToReal(count,tmp1)
    RealMult(sum,sum,tmp2)  ;tmp2=sum*sum
    RealDiv(tmp2,tmp1,tmp3) ;tmp3=sum*sum/count
    RealDiv(tmp3,tmp1,tmp2) ;tmp2=sum*sum/count/count
    RealDiv(sum2,tmp1,tmp3) ;tmp3=sum2/count
    RealSub(tmp3,tmp2,tmp1) ;tmp1=sum2/count-sum*sum/count/count
    Sqrt(tmp1,sd)           ;sd=sqrt(sum2/count-sum*sum/count/count)
  FI
RETURN

PROC Main()
  INT ARRAY values=[2 4 4 4 5 5 7 9]
  INT i
  REAL x,sd

  Put(125) PutE() ;clear screen
  MathInit()
  IntToReal(0,sum)
  IntToReal(0,sum2)
  count=0
  FOR i=0 TO 7
  DO
    IntToReal(values(i),x)
    Calc(x,sd)
    Print("x=") PrintR(x)
    Print(" sum=") PrintR(sum)
    Print(" sd=") PrintRE(sd)
  OD
RETURN

  

You may also check:How to resolve the algorithm Soundex step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Empty directory step by step in the Raku programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the BASIC programming language
You may also check:How to resolve the algorithm Burrows–Wheeler transform step by step in the Haskell programming language
You may also check:How to resolve the algorithm Magic squares of singly even order step by step in the 11l programming language