MODULE stat

CONTAINS

  REAL FUNCTION media(n,x)
    IMPLICIT NONE
    INTEGER :: n
    REAL :: x(n)
    INTEGER :: i
    REAL :: somma=0.0

    DO i=1,n
       somma=somma+x(i)
    ENDDO
    media=somma/n
  END FUNCTION media

  FUNCTION stdev(n,x,xm)
    IMPLICIT NONE
    INTEGER :: n, i
    REAL :: x(n), xm, stdev
    REAL :: somma=0.0

    DO i=1,n
       somma=somma+(x(i)-xm)*(x(i)-xm)
    ENDDO
    stdev=SQRT(somma/n)
  END FUNCTION stdev

  SUBROUTINE istogramma(n,x,nb,xb,yb)
    IMPLICIT NONE
    INTEGER :: n, nb
    REAL, DIMENSION(n) :: x
    REAL, DIMENSION(nb) :: xb, yb
    INTEGER :: i, bin, yc(nb)
    REAL :: xmin, xmax, delta

    xmin=x(1)
    DO i=2,n
       IF (x(i)<xmin) xmin=x(i)
    ENDDO

    xmax=x(1)
    DO i=2,n
       IF (x(i)>xmax) xmax=x(i)
    ENDDO

    delta=(xmax-xmin)/nb

    DO i=1,nb
       xb(i)=xmin+(i-0.5)*delta
    ENDDO

    yc=0
    DO i=1,n
       bin=int((x(i)-xmin)/delta)+1
       IF (bin==nb+1) bin=nb
       yc(bin)=yc(bin)+1
    ENDDO
    yb=float(yc)/(n*delta)

  END SUBROUTINE istogramma

END MODULE stat
