NAG Library Manual, Mark 28.5
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_specfun_opt_binary_aon_greeks (s30cdc) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.5, 2022.
 */
#include <math.h>
#include <nag.h>
#include <stdio.h>
#include <string.h>

int main(void) {
  /* Integer scalar and array declarations */
  Integer exit_status = 0;
  Integer i, j, m, n;
  NagError fail;
  Nag_CallPut putnum;
  /* Double scalar and array declarations */
  double q, r, s, sigma;
  double *charm = 0, *colour = 0, *delta = 0, *gamma = 0, *p = 0;
  double *rho = 0, *rhoq = 0, *speed = 0, *t = 0, *theta = 0;
  double *vanna = 0, *vega = 0, *vomma = 0, *x = 0, *zomma = 0;
  /* Character scalar and array declarations */
  char put[8 + 1];
  Nag_OrderType order;

  INIT_FAIL(fail);

  printf(
      "nag_specfun_opt_binary_aon_greeks (s30cdc) Example Program Results\n");
  printf("Binary (Digital): Asset-or-Nothing\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Read put */
  scanf("%8s%*[^\n] ", put);
  /*
   * nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  putnum = (Nag_CallPut)nag_enum_name_to_value(put);
  /* Read s, sigma, r, q */
  scanf("%lf%lf%lf%lf%*[^\n] ", &s, &sigma, &r, &q);
  /* Read m, n */
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &m, &n);
#ifdef NAG_COLUMN_MAJOR
#define CHARM(I, J) charm[(J - 1) * m + I - 1]
#define COLOUR(I, J) colour[(J - 1) * m + I - 1]
#define DELTA(I, J) delta[(J - 1) * m + I - 1]
#define GAMMA(I, J) gamma[(J - 1) * m + I - 1]
#define P(I, J) p[(J - 1) * m + I - 1]
#define RHO(I, J) rho[(J - 1) * m + I - 1]
#define RHOQ(I, J) rhoq[(J - 1) * m + I - 1]
#define SPEED(I, J) speed[(J - 1) * m + I - 1]
#define THETA(I, J) theta[(J - 1) * m + I - 1]
#define VANNA(I, J) vanna[(J - 1) * m + I - 1]
#define VEGA(I, J) vega[(J - 1) * m + I - 1]
#define VOMMA(I, J) vomma[(J - 1) * m + I - 1]
#define ZOMMA(I, J) zomma[(J - 1) * m + I - 1]
  order = Nag_ColMajor;
#else
#define CHARM(I, J) charm[(I - 1) * n + J - 1]
#define COLOUR(I, J) colour[(I - 1) * n + J - 1]
#define DELTA(I, J) delta[(I - 1) * n + J - 1]
#define GAMMA(I, J) gamma[(I - 1) * n + J - 1]
#define P(I, J) p[(I - 1) * n + J - 1]
#define RHO(I, J) rho[(I - 1) * n + J - 1]
#define RHOQ(I, J) rhoq[(I - 1) * n + J - 1]
#define SPEED(I, J) speed[(I - 1) * n + J - 1]
#define THETA(I, J) theta[(I - 1) * n + J - 1]
#define VANNA(I, J) vanna[(I - 1) * n + J - 1]
#define VEGA(I, J) vega[(I - 1) * n + J - 1]
#define VOMMA(I, J) vomma[(I - 1) * n + J - 1]
#define ZOMMA(I, J) zomma[(I - 1) * n + J - 1]
  order = Nag_RowMajor;
#endif
  if (!(charm = NAG_ALLOC(m * n, double)) ||
      !(colour = NAG_ALLOC(m * n, double)) ||
      !(delta = NAG_ALLOC(m * n, double)) ||
      !(gamma = NAG_ALLOC(m * n, double)) || !(p = NAG_ALLOC(m * n, double)) ||
      !(rho = NAG_ALLOC(m * n, double)) || !(rhoq = NAG_ALLOC(m * n, double)) ||
      !(speed = NAG_ALLOC(m * n, double)) || !(t = NAG_ALLOC(n, double)) ||
      !(theta = NAG_ALLOC(m * n, double)) ||
      !(vanna = NAG_ALLOC(m * n, double)) ||
      !(vega = NAG_ALLOC(m * n, double)) ||
      !(vomma = NAG_ALLOC(m * n, double)) || !(x = NAG_ALLOC(m, double)) ||
      !(zomma = NAG_ALLOC(m * n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  /* Read array of strike/exercise prices, X */
  for (i = 0; i < m; i++)
    scanf("%lf ", &x[i]);
  scanf("%*[^\n] ");
  /* Read array of times to expiry */
  for (i = 0; i < n; i++)
    scanf("%lf ", &t[i]);
  scanf("%*[^\n] ");
  /*
   * nag_specfun_opt_binary_aon_greeks (s30cdc)
   * Binary option: asset-or-nothing pricing formula with Greeks
   */
  nag_specfun_opt_binary_aon_greeks(
      order, putnum, m, n, x, s, t, sigma, r, q, p, delta, gamma, vega, theta,
      rho, rhoq, vanna, charm, speed, colour, zomma, vomma, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_specfun_opt_binary_aon_greeks (s30cdc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  if (putnum == Nag_Call)
    printf("European Call :\n\n");
  else if (putnum == Nag_Put)
    printf("European Put :\n\n");
  printf("%s%8.4f\n", "  Spot       = ", s);
  printf("%s%8.4f\n", "  Volatility = ", sigma);
  printf("%s%8.4f\n", "  Rate       = ", r);
  printf("%s%8.4f\n", "  Dividend   = ", q);
  printf("\n");
  for (j = 1; j <= n; j++) {
    printf("\n");
    printf("  Time to Expiry :    %8.4f\n", t[j - 1]);
    printf("   Strike     Price     Delta     Gamma      Vega"
           "     Theta       Rho      Rhoq\n");
    for (i = 1; i <= m; i++)
      printf("%9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n", x[i - 1],
             P(i, j), DELTA(i, j), GAMMA(i, j), VEGA(i, j), THETA(i, j),
             RHO(i, j), RHOQ(i, j));
    printf("                        Vanna     Charm     Speed"
           "    Colour     Zomma     Vomma\n");
    for (i = 1; i <= m; i++)
      printf("%29.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n", VANNA(i, j), CHARM(i, j),
             SPEED(i, j), COLOUR(i, j), ZOMMA(i, j), VOMMA(i, j));
  }

END:
  NAG_FREE(charm);
  NAG_FREE(colour);
  NAG_FREE(delta);
  NAG_FREE(gamma);
  NAG_FREE(p);
  NAG_FREE(rho);
  NAG_FREE(rhoq);
  NAG_FREE(speed);
  NAG_FREE(t);
  NAG_FREE(theta);
  NAG_FREE(vanna);
  NAG_FREE(vega);
  NAG_FREE(vomma);
  NAG_FREE(x);
  NAG_FREE(zomma);

  return exit_status;
}