/* nag_anderson_darling_uniform_prob (g08cjc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg08.h>

int main(void)
{
  /* Scalars */
  Integer       exit_status = 0, i, n;
  double        a2, mu, p;
  /* Arrays */
  double        *x = 0, *y = 0;
  /* Nag types */
  Nag_Boolean   issort;
  NagError      fail;

  printf("%s\n\n",
          "nag_anderson_darling_uniform_prob (g08cjc) Example Program Results");

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

  /* Read number of observations and parameter value */
  scanf("%"NAG_IFMT "", &n);
  scanf("%lf", &mu);
  scanf("%*[^\n] ");

  /* Memory allocation */
  if (!(x = NAG_ALLOC((n), double)) ||
      !(y = NAG_ALLOC((n), double)))
  {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
  }

  /* Read observations */
  for (i = 0; i < n; i++)
  {
      scanf("%lf", x+i);
  }
  scanf("%*[^\n]");

  /* PIT */
  for (i = 0; i < n; i++)
  {
      y[i] = 1.0 - exp(-x[i]/mu);
  }

  /* Let nag_anderson_darling_uniform_prob (g08cjc) sort the uniform variates */
  issort = Nag_FALSE;

  /* Calculate the Anderson-Darling goodness-of-fit test statistic and its
     probability for the case of uniformly distributed data */
  INIT_FAIL(fail);
  /* nag_anderson_darling_uniform_prob (g08cjc) */
  nag_anderson_darling_uniform_prob(n, issort, y, &a2, &p, &fail);

  /* Results */
  printf("%s ", " H0: data from exponential distribution with mean");
  printf("%f\n", mu);
  printf("%s ", " Test statistic, A-squared: ");
  printf("%f\n", a2);
  printf("%s ", " Upper tail probability:    ");
  printf("%f\n", p);

 END:
  NAG_FREE(x);
  NAG_FREE(y);

  return exit_status;
}