/*

determine dcf based on hard decisions

*/

#include <stdio.h>

#include <math.h>


#define Cmiss  10.0

#define Cfa    1.0

#define Ptarg  0.01

#define Pnont  0.99

#define PMIN   0.0000
#define PMAX   1.0


main (argc, argv)

int argc;
char *argv[];

  {
  FILE *truefile;
  FILE *falsefile;
  char inline[100];
  int spknum;
  char seg[12];
  char hdec;
  int targct = 0;
  int missct = 0;
  int backct = 0;
  int fact = 0;
  double dcf;
  double missprob;
  double faprob;
  double plotmissprob;
  double plotfaprob;
  double dcfmiss, dcffa;

  double ppndf ();

  truefile = fopen (argv[1], "r");
  while (fgets (inline, 80, truefile) != NULL)
    {
    sscanf (inline, "%*s %*s %d %*s %s %c %*s", &spknum, seg, &hdec);
/* printf ("inline = %s\n", inline); */
      targct++;
      if (hdec == 'f' || hdec == 'F')
        missct++;
    }

  fclose (truefile);


  falsefile = fopen (argv[2], "r");
  while (fgets (inline, 80, falsefile) != NULL)
    {
    sscanf (inline, "%*s %*s %d %*s %s %c %*s", &spknum, seg, &hdec);
/* printf ("inline = %s\n", inline); */
      backct++;
      if (hdec == 't' || hdec == 'T')
        fact++;
    }

  fclose (falsefile);

  missprob = (double) missct / (double) targct;
  faprob = (double) fact / (double) backct;

/* printf ("missct = %d, targct = %d, fact = %d, backct = %d\n", missct, targct, fact, backct); */

  dcfmiss = Cmiss * Ptarg * missprob;
  dcffa = Cfa * Pnont * faprob;
  dcf = dcfmiss + dcffa;

/*  dcf = Cmiss * Ptarg * (missprob) + Cfa * Pnont * (faprob); */

  if (missprob < PMIN)
    missprob = PMIN;
  if (missprob > PMAX)
    missprob = PMAX;
  if (faprob < PMIN)
    faprob = PMIN;
  if (faprob > PMAX)
    faprob = PMAX;
/*
printf ("missprob = %lf\n", missprob);
printf ("faprob = %lf\n", faprob);
*/
  plotmissprob = ppndf (missprob);
  plotfaprob = ppndf (faprob);
  
  printf ("miss prob plotted = %lf\n", plotmissprob);
  printf ("fa   prob plotted = %lf\n", plotfaprob);
  printf ("DCF               = %lf\n", dcf);
  printf ("DCFMISS           = %lf\n", dcfmiss);
  printf ("DCFFA             = %lf\n", dcffa);

/*
  printf ("plotted miss probability = %lf\n", plotmissprob);
  printf ("plotted fa   probability = %lf\n", plotfaprob);
*/
 
  }


#define abs(x)  (x < 0  ?  -x : x)
 
#define SPLIT      0.42
 
#define A0      2.5066282388
#define A1    -18.6150006252
#define A2     41.3911977353
#define A3    -25.4410604963
#define B1     -8.4735109309
#define B2     23.0833674374
#define B3    -21.0622410182
#define B4      3.1308290983
#define C0     -2.7871893113
#define C1     -2.2979647913
#define C2      4.8501412713
#define C3      2.3212127685
#define D1      3.5438892476
#define D2      1.6370678189
 
#define LL      140


#define eps     2.2204e-16
 
double ppndf (p)
 
double p;
 
{
  double q;
  double r;
  double retval;
 
 
/* printf ("p = %f\n", p); */
 
  if (p >= 1.0)
    p = 1 - eps;
  if (p <= 0.0)
    p = eps;
 
  q = p - 0.5;
  if (abs(q) <= SPLIT)
      {
    r = q * q;
    retval = q * (((A3 * r + A2) * r + A1) * r + A0) /
             ((((B4 * r + B3) * r + B2) * r + B1) * r + 1.0);
}
 
  else
 
      {
/*
    r = sqrt (log (0.5 - abs(q)));
*/
 
    r = (q > 0.0  ?  1.0 - p : p);
    if (r <= 0.0)
	{
      printf ("Found r = %f\n", r);
      return;
  }


    r = sqrt ((-1.0) * log (r));

 
    retval = (((C3 * r + C2) * r + C1) * r + C0) /
             ((D2 * r + D1) * r + 1.0);
    if (q < 0)
      retval *= -1.0;
}
 
    return (retval);
 
}


