Groovy web console

subscribe to the feed Subscribe
to this
site
VarianciaDesvioadraoFrequenciaDiscreta (via #groovywebconsole)
tweet this snippet Tweet
this
script

VarianciaDesvioadraoFrequenciaDiscreta

Published 6 months ago by Anderson Lira with tags matemática estatística
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
/** 
 *  
 *  2        _ 2 
 * s = (x  - x) . f 
 *    ∑( i    )    i 
 *   ---------------- 
 *       f - 1 
 *    ∑   i 
 */ 
 
 class VarianciaDesvioadraoFrequenciaDiscreta { 
     def rol = [:] 
       
     def calcular(){ 
        def total = 0 
        def frequencia = 0 
        rol.each { key,value -> 
            total+=key * value 
            frequencia+=value 
        } 
        def media = total/frequencia 
        def sum = 0 
        rol.each{ key,value -> 
            sum+= ((key - media).pow(2) * value) 
        } 
        
        def variancia = sum / (frequencia -1) 
        
        Math.sqrt(variancia) 
     } 
 } 
   
 VarianciaDesvioadraoFrequenciaDiscreta variancia = new VarianciaDesvioadraoFrequenciaDiscreta() 
 variancia.rol = [2:3,3:5,4:8,5:4] 
println variancia.calcular()