Free Web Site - Free Web Space and Site Hosting - Web Hosting - Internet Store and Ecommerce Solution Provider - High Speed Internet
Search the Web

Implementación de la Distribución Normal Acumulada



Explicación de los métodos utilizados (C)



Código fuente

interface Evaluar
{
    double f(double x);    
}

class FuncZ implements Evaluar
{
    public double f (double x)
    {
        return Math.exp(-Math.pow(x,2.0) / 2.0) / Math.sqrt(2.0 * Math.PI);     
    }
}


class Integral
{
    public double sumaParcial(Double x, Double y, double h, Evaluar e)
    {
        double s = y.doubleValue();
        s += 4 * e.f(x.doubleValue() + h);
        x = new Double(x.doubleValue() + 2.0 * h);
        y = new Double(e.f(x.doubleValue()));
        s += y.doubleValue();
        return s;    
    }    
    
    public double fintegral(Double a, double b, int n, Evaluar e)
    {
        double s = 0.0;
        Double y = new Double(e.f(a.doubleValue()));
        if (n % 2 != 0) n++;
        double h = (b - a.doubleValue()) / n;
        int cont = 0;
        do
        {
            s += sumaParcial(a,y,h,e);
            cont += 2;
        } while(cont < n);
        
        s *= h / 3.0;
        return s;    
    }
}

class DistNormal
{
    FuncZ fz;
    Integral integral;
    
    public DistNormal()
    {
        fz = new FuncZ();
        integral = new Integral();    
    }    
        
    private double redondear4d(double num)
    {
        double aux = num * 10000;
        int tmp = (int) aux;
    
        return (double) tmp / 10000;     
    }   
    
    public double normal_acum(double z)
    {
        Double lim_inf = new Double(-3.60);
        double acum = 0.000159;
        double inc = 0.01;    
        do
        {
            acum += integral.fintegral(lim_inf,lim_inf.doubleValue() + inc , 4 , fz);
            lim_inf = new Double(lim_inf.doubleValue() + inc);    
        } while (lim_inf.doubleValue() < z - inc);
        return redondear4d(acum);
    }
    
    double dist_normal(double z)
    {
        return (z <= 0.0)? normal_acum(z) : 1.0 - normal_acum(-z);    
    }
    
    double func_z(double prob)
    {
        Double lim_inf = new Double(-3.60);
        double acum = 0.000159;
        double inc = 0.01;    
        do
        {
            acum += integral.fintegral(lim_inf,lim_inf.doubleValue() + inc , 4 , fz);
            lim_inf = new Double(lim_inf.doubleValue() + inc);    
        } while (acum < prob + 0.000159);
        return redondear4d(lim_inf.doubleValue() - inc);
        
    }
    
    double obtener_z(double prob)
    {
        return (prob <= 0.5)? func_z(prob) : -func_z(1.0 - prob);    
        
    }
    
    public static void main(String args[])
    {
        DistNormal d = new DistNormal();
        
        System.out.println("P(Z < 2.66) : " + d.dist_normal(2.66));    
        System.out.println("Z(0.9962) : " + d.obtener_z(0.9962));
    }
}

Bajar archivo