Black–Scholes–Merton option pricing formula, function s30aac |
According to the C Library Manual, the prototype for function s30aac looks like this:
#include <nag.h> #include <nags.h> void s30aac(Nag_OrderType order, Nag_PutType iput, Integer m, Integer n, const double x[], double s, const double t[], double sigma, double r, double q, double p[], NagError *fail)The function s30aac is designed to compute the price of a European call or put (determined by iput) option for constant volatility, sigma, and risk-free interest rate, r, with a possible dividend yield, q, using the Black–Scholes–Merton formula.
The computing option prices are returned via array argument p.
For full description of the roles of all routine arguments consult the s30aac routine document in the NAG C Library Manual.
As with Example 1, Example 2 and Example 3, we will not attempt to pass the contents of the NagError structure back to Java. In our Java program, we will declare the function like this:
// Declaration of the Native (C) function private native int s30aac(char calput, int m, int n, double[] x, double s, double[] t, double sigma, double r, double q, double[] p);i.e. a method with return type int.
Note that we choose not to pass all possible arguments - the order is missing. We could include this argument if we wanted the information it contains to be returned to Java; here we don't. Also the argument iput is replaced with an argument of type char. Later on we will convert it into Nag_PutType argument. Since we are also not using the ifail argument, we will use the int return value to send back any error code.
public class EuropeanOptPrice { // Declaration of the Native (C) function private native int s30aac(char calput, int m, int n, double[] x, double s, double[] t, double sigma, double r, double q, double[] p); static { // The runtime system executes a class's static // initializer when it loads the class. System.loadLibrary("nagCJavaInterface"); } // The main program public static void main(String[] args) { double x[], t[], p[]; double s, sigma, r, q; int i, j, m, n, retCode; char calput; // Create an object of class EuropeanOptPrice EuropeanOptPrice price = new EuropeanOptPrice(); calput = 'C'; s = 55.0; sigma = 0.3; r = 0.1; q = 0.0; m = 3; n = 2; p = new double[m*n]; x = new double[m]; t = new double[n]; for (i = 0; i < m*n; i++) p[i] = 0.0; x[0] = 58.0; x[1] = 60.0; x[2] = 62.0; t[0] = 0.7; t[1] = 0.8; System.out.println(); System.out.println("Call of NAG Black-Scholes-Merton option pricing routine s30aac"); System.out.println(); // Call method s30aac of object price retCode = price.s30aac(calput, m, n, x, s, t, sigma, r, q, p); System.out.print("Return code from s30aac = "); System.out.println(retCode); System.out.println(); if (retCode == 0) { // Print the input values System.out.println("European Call:"); System.out.println("Spot " + s); System.out.println("Volatility " + sigma); System.out.println("Rate " + r); System.out.println("Dividend " + q); System.out.println(); // Print the solution System.out.println(" Strike Expiry Option Price"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) System.out.format("%8.4f %8.4f %8.4f%n",x[i],t[j],p[i*n + j]); } System.out.println(); } } }The main program simply assigns values of the arguments, and calls the native method using those arguments.
We can compile our Java program with the following command:
% javac EuropeanOptPrice.java
% javah -jni EuropeanOptPriceThe generated header file, EuropeanOptPrice.h, contains this function prototype:
JNIEXPORT jint JNICALL Java_EuropeanOptPrice_s30aac (JNIEnv *, jobject, jchar, jint, jint, jdoubleArray, jdouble, jdoubleArray, jdouble, jdouble, jdouble, jdoubleArray);
#include <jni.h> /* Java Native Interface headers */ #include "EuropeanOptPrice.h" /* Auto-generated header created by javah -jni */ #include <nag.h> /* NAG C Library headers */ #include <nags.h> /* Our C definition of the function s30aac declared in EuropeanOptPrice.java */ JNIEXPORT jint JNICALL Java_EuropeanOptPrice_s30aac (JNIEnv *env, jobject obj, jchar calput, jint m, jint n, jdoubleArray x, jdouble s, jdoubleArray t, jdouble sigma, jdouble r, jdouble q, jdoubleArray p) { static NagError fail; Nag_PutType iput; Nag_OrderType order; /* First extract the arrays from Java */ jdouble *xpt, *tpt, *ppt; jboolean isCopy; xpt = (*env)->GetDoubleArrayElements(env, x, &isCopy); tpt = (*env)->GetDoubleArrayElements(env, t, &isCopy); ppt = (*env)->GetDoubleArrayElements(env, p, &isCopy); /* Java stores arrays in row order */ order = Nag_RowMajor; /* Convert calput into Nag_PutType */ if (calput == 'P') { iput = Nag_Put; } else if (calput == 'C') { iput = Nag_Call; } /* Call s30aac */ fail.print = Nag_FALSE; s30aac(order, iput, m, n, xpt, s, tpt, sigma, r, q, ppt, &fail); /* Release the array elements back to Java */ (*env)->ReleaseDoubleArrayElements(env, x, xpt, 0); (*env)->ReleaseDoubleArrayElements(env, t, tpt, 0); (*env)->ReleaseDoubleArrayElements(env, p, ppt, 0); /* Return any fail code that the nagc.dll function s30aac returned. */ return fail.code; }Points to note:
typedef unsigned short jchar;Therefore, we can make simple comparisons using if statement to assign iput the right value.
% cc -c -fPIC -I/opt/jdk1.6.0_11/include -I/opt/jdk1.6.0_11/include/linux \ -I/opt/NAG/cll6a09dhl/include EuropeanOptPriceImp.c % ld -G -z defs EuropeanOptPriceImp.o -o libnagCJavaInterface.so \ /opt/NAG/cll6a09dhl/lib/libnagc_nag.so -lm -lc -lpthread
Recall that on other UNIX machines it may be necessary to add further libraries at link time - see note.
C:\> cl -Ic:\jdk1.6.0_11\include -Ic:\jdk1.6.0_11\include\win32 -I"c:\Program Files\NAG\CL09\clw3209dal\include" /Gz -LD EuropeanOptPriceImp.c "c:\Program Files\NAG\CL09\clw3209dal\lib\CLW3209DA_nag.lib" -FenagCJavaInterface.dll
The compiler flags used were described in Section 7 of Example 1.
% java EuropeanOptPriceThe expected output looks like this:
Call of NAG Black-Scholes-Merton option pricing routine s30aac Return code from s30aac = 0 European Call: Spot 55.0 Volatility 0.3 Rate 0.1 Dividend 0.0 Strike Expiry Option Price 58.0000 0.7000 5.9198 58.0000 0.8000 6.5506 60.0000 0.7000 5.0809 60.0000 0.8000 5.6992 62.0000 0.7000 4.3389 62.0000 0.8000 4.9379
(If you get an error message saying that a library cannot be located, see the tip given in Example 1).
% javac EuropeanOptPrice.java
% javah -jni EuropeanOptPrice
% gcc -c -fPIC -I/opt/jdk1.6.0_11/include -I/opt/jdk1.6.0_11/include/linux \ -I/opt/NAG/cll6a09dhl/include EuropeanOptPriceImp.c % ld -G -z defs EuropeanOptPriceImp.o -o libnagCJavaInterface.so \ /opt/NAG/cll6a09dhl/lib/libnagc_nag.so -lm -lc -lpthreadwhere /opt/jdk1.6.0_11/include, /opt/jdk1.6.0_11/include/linux, /opt/NAG/cll6a09dhl/include and /opt/NAG/cll6a09dhl/lib are directory names appropriate to your Java and NAG C Library installations .
C:\> cl -Ic:\jdk1.6.0_11\include -Ic:\jdk1.6.0_11\include\win32 -I"c:\Program Files\NAG\CL09\clw3209dal\include" /Gz -LD EuropeanOptPriceImp.c "c:\Program Files\NAG\CL09\clw3209dal\lib\CLW3209DA_nag.lib" -FenagCJavaInterface.dllwhere c:\jdk1.6.0_11\include, c:\jdk1.6.0_11\include\win32, "c:\Program Files\NAG\CL09\clw3209dal\include" and "c:\Program Files\NAG\CL09\clw3209dal\lib" are directory names appropriate to your Java and NAG C Library installations.
% java EuropeanOptPrice