Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed up tests, line formatting

  • Loading branch information...
commit 2469869370f833dff97128087ba9afeffbac4551 1 parent baeb5d8
@brymck authored
View
32 ext/rupee/options.c
@@ -1,13 +1,14 @@
#include "rupee.h"
-/* Whether a particular string refers to a call option (returns false for put) */
+/* Whether a particular string refers to a call option (false for put) */
static bool is_call(const char *call_put_flag)
{
/* Returns true for everything unless it starts with a 'p' */
return (call_put_flag[0] != 'p');
}
-static double bs(const char *call_put_flag, double S, double X, double T, double r, double q, double v)
+static double bs(const char *call_put_flag, double S, double X, double T,
+ double r, double q, double v)
{
double d1, d2;
@@ -21,7 +22,8 @@ static double bs(const char *call_put_flag, double S, double X, double T, double
}
/* call-seq:
- * Rupee.black_scholes(call_put_flag, forward, strike_price, time_to_expiry, risk_free_rate, volatility)
+ * black_scholes(call_put_flag, forward, strike_price, time_to_expiry,
+ * risk_free_rate, volatility)
*
* The Black-Scholes European call/put valuation
*
@@ -35,7 +37,8 @@ static double bs(const char *call_put_flag, double S, double X, double T, double
* * +dividend_yield+ - The annual dividend yield
* * +volatility+ - The implied volatility at expiry
*/
-static VALUE rupee_black_scholes(VALUE self, VALUE rcall_put_flag, VALUE rF, VALUE rX, VALUE rT, VALUE rr, VALUE rq, VALUE rv)
+static VALUE rupee_black_scholes(VALUE self, VALUE rcall_put_flag, VALUE rF,
+ VALUE rX, VALUE rT, VALUE rr, VALUE rq, VALUE rv)
{
const char *call_put_flag;
double F, X, T, r, q, v;
@@ -51,7 +54,8 @@ static VALUE rupee_black_scholes(VALUE self, VALUE rcall_put_flag, VALUE rF, VAL
return rb_float_new(bs(call_put_flag, F, X, T, r, q, v));
}
-double gbs(const char *call_put_flag, double S, double X, double T, double r, double b, double v)
+double gbs(const char *call_put_flag, double S, double X, double T, double r,
+ double b, double v)
{
double d1, d2;
@@ -65,7 +69,8 @@ double gbs(const char *call_put_flag, double S, double X, double T, double r, do
}
/* call-seq:
- * Rupee.generalized_black_scholes(call_put_flag, forward, strike_price, time_to_expiry, risk_free_rate, volatility)
+ * generalized_black_scholes(call_put_flag, forward, strike_price,
+ * time_to_expiry, risk_free_rate, volatility)
*
* The generalized Black-Scholes European call/put valuation
*
@@ -79,7 +84,8 @@ double gbs(const char *call_put_flag, double S, double X, double T, double r, do
* * +cost_of_carry+ - The annualized cost of carry
* * +volatility+ - The implied volatility at expiry
*/
-static VALUE rupee_generalized_black_scholes(VALUE self, VALUE rcall_put_flag, VALUE rF, VALUE rX, VALUE rT, VALUE rr, VALUE rb, VALUE rv)
+static VALUE rupee_generalized_black_scholes(VALUE self, VALUE rcall_put_flag,
+ VALUE rF, VALUE rX, VALUE rT, VALUE rr, VALUE rb, VALUE rv)
{
const char *call_put_flag;
double F, X, T, r, b, v;
@@ -95,7 +101,8 @@ static VALUE rupee_generalized_black_scholes(VALUE self, VALUE rcall_put_flag, V
return rb_float_new(gbs(call_put_flag, F, X, T, r, b, v));
}
-static double black76(const char *call_put_flag, double F, double X, double T, double r, double v)
+static double black76(const char *call_put_flag, double F, double X, double T,
+ double r, double v)
{
double d1, d2;
@@ -109,7 +116,8 @@ static double black76(const char *call_put_flag, double F, double X, double T, d
}
/* call-seq:
- * Rupee.black76(call_put_flag, forward, strike_price, time_to_expiry, risk_free_rate, volatility)
+ * black76(call_put_flag, forward, strike_price, time_to_expiry,
+ * risk_free_rate, volatility)
*
* The Black-76 valuation for options on futures and forwards
*
@@ -122,7 +130,8 @@ static double black76(const char *call_put_flag, double F, double X, double T, d
* * +risk_free_rate+ - The risk-free rate through expiry
* * +volatility+ - The implied volatility at expiry
*/
-static VALUE rupee_black76(VALUE self, VALUE rcall_put_flag, VALUE rF, VALUE rX, VALUE rT, VALUE rr, VALUE rv)
+static VALUE rupee_black76(VALUE self, VALUE rcall_put_flag, VALUE rF, VALUE rX,
+ VALUE rT, VALUE rr, VALUE rv)
{
const char *call_put_flag;
double F, X, T, r, v;
@@ -150,7 +159,8 @@ void init_options()
rb_define_singleton_method(klass, "black_scholes", rupee_black_scholes, 7);
rb_define_alias(singleton, "bs", "black_scholes");
- rb_define_singleton_method(klass, "generalized_black_scholes", rupee_generalized_black_scholes, 7);
+ rb_define_singleton_method(klass, "generalized_black_scholes",
+ rupee_generalized_black_scholes, 7);
rb_define_alias(singleton, "gbs", "generalized_black_scholes");
rb_define_singleton_method(klass, "black76", rupee_black76, 6);
}
View
3  ext/rupee/options.h
@@ -3,7 +3,8 @@
#include "rupee.h"
-double gbs(const char *call_put_flag, double S, double X, double T, double r, double b, double v);
+double gbs(const char *call_put_flag, double S, double X, double T, double r,
+ double b, double v);
void init_options();
#endif
View
1  ext/rupee/rupee.c
@@ -2,7 +2,6 @@
VALUE module;
-/* Ruby calls this to load the extension */
void Init_rupee(void)
{
module = rb_define_module("Rupee");
View
4 ext/rupee/statistics.c
@@ -29,8 +29,7 @@ double cnd(double z)
return dCND;
}
-/* call-seq:
- * Rupee.cnd(z)
+/* call-seq: cnd(z)
*
* Returns the standard normal cumulative distribution (has a mean of zero and
* a standard deviation of one).
@@ -44,6 +43,7 @@ static VALUE rupee_cnd(VALUE self, VALUE rz)
return rb_float_new(cnd(NUM2DBL(rz)));
}
+/* Maybe this does something */
void init_statistics()
{
VALUE klass, singleton;
View
2  lib/rupee/version.rb
@@ -1,4 +1,4 @@
module Rupee
# The current version
- VERSION = "0.0.5"
+ VERSION = "0.0.6"
end
View
20 spec/c/options_spec.rb
@@ -4,11 +4,11 @@
describe "using the Black-76 model" do
describe "on a call option of price $60, strike $65, time to expiry 0.25, risk-free rate 8%, and volatility 30%" do
it "should return $1.7202 for a call" do
- Rupee.black76("c", 60, 65, 0.25, 0.08, 0.3).round(4).should == 1.7202
+ Rupee::Options.black76("c", 60, 65, 0.25, 0.08, 0.3).round(4).should == 1.7202
end
it "should return $6.6212 for a put" do
- Rupee.black76("p", 60, 65, 0.25, 0.08, 0.3).round(4).should == 6.6212
+ Rupee::Options.black76("p", 60, 65, 0.25, 0.08, 0.3).round(4).should == 6.6212
end
end
end
@@ -16,13 +16,13 @@
describe "using the generalized Black-Scholes model" do
describe "on a call option of price $60, strike $65, time to expiry 0.25, risk-free rate 8%, and volatility 30%" do
it "should return $1.7202 for a call" do
- Rupee.generalized_black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 1.7202
- Rupee.gbs("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 1.7202
+ Rupee::Options.generalized_black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 1.7202
+ Rupee::Options.gbs("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 1.7202
end
it "should return $6.6212 for a put" do
- Rupee.generalized_black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 6.6212
- Rupee.gbs("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 6.6212
+ Rupee::Options.generalized_black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 6.6212
+ Rupee::Options.gbs("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 6.6212
end
end
end
@@ -30,13 +30,13 @@
describe "using the Black-Scholes model" do
describe "on a call option of price $60, strike $65, time to expiry 0.25, risk-free rate 8%, and volatility 30%" do
it "should return $1.7202 for a call" do
- Rupee.black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 2.1334
- Rupee.bs("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 2.1334
+ Rupee::Options.black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 2.1334
+ Rupee::Options.bs("c", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 2.1334
end
it "should return $6.6212 for a put" do
- Rupee.black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 5.8463
- Rupee.bs("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 5.8463
+ Rupee::Options.black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 5.8463
+ Rupee::Options.bs("p", 60, 65, 0.25, 0.08, 0, 0.3).round(4).should == 5.8463
end
end
end
View
2  spec/c/statistics_spec.rb
@@ -2,6 +2,6 @@
describe "Cumulative normal distribution" do
it "should return a probability of 50% for 0" do
- Rupee.cnd(0).round(4).should == 0.5
+ Rupee::Stat.cnd(0).round(4).should == 0.5
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.