/* nag_estim_weibull (g07bec) 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   beta, corr, dev, gamma, sebeta, segam, tol;
  Integer  exit_status, i, maxit, n, nit;
  NagError fail;

  /* Arrays */
  double   *x = 0;
  Integer  *ic = 0;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_estim_weibull (g07bec) Example Program Results\n");

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

  /* Allocate memory */
  if (!(x = NAG_ALLOC(n, double)) ||
      !(ic = NAG_ALLOC(n, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  for (i = 1; i <= n; ++i)
    scanf("%lf", &x[i - 1]);
  scanf("%*[^\n] ");

  /* If data were censored then ic would also be read in.
   *  Leave nag_estim_weibull (g07bec) to calculate initial values
   */
  gamma = 0.0;
  /* Use default values for tol and maxit */
  tol = 0.0;
  maxit = 0;
  /* nag_estim_weibull (g07bec).
   * Computes maximum likelihood estimates for parameters of
   * the Weibull distribution
   */
  nag_estim_weibull(Nag_NoCensored, n, x, ic, &beta, &gamma, tol, maxit,
                    &sebeta, &segam, &corr, &dev, &nit, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_estim_weibull (g07bec).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf("\n");
  printf("Beta  = %10.4f Standard error = %10.4f\n", beta, sebeta);
  printf("Gamma = %10.4f Standard error = %10.4f\n", gamma, segam);
 END:
  NAG_FREE(x);
  NAG_FREE(ic);
  return exit_status;
}