/* nag_poisson_ci (g07abc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2001.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg07.h>

int main(void)
{

  /* Scalars */
  double   clevel, sum, tl, tu, xmean;
  Integer  exit_status, i, ifreq, n, num;
  NagError fail;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_poisson_ci (g07abc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");


  /* Read in the number of Noxious Seeds in a sub sample and
   * the frequency with which that number occurs.
   */

  /* Compute the sample mean */
  sum = 0.0;
  n = 0;
  while (scanf("%ld%ld%*[^\n] ", &num, &ifreq) != EOF)
    {
      sum += (double) num * (double) ifreq;
      n += ifreq;
    }
  xmean = sum / (double) n;

  printf("\n");
  printf("The point estimate of the Poisson parameter = %6.4f\n",
          xmean);
  for (i = 1; i <= 2; ++i)
    {
      if (i == 1)
        {
          clevel = 0.95;
          printf("\n");
          printf("95 percent Confidence Interval for the estimate\n");
        }
      else
        {
          clevel = 0.99;
          printf("99 percent Confidence Interval for the estimate\n");
        }
      /* nag_poisson_ci (g07abc).
       * Computes confidence interval for the parameter of a
       * Poisson distribution
       */
      nag_poisson_ci(n, xmean, clevel, &tl, &tu, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_poisson_ci (g07abc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      printf("( %6.4f , %6.4f )\n", tl, tu);
      printf("\n");
    }

 END:
  return exit_status;
}