Skip to content

Commit

Permalink
Fix put option typo and format
Browse files Browse the repository at this point in the history
  • Loading branch information
kchristin22 committed Nov 14, 2024
1 parent b6f03a4 commit c5f8ba6
Show file tree
Hide file tree
Showing 9 changed files with 931 additions and 981 deletions.
28 changes: 14 additions & 14 deletions demos/CUDA/BlackScholes/BlackScholes.cu
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ double computeL1norm_Call(float* S, float* X, float* T, float* d, Greek greek) {
return sum_delta / sum_ref;
}

double computeL1norm_Pull(float* S, float* X, float* T, float* d, Greek greek) {
double computeL1norm_Put(float* S, float* X, float* T, float* d, Greek greek) {
double delta, ref, sum_delta, sum_ref;
sum_delta = 0;
sum_ref = 0;
Expand Down Expand Up @@ -337,7 +337,7 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}

// Compute the derivatives of the price of the pull options
// Compute the derivatives of the price of the Put options
for (int i = 0; i < OPT_N; i++) {
h_CallResultCPU[i] = 0.0f;
h_PutResultCPU[i] = -1.0f;
Expand All @@ -356,34 +356,34 @@ int main(int argc, char** argv) {
d_PutResultGPU, d_StockPrice, d_OptionStrike, d_OptionYears);

// Verify delta
L1norm = computeL1norm_Pull(h_StockPrice, h_OptionStrike, h_OptionYears,
d_StockPrice, Delta);
printf("L1norm of delta for Pull option = %E\n", L1norm);
L1norm = computeL1norm_Put(h_StockPrice, h_OptionStrike, h_OptionYears,
d_StockPrice, Delta);
printf("L1norm of delta for Put option = %E\n", L1norm);
if (L1norm > 1e-5) {
printf("Gradient test failed: the difference between the computed and "
"the approximated theoretical delta for Pull option is larger than "
"the approximated theoretical delta for Put option is larger than "
"expected\n");
return EXIT_FAILURE;
}

// Verify derivatives with respect to the Strike price
L1norm = computeL1norm_Pull(h_StockPrice, h_OptionStrike, h_OptionYears,
d_OptionStrike, dX);
printf("L1norm of derivative of Pull w.r.t. the strike price = %E\n", L1norm);
L1norm = computeL1norm_Put(h_StockPrice, h_OptionStrike, h_OptionYears,
d_OptionStrike, dX);
printf("L1norm of derivative of Put w.r.t. the strike price = %E\n", L1norm);
if (L1norm > 1e-6) {
printf("Gradient test failed: the difference between the computed and the "
"approximated theoretcial derivative of "
"PUll w.r.t. the strike price is larger than expected\n");
"Put w.r.t. the strike price is larger than expected\n");
return EXIT_FAILURE;
}

// Verify theta
L1norm = computeL1norm_Pull(h_StockPrice, h_OptionStrike, h_OptionYears,
d_OptionYears, Theta);
printf("L1norm of theta for Pull option = %E\n", L1norm);
L1norm = computeL1norm_Put(h_StockPrice, h_OptionStrike, h_OptionYears,
d_OptionYears, Theta);
printf("L1norm of theta for Put option = %E\n", L1norm);
if (L1norm > 1e-5) {
printf("Gradient test failed: the difference between the computed and the "
"approximated theoretical theta for Pull option is larger than "
"approximated theoretical theta for Put option is larger than "
"expected\n");
return EXIT_FAILURE;
}
Expand Down
21 changes: 11 additions & 10 deletions demos/CUDA/BlackScholes/BlackScholes_gold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,22 @@ extern "C" double CND(double d) {
double cnd = RSQRT2PI * exp(-0.5 * d * d) *
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))));

if (d > 0) cnd = 1.0 - cnd;
if (d > 0)
cnd = 1.0 - cnd;

return cnd;
}

////////////////////////////////////////////////////////////////////////////////
// Black-Scholes formula for both call and put
////////////////////////////////////////////////////////////////////////////////
static void BlackScholesBodyCPU(float &callResult, float &putResult,
float Sf, // Stock price
float Xf, // Option strike
float Tf, // Option years
float Rf, // Riskless rate
static void BlackScholesBodyCPU(float& callResult, float& putResult,
float Sf, // Stock price
float Xf, // Option strike
float Tf, // Option years
float Rf, // Riskless rate
float Vf // Volatility rate
) {
) {
double S = Sf, X = Xf, T = Tf, R = Rf, V = Vf;

double sqrtT = sqrt(T);
Expand All @@ -81,9 +82,9 @@ static void BlackScholesBodyCPU(float &callResult, float &putResult,
////////////////////////////////////////////////////////////////////////////////
// Process an array of optN options
////////////////////////////////////////////////////////////////////////////////
extern "C" void BlackScholesCPU(float *h_CallResult, float *h_PutResult,
float *h_StockPrice, float *h_OptionStrike,
float *h_OptionYears, float Riskfree,
extern "C" void BlackScholesCPU(float* h_CallResult, float* h_PutResult,
float* h_StockPrice, float* h_OptionStrike,
float* h_OptionYears, float Riskfree,
float Volatility, int optN) {
for (int opt = 0; opt < optN; opt++)
BlackScholesBodyCPU(h_CallResult[opt], h_PutResult[opt], h_StockPrice[opt],
Expand Down
15 changes: 8 additions & 7 deletions demos/CUDA/BlackScholes/BlackScholes_kernel.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ __device__ inline float cndGPU(float d) {
float cnd = RSQRT2PI * expf(-0.5f * d * d) *
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))));

if (d > 0) cnd = 1.0f - cnd;
if (d > 0)
cnd = 1.0f - cnd;

return cnd;
}

////////////////////////////////////////////////////////////////////////////////
// Black-Scholes formula for both call and put
////////////////////////////////////////////////////////////////////////////////
__device__ inline void BlackScholesBodyGPU(float &CallResult, float &PutResult,
float S, // Stock price
float X, // Option strike
float T, // Option years
float R, // Riskless rate
__device__ inline void BlackScholesBodyGPU(float& CallResult, float& PutResult,
float S, // Stock price
float X, // Option strike
float T, // Option years
float R, // Riskless rate
float V // Volatility rate
) {
) {
float sqrtT, expRT;
float d1, d2, CNDD1, CNDD2;

Expand Down
Loading

0 comments on commit c5f8ba6

Please sign in to comment.