Permalink
Browse files

Added basic statistical functions

  • Loading branch information...
1 parent c0ab3fb commit cf3bae9196041f535b0477fabd4be61518450e97 @brymck committed Sep 29, 2011
View
@@ -16,7 +16,13 @@ double simple_df(double r, double time, bool discrete);
/* Statistics */
double cnd(double);
+double corr(double *, double *, int);
+double cov(double *, double *, int);
+double mean(double *, int);
double std(double *, int);
+double sum(double *, int);
+double sum_prod(double *, double *, int);
+double var(double *, int);
void init_distribution();
/* Options */
View
@@ -32,35 +32,93 @@ cnd(z)
}
double
-mean(values, len)
- double *values;
+sum_prod(xvals, yvals, len)
+ double *xvals, *yvals;
int len;
{
double result;
int i;
+
result = 0;
-
+
for (i = 0; i < len; i++)
- result += values[i];
+ result += xvals[i] * yvals[i];
+
+ return result;
+}
+
+double
+corr(xvals, yvals, len)
+ double *xvals, *yvals;
+ int len;
+{
+ return cov(xvals, yvals, len) / std(xvals, len) / std(yvals, len);
+}
+
+double
+cov(xvals, yvals, len)
+ double *xvals, *yvals;
+ int len;
+{
+ double result, x_bar, y_bar;
+ int i;
+
+ result = 0;
+ x_bar = mean(xvals, len);
+ y_bar = mean(yvals, len);
+
+ for (i = 0; i < len; i++)
+ result += (xvals[i] - x_bar) * (yvals[i] - y_bar);
return result / len;
}
double
-std(values, len)
+var(values, len)
double *values;
int len;
{
- double bar_x, result;
+ double x_bar, result;
int i;
result = 0;
- bar_x = mean(values, len);
+ x_bar = mean(values, len);
for (i = 0; i < len; i++)
- result += pow(values[i] - bar_x, 2);
+ result += pow(values[i] - x_bar, 2);
- return sqrt(result / (len - 1));
+ return result / len;
+}
+
+double
+sum(values, len)
+ double *values;
+ int len;
+{
+ double result;
+ int i;
+ result = 0;
+
+ for (i = 0; i < len; i++)
+ result += values[i];
+
+ return result;
+}
+
+double
+mean(values, len)
+ double *values;
+ int len;
+{
+ return sum(values, len) / len;
+}
+
+double
+std(values, len)
+ double *values;
+ int len;
+{
+ return sqrt(var(values, len));
}
/* call-seq: cnd(z)
@@ -80,17 +138,79 @@ rupee_cnd(self, _z)
}
static VALUE
+rupee_corr(self, _xvals, _yvals)
+ VALUE self, _xvals, _yvals;
+{
+ int len = RARRAY_LEN(_xvals);
+ double xvals[len], yvals[len];
+
+ rtofa(xvals, _xvals, len);
+ rtofa(yvals, _yvals, len);
+
+ return rb_float_new(corr(xvals, yvals, len));
+}
+
+static VALUE
+rupee_cov(self, _xvals, _yvals)
+ VALUE self, _xvals, _yvals;
+{
+ int len = RARRAY_LEN(_xvals);
+ double xvals[len], yvals[len];
+
+ rtofa(xvals, _xvals, len);
+ rtofa(yvals, _yvals, len);
+
+ return rb_float_new(cov(xvals, yvals, len));
+}
+
+static VALUE
+rupee_mean(self, _values)
+ VALUE self, _values;
+{
+ int len = RARRAY_LEN(_values);
+ double values[len];
+
+ rtofa(values, _values, len);
+
+ return rb_float_new(mean(values, len));
+}
+
+static VALUE
rupee_std(self, _values)
VALUE self, _values;
{
int len = RARRAY_LEN(_values);
- double *values;
+ double values[len];
rtofa(values, _values, len);
return rb_float_new(std(values, len));
}
+static VALUE
+rupee_sum(self, _values)
+ VALUE self, _values;
+{
+ int len = RARRAY_LEN(_values);
+ double values[len];
+
+ rtofa(values, _values, len);
+
+ return rb_float_new(sum(values, len));
+}
+
+static VALUE
+rupee_var(self, _values)
+ VALUE self, _values;
+{
+ int len = RARRAY_LEN(_values);
+ double values[len];
+
+ rtofa(values, _values, len);
+
+ return rb_float_new(var(values, len));
+}
+
void
init_distribution()
{
@@ -103,9 +223,21 @@ init_distribution()
klass = rb_define_class_under(module, "Statistics", rb_cObject);
singleton = rb_singleton_class(klass);
+ rb_define_singleton_method(klass, "correlation", rupee_corr, 2);
+ rb_define_alias(singleton, "corr", "correlation");
+ rb_define_alias(singleton, "correl", "correlation");
+ rb_define_singleton_method(klass, "covariance", rupee_cov, 2);
+ rb_define_alias(singleton, "cov", "covariance");
+ rb_define_alias(singleton, "covar", "covariance");
rb_define_singleton_method(klass, "cumulative_normal_distribution", rupee_cnd, 1);
rb_define_alias(singleton, "cnd", "cumulative_normal_distribution");
+ rb_define_singleton_method(klass, "mean", rupee_mean, 1);
+ rb_define_alias(singleton, "average", "mean");
+ rb_define_alias(singleton, "avg", "mean");
rb_define_singleton_method(klass, "standard_deviation", rupee_std, 1);
rb_define_alias(singleton, "std", "standard_deviation");
rb_define_alias(singleton, "stdev", "standard_deviation");
+ rb_define_singleton_method(klass, "sum", rupee_sum, 1);
+ rb_define_singleton_method(klass, "variance", rupee_var, 1);
+ rb_define_alias(singleton, "var", "variance");
}
View
@@ -1,4 +1,4 @@
module Rupee
# The current version
- VERSION = "0.1.7"
+ VERSION = "0.1.8"
end
View
@@ -6,7 +6,7 @@
# bond yield to maturity = 0.09
# new bond price = 104.282
-describe Rupee::Bond do
+describe Bond do
before :each do
@tolerance = 0.001
@times = [1, 2, 3]
@@ -16,46 +16,46 @@
describe "with discrete discounting" do
before :each do
- @price = Rupee::Bond.price(@times, @cflows, @r)
+ @price = Bond.price(@times, @cflows, @r)
end
it "should produce an accurate price" do
@price.should be_within(@tolerance).of 102.531
end
it "should produce an accurate duration" do
- Rupee::Bond.duration(@times, @cflows, @r).should be_within(@tolerance).of 2.73895
+ Bond.duration(@times, @cflows, @r).should be_within(@tolerance).of 2.73895
end
it "should produce an accurate modified duration" do
- Rupee::Bond.modified(@times, @cflows, @price).should be_within(@tolerance).of 2.5128
+ Bond.modified(@times, @cflows, @price).should be_within(@tolerance).of 2.5128
end
it "should produce an accurate convexity" do
- Rupee::Bond.convexity(@times, @cflows, @r).should be_within(@tolerance).of 8.93248
+ Bond.convexity(@times, @cflows, @r).should be_within(@tolerance).of 8.93248
end
it "should produce an accurate yield to maturity" do
- Rupee::Bond.yield_to_maturity(@times, @cflows, @price).should be_within(@tolerance).of @r
+ Bond.yield_to_maturity(@times, @cflows, @price).should be_within(@tolerance).of @r
end
end
describe "with continuous discounting" do
it "should produce an accurate price" do
- Rupee::Bond.continuous_price(@times, @cflows, @r).should be_within(@tolerance).of 101.464
+ Bond.continuous_price(@times, @cflows, @r).should be_within(@tolerance).of 101.464
end
it "should produce an accurate duration" do
- Rupee::Bond.continuous_duration(@times, @cflows, @r).should be_within(@tolerance).of 2.73753
+ Bond.continuous_duration(@times, @cflows, @r).should be_within(@tolerance).of 2.73753
end
it "should produce an accurate convexity" do
- Rupee::Bond.continuous_convexity(@times, @cflows, @r).should be_within(@tolerance).of 7.86779
+ Bond.continuous_convexity(@times, @cflows, @r).should be_within(@tolerance).of 7.86779
end
it "should produce an accurate yield to maturity" do
- @price = Rupee::Bond.continuous_price(@times, @cflows, @r)
- Rupee::Bond.continuous_yield_to_maturity(@times, @cflows, @price).should be_within(@tolerance).of @r
+ @price = Bond.continuous_price(@times, @cflows, @r)
+ Bond.continuous_yield_to_maturity(@times, @cflows, @price).should be_within(@tolerance).of @r
end
end
end
View
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + "/../spec_helper"
-describe Rupee::Future do
+describe Future do
before :each do
@tolerance = 0.001
@underlying = 100
@@ -9,7 +9,7 @@
end
it "should produce an accurate price" do
- Rupee::Future.price(@underlying, @rfr, @ttm).should(
+ Future.price(@underlying, @rfr, @ttm).should(
be_within(@tolerance).of 105.127
)
end
View
@@ -1,10 +1,10 @@
require File.dirname(__FILE__) + "/../spec_helper"
-describe Rupee::Option do
+describe Option do
describe "European option valuation" do
before :each do
@tolerance = 0.0001
- @call = Rupee::Option.new(
+ @call = Option.new(
:underlying => 60,
:strike => 65,
:time => 0.25,
@@ -17,40 +17,40 @@
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::Option.black76("c", 60, 65, 0.25, 0.08, 0.3).should be_within(@tolerance).of 1.7202
+ Option.black76("c", 60, 65, 0.25, 0.08, 0.3).should be_within(@tolerance).of 1.7202
end
it "should return $6.6212 for a put" do
- Rupee::Option.black76("p", 60, 65, 0.25, 0.08, 0.3).should be_within(@tolerance).of 6.6212
+ Option.black76("p", 60, 65, 0.25, 0.08, 0.3).should be_within(@tolerance).of 6.6212
end
end
end
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::Option.generalized_black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 1.7202
- Rupee::Option.gbs("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 1.7202
+ Option.generalized_black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 1.7202
+ Option.gbs("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 1.7202
end
it "should return $6.6212 for a put" do
- Rupee::Option.generalized_black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 6.6212
- Rupee::Option.gbs("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 6.6212
+ Option.generalized_black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 6.6212
+ Option.gbs("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 6.6212
end
end
end
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::Option.black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 2.1334
- Rupee::Option.bs("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 2.1334
+ Option.black_scholes("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 2.1334
+ Option.bs("c", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 2.1334
@call.black_scholes.should be_within(@tolerance).of 2.1334
end
it "should return $6.6212 for a put" do
- Rupee::Option.black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 5.8463
- Rupee::Option.bs("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 5.8463
+ Option.black_scholes("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 5.8463
+ Option.bs("p", 60, 65, 0.25, 0.08, 0, 0.3).should be_within(@tolerance).of 5.8463
end
end
end
Oops, something went wrong.

0 comments on commit cf3bae9

Please sign in to comment.