/*
 * F.java
 *
 * Created on March 8, 2006, 1:32 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package probabilitycalculator;

/**
 *
 * @author Rinaman
 */
public class F extends Distributions {
    
    private double x;
    private int dfn, dfd, option;
    
    /** Creates a new instance of F */
    public F(double dx, int ParmOne, int ParmTwo, int ioption) {
        x = dx;
        dfn = ParmOne;
        dfd = ParmTwo;
        option = ioption;
    }
    
    public double FCalc() {
        double xdfn, xdfd;
        xdfn = (double)dfn;
        xdfd = (double)dfd;
        if(option == 1) return df(x, xdfn, xdfd);
        else if(option == 2) return 1.0 - df(x, xdfn, xdfd);
        else if(option == 3) return invdfF(x, xdfn, xdfd);
        else return invdfF(1.0 - x, xdfn, xdfd);
    }
    
    public double invdfF(double x, double dfn, double dfd) {
        double lo, hi, dflo, dfhi;
        if(x <= 0.0)return 0.0;
        if(x >= 1.0)return 1.0e12;
        lo = 0.0;
        hi = 10.0;
        dflo = x - df(lo, dfn, dfd);
        dfhi = x - df(hi, dfn, dfd);
        while(dflo*dfhi > 0.0){
            lo = hi;
            hi = hi + 10.0;
            dflo = x - df(lo, dfn, dfd);
            dfhi = x - df(hi, dfn, dfd);
        }
        return rtflsp(x, lo, hi, 1.0e-6, dfn, dfd);
    }
    
    public double df(double x, double dfn, double dfd) {
        if(x <= 0.0)return 0.0;
        return 1.0 - betai(dfd/2.0, dfn/2.0, (dfd/(dfd+dfn*x)));
    }
}
