Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
hudde committed Feb 18, 2024
1 parent a498a63 commit aa22824
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: greeks
Title: Sensitivities of Prices of Financial Options and Implied Volatilities
Version: 1.3.3
Version: 1.3.4
Authors@R:
person(given = "Anselm",
family = "Hudde",
Expand Down
25 changes: 14 additions & 11 deletions src/Binomial_American_Greeks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ using std::string;
using std::max;


double american_call(double x, double price, double exercise_price) {
return max(x, price - exercise_price);
double american_call(double price, double exercise_price) {
return max(0.0, price - exercise_price);
}

double american_put(double x, double price, double exercise_price) {
return max(x, exercise_price - price);
double american_put(double price, double exercise_price) {
return max(0.0, exercise_price - price);
}

// [[Rcpp::export]]
Expand All @@ -26,7 +26,7 @@ NumericVector Binomial_American_Greeks_cpp(double initial_price = 100,
const int steps = 1000) {

// payoff_function
double (*payoff_function)(double x, double price, double exercise_price);
double (*payoff_function)(double price, double exercise_price);

// iterators
int i;
Expand All @@ -42,7 +42,7 @@ NumericVector Binomial_American_Greeks_cpp(double initial_price = 100,
const double down = exp(-volatility * sqrt(dt));
const double p = (exp((r-dividend_yield)*dt) - down)/(up-down);
const double q = (1 - p);
const double exp_min_r_dt = exp(-r*dt);
const double exp_min_r_steps_dt = exp(-r*steps*dt);

// generate the price vector
NumericVector price(2*steps+1);
Expand All @@ -62,17 +62,20 @@ NumericVector Binomial_American_Greeks_cpp(double initial_price = 100,
}

for(i = 0; i <= steps; i++) {
american_value(i) = payoff_function(0.0, price(2*steps - 2*i), exercise_price);
european_value(i) =
exp_min_r_steps_dt * payoff_function(price(2*steps - 2*i), exercise_price);
}

european_value = clone(american_value);
american_value = clone(european_value);

for(j = steps-1; j >= 0; j--) {
for(i = 0; i <= j; i++) {
american_value(i) = payoff_function(
exp_min_r_dt * (p*american_value(i) + q*american_value(i+1)),
price(2*steps - 2*i + j - steps), exercise_price);
european_value(i) = p * european_value(i) + q * european_value(i+1);
american_value(i) = p * american_value(i) + q * american_value(i+1);
american_value(i) = max(
exp(-(r-dividend_yield)*j*dt) *
payoff_function(price(2*steps - 2*i + j - steps), exercise_price),
american_value(i));
}
}

Expand Down

0 comments on commit aa22824

Please sign in to comment.