alglib 0.1.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -4
- data/Manifest.txt +0 -10
- data/README.txt +1 -1
- data/Rakefile +12 -23
- data/ext/Rakefile +10 -11
- data/ext/alglib.i +151 -16
- data/ext/alglib/alglib.cpp +164 -2664
- data/ext/alglib/alglib_util.cpp +1 -9
- data/ext/alglib/alglib_util.h +1 -3
- data/ext/alglib/ap.h +1 -1
- data/ext/extconf.rb +1 -3
- data/lib/alglib.rb +60 -53
- data/test/test_alglib.rb +10 -46
- metadata +10 -64
- data/ext/ap.i +0 -97
- data/ext/correlation.i +0 -24
- data/ext/logit.i +0 -89
- data/lib/alglib/correlation.rb +0 -26
- data/lib/alglib/linearregression.rb +0 -63
- data/lib/alglib/logit.rb +0 -42
- data/test/test_correlation.rb +0 -44
- data/test/test_correlationtest.rb +0 -45
- data/test/test_linreg.rb +0 -19
- data/test/test_logit.rb +0 -43
- data/test/test_pca.rb +0 -27
data/ext/correlation.i
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
%{
|
2
|
-
#include "correlation.h"
|
3
|
-
#include "ialglib.h"
|
4
|
-
%}
|
5
|
-
|
6
|
-
// Complete wrapping and testing
|
7
|
-
|
8
|
-
double pearsoncorrelation(const ap::real_1d_array& x,
|
9
|
-
const ap::real_1d_array& y,
|
10
|
-
int n);
|
11
|
-
|
12
|
-
%rename(spearmanrankcorrelation) wrap_spearmanrankcorrelation;
|
13
|
-
%inline %{
|
14
|
-
double wrap_spearmanrankcorrelation(const ap::real_1d_array& x,
|
15
|
-
const ap::real_1d_array& y,
|
16
|
-
int n) {
|
17
|
-
ap::real_1d_array x1,y1;
|
18
|
-
x1=x;
|
19
|
-
y1=y;
|
20
|
-
return spearmanrankcorrelation(x1,y1,n);
|
21
|
-
|
22
|
-
}
|
23
|
-
%}
|
24
|
-
|
data/ext/logit.i
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
%{
|
2
|
-
#include "logit.h"
|
3
|
-
%}
|
4
|
-
/**
|
5
|
-
* Logit
|
6
|
-
*/
|
7
|
-
|
8
|
-
%rename (LogitModel) logitmodel;
|
9
|
-
%rename (LogitMcState) logitmcstate;
|
10
|
-
%rename (MNLReport) mnlreport;
|
11
|
-
struct logitmodel
|
12
|
-
{
|
13
|
-
ap::real_1d_array w;
|
14
|
-
};
|
15
|
-
struct logitmcstate
|
16
|
-
{
|
17
|
-
bool brackt;
|
18
|
-
bool stage1;
|
19
|
-
int infoc;
|
20
|
-
double dg;
|
21
|
-
double dgm;
|
22
|
-
double dginit;
|
23
|
-
double dgtest;
|
24
|
-
double dgx;
|
25
|
-
double dgxm;
|
26
|
-
double dgy;
|
27
|
-
double dgym;
|
28
|
-
double finit;
|
29
|
-
double ftest1;
|
30
|
-
double fm;
|
31
|
-
double fx;
|
32
|
-
double fxm;
|
33
|
-
double fy;
|
34
|
-
double fym;
|
35
|
-
double stx;
|
36
|
-
double sty;
|
37
|
-
double stmin;
|
38
|
-
double stmax;
|
39
|
-
double width;
|
40
|
-
double width1;
|
41
|
-
double xtrapf;
|
42
|
-
};
|
43
|
-
struct mnlreport
|
44
|
-
{
|
45
|
-
int ngrad;
|
46
|
-
int nhess;
|
47
|
-
};
|
48
|
-
|
49
|
-
|
50
|
-
void mnltrainh(const ap::real_2d_array& xy,
|
51
|
-
int npoints,
|
52
|
-
int nvars,
|
53
|
-
int nclasses,
|
54
|
-
int& info,
|
55
|
-
logitmodel& lm,
|
56
|
-
mnlreport& rep);
|
57
|
-
|
58
|
-
|
59
|
-
%rename(mnlprocess) wrap_mnlprocess;
|
60
|
-
%inline %{
|
61
|
-
VALUE wrap_mnlprocess(logitmodel& lm,
|
62
|
-
const ap::real_1d_array& x) {
|
63
|
-
ap::real_1d_array y;
|
64
|
-
// y.setlength(lm.w(3));
|
65
|
-
mnlprocess(lm,x,y);
|
66
|
-
// std::cout << "Saliendo";
|
67
|
-
return real1d_to_array(&y);
|
68
|
-
}
|
69
|
-
%}
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
void mnlunpack(const logitmodel& lm,
|
74
|
-
ap::real_2d_array& a,
|
75
|
-
int& nvars,
|
76
|
-
int& nclasses);
|
77
|
-
|
78
|
-
void mnlpack(const ap::real_2d_array& a,
|
79
|
-
int nvars,
|
80
|
-
int nclasses,
|
81
|
-
logitmodel& lm);
|
82
|
-
|
83
|
-
|
84
|
-
void mnlserialize(const logitmodel& lm, ap::real_1d_array& ra, int& rlen);
|
85
|
-
|
86
|
-
void mnlcopy(const logitmodel& lm1, logitmodel& lm2);
|
87
|
-
|
88
|
-
void mnlunserialize(const ap::real_1d_array& ra, logitmodel& lm);
|
89
|
-
double mnlavgce(logitmodel& lm, const ap::real_2d_array& xy, int npoints);
|
data/lib/alglib/correlation.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module Alglib
|
2
|
-
# Retrieves Pearson correlation for two arrays
|
3
|
-
def self.pearson_correlation(a,b)
|
4
|
-
raise ArgumentError "argument should be the same size" if a.size!=b.size
|
5
|
-
Alglib_ext.pearsoncorrelation(a,b,a.size)
|
6
|
-
end
|
7
|
-
# Retrieves Spearman ranked correlation for two arrays
|
8
|
-
# Equal to Pearson correlation for ranked data
|
9
|
-
|
10
|
-
def self.spearman_correlation(a,b)
|
11
|
-
raise ArgumentError "argument should be the same size" if a.size!=b.size
|
12
|
-
Alglib_ext.spearmanrankcorrelation(a,b,a.size)
|
13
|
-
end
|
14
|
-
# Retrieves significance for pearson correlation for one or two tails
|
15
|
-
# Returns a hash with keys :both, :left and :right
|
16
|
-
def self.pearson_correlation_significance(r,size)
|
17
|
-
out={}
|
18
|
-
out[:both], out[:left], out[:right] = Alglib_ext.pearsoncorrelationsignificance(r, size)
|
19
|
-
out
|
20
|
-
end
|
21
|
-
def self.spearman_correlation_significance(r,size)
|
22
|
-
out={}
|
23
|
-
out[:both], out[:left], out[:right] = Alglib_ext.spearmanrankcorrelationsignificance(r, size)
|
24
|
-
out
|
25
|
-
end
|
26
|
-
end
|
@@ -1,63 +0,0 @@
|
|
1
|
-
module Alglib
|
2
|
-
class LinearRegression
|
3
|
-
attr_reader :model, :cases, :ivars
|
4
|
-
# Creates a Linear Regression object based on a Matrix
|
5
|
-
# EXAMPLE:
|
6
|
-
# require 'alglib'
|
7
|
-
# matrix=Matrix[[2,3,4],[2,5,5],[1,5,3],[4,6,5]]
|
8
|
-
# lr=Alglib::LinearRegression.build_from_matrix(matrix)
|
9
|
-
# => #<Alglib::LinearRegression:0x7ffaf6c05dc0 @model=#<Alglib_ext::LinearModel:0x7ffaf6c05e60>, @cases=4, @report=#<Alglib_ext::LrReport:0x7ffaf6c05e10>, @ivars=2>
|
10
|
-
# lr.coeffs
|
11
|
-
# => [0.585714285714286, -0.0142857142857142]
|
12
|
-
|
13
|
-
def self.build_from_matrix(matrix)
|
14
|
-
raise "Argument should be a matrix" unless matrix.is_a? Matrix
|
15
|
-
cases=matrix.row_size
|
16
|
-
ivars=matrix.column_size-1
|
17
|
-
lm=Alglib_ext::LinearModel.new
|
18
|
-
lr=Alglib_ext::LrReport.new
|
19
|
-
am=matrix.to_alglib_matrix
|
20
|
-
Alglib_ext::lrbuild(am,cases,ivars,lm,lr)
|
21
|
-
self.new(lm,lr,cases,ivars)
|
22
|
-
end
|
23
|
-
# Use with care...
|
24
|
-
def initialize(lm,lr,cases,ivars)
|
25
|
-
@model=lm
|
26
|
-
@report=lr
|
27
|
-
@cases=cases
|
28
|
-
@ivars=ivars
|
29
|
-
end
|
30
|
-
# Constant value. a on
|
31
|
-
# y= a+b1x1+b2x2...
|
32
|
-
|
33
|
-
def constant
|
34
|
-
v=Alglib_ext::Real1dArray.new
|
35
|
-
Alglib_ext::lrunpack(@model,v, @ivars);
|
36
|
-
Alglib_ext.real1d_to_array(v).slice(-1)
|
37
|
-
end
|
38
|
-
# Array with b coeffs.
|
39
|
-
def coeffs
|
40
|
-
v=Alglib_ext::Real1dArray.new
|
41
|
-
Alglib_ext::lrunpack(@model,v, @ivars);
|
42
|
-
Alglib_ext.real1d_to_array(v)[0,v.size]
|
43
|
-
end
|
44
|
-
# Predict an y value based on an array of predictors.
|
45
|
-
def process(vector)
|
46
|
-
Alglib_ext::lrprocess(@model,vector);
|
47
|
-
end
|
48
|
-
# A wrapper for lm_report
|
49
|
-
def report
|
50
|
-
{:c=>@report.c,
|
51
|
-
:rmserror=>@report.rmserror,
|
52
|
-
:avgerror=>@report.avgerror,
|
53
|
-
:avgrelerror=>@report.avgrelerror,
|
54
|
-
:cvrmserror=>@report.cvrmserror,
|
55
|
-
:cvavgerror=>@report.cvavgerror,
|
56
|
-
:cvavgrelerror=>@report.cvavgrelerror,
|
57
|
-
:ncvdefects=>@report.ncvdefects,
|
58
|
-
:cvdefects=>@report.cvdefects
|
59
|
-
}
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
data/lib/alglib/logit.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
module Alglib
|
2
|
-
class Logit
|
3
|
-
attr_reader :model, :cases, :ivars
|
4
|
-
# Creates a Logit object based on a Matrix
|
5
|
-
# EXAMPLE:
|
6
|
-
# require 'alglib'
|
7
|
-
# matrix=Matrix[[2,3,4],[2,5,5],[1,5,3],[4,6,5]]
|
8
|
-
# lr=Alglib::LinearRegression.build_from_matrix(matrix)
|
9
|
-
# => #<Alglib::LinearRegression:0x7ffaf6c05dc0 @model=#<Alglib_ext::LinearModel:0x7ffaf6c05e60>, @cases=4, @report=#<Alglib_ext::LrReport:0x7ffaf6c05e10>, @ivars=2>
|
10
|
-
# lr.coeffs
|
11
|
-
# => [0.585714285714286, -0.0142857142857142]
|
12
|
-
|
13
|
-
def self.build_from_matrix(matrix)
|
14
|
-
raise "Argument should be a matrix" unless matrix.is_a? Matrix
|
15
|
-
cases=matrix.row_size
|
16
|
-
ivars=matrix.column_size-1
|
17
|
-
lm=Alglib_ext::LogitModel.new
|
18
|
-
lr=Alglib_ext::MNLReport.new
|
19
|
-
nclasses=matrix.column(matrix.column_size-1).to_a.uniq.size
|
20
|
-
am=matrix.to_alglib_matrix
|
21
|
-
Alglib_ext::mnltrainh(am,cases,ivars,nclasses,lm,lr)
|
22
|
-
self.new(lm,lr,cases,ivars,nclasses)
|
23
|
-
end
|
24
|
-
# Use with care...
|
25
|
-
def initialize(lm,lr,cases,ivars,nclasses)
|
26
|
-
@model=lm
|
27
|
-
@report=lr
|
28
|
-
@cases=cases
|
29
|
-
@ivars=ivars
|
30
|
-
@nclasses=nclasses
|
31
|
-
end
|
32
|
-
def process(vector)
|
33
|
-
Alglib_ext.mnlprocess(@model,vector)
|
34
|
-
end
|
35
|
-
def unpack
|
36
|
-
a2d=Alglib_ext::Real2dArray.new
|
37
|
-
Alglib_ext::mnlunpack(@model, a2d,@ivars,@nclasses);
|
38
|
-
Alglib.real2darray_to_array(a2d)
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
data/test/test_correlation.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
|
2
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
|
3
|
-
|
4
|
-
require "test/unit"
|
5
|
-
begin
|
6
|
-
require 'statsample'
|
7
|
-
HAS_SS=true
|
8
|
-
rescue
|
9
|
-
HAS_SS=false
|
10
|
-
end
|
11
|
-
|
12
|
-
if(HAS_GSL.nil?)
|
13
|
-
begin
|
14
|
-
require 'rbgsl'
|
15
|
-
HAS_GSL=true
|
16
|
-
rescue
|
17
|
-
HAS_GSL=false
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
require "alglib"
|
22
|
-
require 'matrix'
|
23
|
-
class TestAlglibCorrelation < Test::Unit::TestCase
|
24
|
-
def test_correlation
|
25
|
-
a=[1,2,3,6,7,9,0,10]
|
26
|
-
b=[2,5, 7,8,4,8, 0, 5]
|
27
|
-
|
28
|
-
if(HAS_GSL)
|
29
|
-
a_gsl=GSL::Vector.alloc(a)
|
30
|
-
b_gsl=GSL::Vector.alloc(b)
|
31
|
-
corr_expected=GSL::Stats::correlation(a_gsl, b_gsl)
|
32
|
-
corr_alglib=Alglib.pearson_correlation(a,b)
|
33
|
-
assert_equal(corr_expected,corr_alglib)
|
34
|
-
end
|
35
|
-
if(HAS_SS)
|
36
|
-
va=a.to_vector(:scale)
|
37
|
-
vb=b.to_vector(:scale)
|
38
|
-
corr_expected=Statsample::Bivariate::spearman(va, vb)
|
39
|
-
corr_alglib=Alglib.spearman_correlation(a,b)
|
40
|
-
assert_in_delta(corr_expected,corr_alglib,0.001)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
|
2
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
|
3
|
-
|
4
|
-
require "test/unit"
|
5
|
-
begin
|
6
|
-
require 'statsample'
|
7
|
-
HAS_SS=true
|
8
|
-
rescue
|
9
|
-
HAS_SS=false
|
10
|
-
end
|
11
|
-
|
12
|
-
if(HAS_GSL.nil?)
|
13
|
-
begin
|
14
|
-
require 'rbgsl'
|
15
|
-
HAS_GSL=true
|
16
|
-
rescue
|
17
|
-
HAS_GSL=false
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
require "alglib"
|
22
|
-
require 'matrix'
|
23
|
-
class TestAlglibCorrelationTests < Test::Unit::TestCase
|
24
|
-
def test_pearson
|
25
|
-
a=[2,3,4,5,6,7,8,9 ,10,22,20]
|
26
|
-
b=[1,4,3,5,6,8,9,10,10,20,30]
|
27
|
-
r=Alglib.pearson_correlation(a,b)
|
28
|
-
t= Statsample::Bivariate.t_r(r, a.size)
|
29
|
-
out = Alglib.pearson_correlation_significance(r, a.size)
|
30
|
-
assert_in_delta(out[:both], Statsample::Bivariate.prop_pearson(t,a.size), 0.0001)
|
31
|
-
assert_in_delta(out[:left], Statsample::Bivariate.prop_pearson(t, a.size,:left), 0.0001)
|
32
|
-
assert_in_delta(out[:right], Statsample::Bivariate.prop_pearson(t,a.size, :right), 0.0001)
|
33
|
-
end
|
34
|
-
def test_spearman
|
35
|
-
a=[2,3,4,5,6,7,8,9 ,10,22,20]
|
36
|
-
b=[1,4,3,5,6,8,9,10,10,20,30]
|
37
|
-
r=Alglib.spearman_correlation(a,b)
|
38
|
-
t= Statsample::Bivariate.t_r(r, a.size)
|
39
|
-
out = Alglib.spearman_correlation_significance(r, a.size)
|
40
|
-
|
41
|
-
assert_in_delta(out[:both], Statsample::Bivariate.prop_pearson(t,a.size), 0.0001)
|
42
|
-
assert_in_delta(out[:left], Statsample::Bivariate.prop_pearson(t, a.size,:left), 0.0001)
|
43
|
-
assert_in_delta(out[:right], Statsample::Bivariate.prop_pearson(t,a.size, :right), 0.0001)
|
44
|
-
end
|
45
|
-
end
|
data/test/test_linreg.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
|
2
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
|
3
|
-
|
4
|
-
require "test/unit"
|
5
|
-
require "alglib"
|
6
|
-
require 'matrix'
|
7
|
-
class TestAlglibLinreg < Test::Unit::TestCase
|
8
|
-
def test_linear_regression
|
9
|
-
a=[1,3,2,4,3,5,4,6,5,7]
|
10
|
-
b=[3,3,4,4,5,5,6,6,4,4]
|
11
|
-
c=[11,22,30,40,50,65,78,79,99,100]
|
12
|
-
y=[3,4,5,6,7,8,9,10,20,30]
|
13
|
-
matrix=Matrix.columns([a,b,c,y])
|
14
|
-
lr=Alglib::LinearRegression.build_from_matrix(matrix)
|
15
|
-
assert_in_delta(0.695,lr.coeffs[0],0.001)
|
16
|
-
assert_in_delta(11.027,lr.constant,0.001)
|
17
|
-
assert_in_delta(1.785,lr.process([1,3,11]),0.001)
|
18
|
-
end
|
19
|
-
end
|
data/test/test_logit.rb
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
|
2
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
|
3
|
-
|
4
|
-
require "test/unit"
|
5
|
-
begin
|
6
|
-
require 'statsample'
|
7
|
-
HAS_SS=true
|
8
|
-
rescue
|
9
|
-
HAS_SS=false
|
10
|
-
end
|
11
|
-
|
12
|
-
if(HAS_GSL.nil?)
|
13
|
-
begin
|
14
|
-
require 'rbgsl'
|
15
|
-
HAS_GSL=true
|
16
|
-
rescue
|
17
|
-
HAS_GSL=false
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
require "alglib"
|
22
|
-
require 'matrix'
|
23
|
-
class TestAlglib < Test::Unit::TestCase
|
24
|
-
def test_logit
|
25
|
-
a=[1,2,3,4,5,6,7,8,9,10]
|
26
|
-
b=[0,0,1,0,0,0,1,1,1,1]
|
27
|
-
matrix=Matrix.columns([a,b])
|
28
|
-
lr=Alglib::Logit.build_from_matrix(matrix)
|
29
|
-
end
|
30
|
-
def atest_logit_1
|
31
|
-
ds=Statsample::CSV.read(File.dirname(__FILE__)+"/sample_logit.csv")
|
32
|
-
ds['female'].recode! {|c|
|
33
|
-
c=='female' ? 1: 0
|
34
|
-
}
|
35
|
-
constant=[1]*(ds.cases)
|
36
|
-
matrix=Matrix.columns([ds['female'].data,ds['hon'].data])
|
37
|
-
lr=Alglib::Logit.build_from_matrix(matrix)
|
38
|
-
lr.unpack
|
39
|
-
matrix=Matrix.columns([ds['math'].data, ds['female'].data,ds['read'].data, ds['hon'].data])
|
40
|
-
lr=Alglib::Logit.build_from_matrix(matrix)
|
41
|
-
|
42
|
-
end
|
43
|
-
end
|
data/test/test_pca.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
|
2
|
-
$:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
|
3
|
-
require "test/unit"
|
4
|
-
require "alglib"
|
5
|
-
require 'matrix'
|
6
|
-
class TestAlglibPca < Test::Unit::TestCase
|
7
|
-
def test_pca
|
8
|
-
a=[10,20,30,40,50,60,70,80,90,100]
|
9
|
-
b=[1.2,2.3,3.4,4,5,6,7,8,9,10]
|
10
|
-
c=[6,8,3,6,8,2,3,9,10,1]
|
11
|
-
d=[61,82,31,62,86,21,33,99,101,12]
|
12
|
-
matrix=Matrix.columns([a,b,c,d])
|
13
|
-
input_2d_array=matrix.to_alglib_matrix
|
14
|
-
npoints=a.size
|
15
|
-
nvars=4
|
16
|
-
info=1
|
17
|
-
s2=Alglib_ext::Real1dArray.new
|
18
|
-
v=Alglib_ext::Real2dArray.new
|
19
|
-
a=Alglib_ext.pcabuildbasis(input_2d_array,npoints,nvars, s2,v)
|
20
|
-
comp=s2.to_a
|
21
|
-
sum=comp.inject{|a,val| a+val}
|
22
|
-
|
23
|
-
new=comp.collect{|val| (4.0*val).quo(sum)}
|
24
|
-
p new
|
25
|
-
p v.to_2d_a
|
26
|
-
end
|
27
|
-
end
|