Permalink
Browse files

Got duration working a bit, plus some knowledge on array conversion

  • Loading branch information...
1 parent 61ba1c8 commit 6bf27f9e1f7ed9ace6cf4de512d7b32c13a3a25f @brymck committed Sep 27, 2011
Showing with 48 additions and 4 deletions.
  1. +1 −1 README.md
  2. +41 −0 ext/rupee/bond.c
  3. +1 −2 ext/rupee/distribution.c
  4. +1 −0 ext/rupee/rupee.c
  5. +3 −0 ext/rupee/rupee.h
  6. +1 −1 lib/rupee/version.rb
View
@@ -74,7 +74,7 @@ Performance
This is just a simple benchmark I ran on my own laptop, where I value a simple
call option with Black-Scholes one million times. You can test the same on
yours with rake, but in any case it makes the point that for the mathematical
-side of finance a native extension in C/C++ has substantial benefits:
+side of finance a native extension has substantial benefits:
rake benchmark:black_scholes
View
@@ -0,0 +1,41 @@
+#include "rupee.h"
+
+static VALUE duration(VALUE self, VALUE rcf_times, VALUE rcfs, VALUE rr)
+{
+ double r, S, D1;
+ int i, cfs_len;
+
+ VALUE *cf_times = RARRAY_PTR(rcf_times);
+ VALUE *cfs = RARRAY_PTR(rcfs);
+ cfs_len = RARRAY_LEN(rcfs);
+ r = NUM2DBL(rr);
+ S = 0;
+ D1 = 0;
+
+ for (i = 0; i < cfs_len; i++) {
+ double cfti, cfi, dcfi;
+
+ cfti = NUM2DBL(cf_times[i]);
+ cfi = NUM2DBL(cfs[i]);
+ dcfi = cfi * exp(-r * cfti);
+
+ S += dcfi;
+ D1 += cfti * dcfi;
+ }
+
+ return rb_float_new(D1 / S);
+}
+
+void init_bond()
+{
+ VALUE klass, singleton;
+
+#if 0
+ value module = rb_define_module("rupee");
+#endif
+
+ klass = rb_define_class_under(module, "Bond", rb_cObject);
+ singleton = rb_singleton_class(klass);
+
+ rb_define_singleton_method(klass, "duration", duration, 3);
+}
@@ -43,13 +43,12 @@ static VALUE rupee_cnd(VALUE self, VALUE rz)
return rb_float_new(cnd(NUM2DBL(rz)));
}
-/* Maybe this does something */
void init_distribution()
{
VALUE klass, singleton;
#if 0
- VALUE module = rb_define_module("Rupee");
+ value module = rb_define_module("rupee");
#endif
klass = rb_define_class_under(module, "Distribution", rb_cObject);
View
@@ -8,4 +8,5 @@ void Init_rupee(void)
init_distribution();
init_option();
+ init_bond();
}
View
@@ -17,4 +17,7 @@ double gbs(const char *call_put_flag, double S, double X, double T, double r,
double b, double v);
void init_option();
+/* Bonds */
+void init_bond();
+
#endif
@@ -1,4 +1,4 @@
module Rupee
# The current version
- VERSION = "0.0.9"
+ VERSION = "0.0.11"
end

0 comments on commit 6bf27f9

Please sign in to comment.