standard deviation

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

standard deviation

Post by marcoagpinto »

Hello!

Is there a command to calculate the "standard deviation"?

If not, how do I do it?

Thanks!
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: standard deviation

Post by Keya »

something like this? (comparing file1 with file2)

Code: Select all

Procedure.d StdDeviation(*bufA.Ascii, *bufB.Ascii, len)
  If *bufA And *bufB
    For i = 0 To len-1
      sum + Pow(*bufA\a - *bufB\a,2)
      *bufA + 1: *bufB + 1
    Next i
    ProcedureReturn Sqr( ((sum/len)*100) + (((Mod(sum,len))*100) + len/2)/len )
  EndIf
EndProcedure

Procedure GetFileBuf(sFile.s)
  hFile = ReadFile(#PB_Any,sFile, #PB_File_SharedRead | #PB_File_SharedWrite)
  If hFile
    flen = Lof(hFile)
    *buf = AllocateMemory(flen)
    If *buf: ReadData(hFile, *buf, flen): EndIf
    CloseFile(hFile)
  EndIf
  ProcedureReturn *buf
EndProcedure


Define sum.d, dev.d
*bufA.Ascii = GetFileBuf("file1.dat")
*bufB.Ascii = GetFileBuf("file2.dat")
lenA = MemorySize(*bufA)
lenB = MemorySize(*bufA)
If lenA <> lenB
  Debug "Error, must be same length":  End
EndIf

Debug "StdDev = " + StrD(StdDeviation(*bufA, *bufB, lenA)/100) ;might not want /100
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: standard deviation

Post by Keya »

ive made some more metrics you might like - Correlation, StdDev, MSE, RMSE, NRMSE, PSNR:

Code: Select all

Procedure Metrics(*bufA.Ascii, *bufB.Ascii, flen.i)
  Define sum.d, mse.d, PSNR.d, ft.d, fCor.d
  
  For i = 0 To flen-1
    sum + Pow(*bufA\a - *bufB\a,2)
    ft = Sqr(Abs(Pow(*bufA\a,2) - Pow(*bufB\a,2)))
    ft / 255.0
    ft = 1.0 - ft
    fCor + ft         
    *bufA + 1: *bufB + 1
  Next i
  
  Debug "Correlation = " + StrD(fCor)  
  fCor = fCor / flen
  
  dev.d = Sqr( ((sum/flen)*100) + (((Mod(sum,flen))*100) + flen/2)/flen )
  Debug "StdDev=" + StrD(dev)
  
  ;Mean Squared Error
  mse = sum / flen
  Debug "MSE=" + StrD(mse)
  
  ;Root MSE
  RMSE.d = Sqr(MSE)
  Debug "RMSE=" + StrD(mse)   
    
  ;Normalised RMSE (often expressed as a percent)
  NRMSE.d = (RMSE/255)*100
  NRMSE.d = (Sqr(MSE)/255)*100  
  Debug "NRMSE=" + StrD(NRMSE) + "%"
  
  ;Peak Signal-to-Noise Ratio (PSNR)
  If mse
    PSNR=10*Log10((Pow(255,2)/mse))
  Else
    PSNR = (100*100-1)/100
  EndIf
  Debug "PSNR=" + StrD(PSNR) + " dB"
  
EndProcedure
Anyone have any other groovy metrics?
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: standard deviation

Post by blueb »

from RosettaCode.zip at: http://www.rsbasic.de/backups/

Code: Select all

;==================================================================
; Rosetta Task: Standard deviation
;
; Write a statefull function, class, generator or coroutine 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.
;==================================================================

;Define our Standard deviation function
Declare.d Standard_deviation(x)
 
; Main program
If OpenConsole()
  Define i, x
  Restore MyList
  For i=1 To 8
    Read.i x
    PrintN(StrD(Standard_deviation(x)))
  Next i
  Print(#CRLF$+"Press ENTER to exit"): Input()
EndIf
 
;Calculation procedure, with memory
Procedure.d Standard_deviation(In)
  Static in_summa, antal
  Static in_kvadrater.q
  in_summa+in
  in_kvadrater+in*in
  antal+1
  ProcedureReturn Pow((in_kvadrater/antal)-Pow(in_summa/antal,2),0.50)
EndProcedure
 
;data section
DataSection
MyList:
  Data.i  2,4,4,4,5,5,7,9
EndDataSection

; Output:
;  0.0000000000
;  1.0000000000
;  0.9428090416
;  0.8660254038
;  0.9797958971
;  1.0000000000
;  1.3997084244
;  2.0000000000
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: standard deviation

Post by marcoagpinto »

Thanks, my friends!

I will try to read some theory behind the calculations before I understand it fully.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: standard deviation

Post by Keya »

Post Reply