""" Code to compute the chisquare increment

Context : SRP
Module  : SRPChiSqIncrement.py
Version : 2.0.0
Author  : Stefano Covino
Date    : 18/08/2011
E-mail  : stefano.covino@brera.inaf.it
URL:    : http://www.merate.mi.astro.it/utenti/covino

Usage   : SRPChiSqIncrement [-a arg1] [-c arg2] -d arg3 [-p arg4] [-v]
            -a is the accuracy of the chisquare increment computation
            -c is the resulting chisquare for a fit
            -d is the number of degrees of freedom
            -p is the probability.
           
         The routine allows one to compute the increment for the chi square having a probability
         100-prob% to occur randonmly. Typical usage is for deriving uncertainties for multiparametric
         fits. Alternatively, one can compute the probability to have randomly a higher chisquare than
         the one obtained in a fit.

Remarks : 

History : (30/04/2011) First version.
        : (18/08/2011) Fit probability computation.
"""

import math
from optparse import OptionParser
from SRP.SRPStatistics.ChiSqIncrement import ChiSqIncrement
import SRP.stats


parser = OptionParser(usage="usage: %prog [-a arg1] [-c arg2] -d arg3 [-p arg4] [-v]", version="%prog 2.0.0")
parser.add_option("-a", "--accuracy", action="store", nargs=1, type="float", dest="acc", default=0.001, help="Result accuracy (default 0.001)")
parser.add_option("-c", "--chisquare", action="store", nargs=1, type="float", help="Obtained (not reduced) chisquare value.")
parser.add_option("-d", "--dof", action="store", nargs=1, type="int", dest="dof", help="Number of degree of freedom")
parser.add_option("-p", "--probability", action="store", nargs=1, type="float", dest="prob", default=90.0, help="100 - increment probability (default = 90)")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Fully describe operations.")
(options, args) = parser.parse_args()


if options.dof:
    if options.dof < 1:
        parser.error("Number of degree of freedom must be at least 1.")
    if options.acc <= 0.0:
        parser.error("Accuracy must be a positive number.")    
    if not 0<= options.prob <= 100:
        parser.error("Probability must be in the [0,100] range.")  
    if options.chisquare != None and options.chisquare <= 0:
        parser.error("Chisquare must be positive.")
    #
    if not options.chisquare and options.prob:
        # compute increment
        res = ChiSqIncrement(options.dof,options.prob,options.acc)
        #
        ndgt = int(math.fabs(round(math.log10(options.acc))))+1
        #
        if options.verbose:
            print "Increment for %d dof and %.3f%% probability is %.*f" % (options.dof, options.prob, ndgt, res)
        else:
            print "%d %.3f %.*f" % (options.dof, options.prob, ndgt, res)
    elif options.chisquare:
        # compute significancy
        res = SRP.stats.chisqprob(options.chisquare,options.dof)
        #
        ndgt = int(math.fabs(round(math.log10(options.acc))))+1
        #
        if options.verbose:
            print "Probability for %d dof and %.2g chisquare is %.*g%%" % (options.dof, options.chisquare, ndgt, res*100.)
        else:
            print "%d %.2g %.*g" % (options.dof, options.chisquare, ndgt, 100.*res)
else:
    parser.print_help()

