alglib 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  #include "alglib_util.h"
2
-
2
+ #include <iostream>
3
3
 
4
4
  VALUE real1d_to_array(ap::real_1d_array *x) {
5
5
  VALUE ar=rb_ary_new2(x->gethighbound()-x->getlowbound());
@@ -8,3 +8,12 @@ VALUE real1d_to_array(ap::real_1d_array *x) {
8
8
  }
9
9
  return ar;
10
10
  }
11
+
12
+ void array_to_real1d(VALUE ary, ap::real_1d_array *x) {
13
+ x->setlength(RARRAY_LEN(ary));
14
+ //std::cout << "Largo:" << RARRAY_LEN(ary) << "\n";
15
+ for(long i=0;i<RARRAY_LEN(ary);i++) {
16
+ // std::cout << "i:" << i << "=" << rb_num2dbl(RARRAY_PTR(ary)[i]) <<"\n";
17
+ x->operator()(i)=rb_num2dbl(RARRAY_PTR(ary)[i]);
18
+ }
19
+ }
@@ -4,9 +4,11 @@
4
4
 
5
5
  #include "ap.h"
6
6
  #include "linreg.h"
7
+ #include "logit.h"
8
+
7
9
  #include "ruby.h"
8
10
 
9
11
  VALUE real1d_to_array(ap::real_1d_array *x);
10
-
12
+ void array_to_real1d(VALUE ary, ap::real_1d_array *x);
11
13
 
12
14
  #endif
@@ -0,0 +1,97 @@
1
+ %typemap(in) ap::real_1d_array {
2
+ $1 = new ap::real_1d_array();
3
+ $1->setlength(RARRAY_LEN($input));
4
+ for(long i=0;i<RARRAY_LEN($input);i++) {
5
+ $1->operator()(i)=rb_num2dbl(RARRAY_PTR($input)[i]);
6
+ }
7
+ }
8
+
9
+
10
+ %apply ap::real_1d_array {ap::real_1d_array const &};
11
+
12
+ namespace ap {
13
+ /********************************************************************
14
+ Template of a dynamical one-dimensional array
15
+ ********************************************************************/
16
+ template<class T, bool Aligned = false>
17
+ class template_1d_array
18
+ {
19
+ public:
20
+ template_1d_array();
21
+ ~template_1d_array();
22
+ template_1d_array(const template_1d_array &rhs);
23
+ %extend {
24
+ T __getitem__(int i) {
25
+ return $self->operator()(i);
26
+ };
27
+ T __setitem__(int i, T v) {
28
+ return $self->operator()(i)=v;
29
+ };
30
+ int size() {
31
+ return $self->gethighbound()-$self->getlowbound()+1;
32
+ }
33
+ }
34
+
35
+ void setbounds( int iLow, int iHigh );
36
+ void setlength(int iLen);
37
+ void setcontent( int iLow, int iHigh, const T *pContent );
38
+ T* getcontent();
39
+ int getlowbound(int iBoundNum = 0);
40
+ int gethighbound(int iBoundNum = 0);
41
+ };
42
+
43
+ template<class T, bool Aligned = false>
44
+ class template_2d_array
45
+ {
46
+ public:
47
+ template_2d_array();
48
+
49
+ ~template_2d_array();
50
+
51
+ template_2d_array(const template_2d_array &rhs);
52
+ T& operator()(int i1, int i2);
53
+
54
+ void setbounds( int iLow1, int iHigh1, int iLow2, int iHigh2 );
55
+
56
+ void setlength(int iLen1, int iLen2);
57
+
58
+ void setcontent( int iLow1, int iHigh1, int iLow2, int iHigh2, const T *pContent );
59
+
60
+ int getlowbound(int iBoundNum) const;
61
+
62
+ int gethighbound(int iBoundNum) const;
63
+
64
+ %extend {
65
+ double __getitem__(int x,int y) {
66
+ return $self->operator()(x,y);
67
+ }
68
+ void __setitem__(int x,int y, T val) {
69
+ $self->operator()(x,y)=val;
70
+ }
71
+
72
+ }
73
+
74
+ };
75
+
76
+
77
+
78
+ typedef template_1d_array<int> integer_1d_array;
79
+ typedef template_1d_array<double,true> real_1d_array;
80
+ typedef template_1d_array<complex> complex_1d_array;
81
+ typedef template_1d_array<bool> boolean_1d_array;
82
+
83
+ typedef template_2d_array<int> integer_2d_array;
84
+ typedef template_2d_array<double,true> real_2d_array;
85
+ typedef template_2d_array<complex> complex_2d_array;
86
+ typedef template_2d_array<bool> boolean_2d_array;
87
+
88
+ %template(Integer1dArray) template_1d_array<int>;
89
+ %template(Real1dArray) template_1d_array<double,true>;
90
+ %template(Boolean1dArray) template_1d_array<bool>;
91
+ %template(Integer2dArray) template_2d_array<int>;
92
+ %template(Real2dArray) template_2d_array<double,true>;
93
+ };
94
+
95
+
96
+ VALUE real1d_to_array(ap::real_1d_array *x);
97
+ void array_to_real1d(VALUE ary, ap::real_1d_array *x);
@@ -0,0 +1,24 @@
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
+
@@ -1,4 +1,5 @@
1
1
  require 'mkmf'
2
+ $CFLAGS+=" -DEBUG "
2
3
  $libs = append_library($libs, "supc++")
3
4
  $libs = append_library($libs, "stdc++")
4
5
  create_makefile("alglib_ext","alglib")
@@ -0,0 +1,89 @@
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);
@@ -1,3 +1,5 @@
1
+ $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../ext"))
2
+ $:.unshift(File.expand_path(File.dirname(__FILE__)+"/../lib"))
1
3
  require 'alglib_ext'
2
4
  require 'matrix'
3
5
  class Matrix
@@ -12,67 +14,32 @@ class Matrix
12
14
  a_matrix
13
15
  end
14
16
  end
15
- module Alglib
16
- VERSION = '1.0.0'
17
- class LinearRegression
18
- attr_reader :model, :cases, :ivars
19
- # Creates a Linear Regression object based on a Matrix
20
- # EXAMPLE:
21
- # require 'alglib'
22
- # matrix=Matrix[[2,3,4],[2,5,5],[1,5,3],[4,6,5]]
23
- # lr=Alglib::LinearRegression.build_from_matrix(matrix)
24
- # => #<Alglib::LinearRegression:0x7ffaf6c05dc0 @model=#<Alglib_ext::LinearModel:0x7ffaf6c05e60>, @cases=4, @report=#<Alglib_ext::LrReport:0x7ffaf6c05e10>, @ivars=2>
25
- # lr.coeffs
26
- # => [0.585714285714286, -0.0142857142857142]
17
+ require 'alglib/linearregression.rb'
18
+ require 'alglib/logit.rb'
19
+ require 'alglib/correlation.rb'
27
20
 
28
- def self.build_from_matrix(matrix)
29
- raise "Argument should be a matrix" unless matrix.is_a? Matrix
30
- cases=matrix.row_size
31
- ivars=matrix.column_size-1
32
- lm=Alglib_ext::LinearModel.new
33
- lr=Alglib_ext::LrReport.new
34
- am=matrix.to_alglib_matrix
35
- Alglib_ext::lrbuild(am,cases,ivars,lm,lr)
36
- self.new(lm,lr,cases,ivars)
37
- end
38
- # Use with care...
39
- def initialize(lm,lr,cases,ivars)
40
- @model=lm
41
- @report=lr
42
- @cases=cases
43
- @ivars=ivars
44
- end
45
- # Constant value. a on
46
- # y= a+b1x1+b2x2...
47
-
48
- def constant
49
- v=Alglib_ext::Real1dArray.new
50
- Alglib_ext::lrunpack(@model,v, @ivars);
51
- Alglib_ext.real1d_to_array(v).slice(-1)
52
- end
53
- # Array with b coeffs.
54
- def coeffs
55
- v=Alglib_ext::Real1dArray.new
56
- Alglib_ext::lrunpack(@model,v, @ivars);
57
- Alglib_ext.real1d_to_array(v)[0,v.size]
58
- end
59
- # Predict an y value based on an array of predictors.
60
- def process(vector)
61
- Alglib_ext::lrprocess(@model,vector);
62
- end
63
- # A wrapper for lm_report
64
- def report
65
- {:c=>@report.c,
66
- :rmserror=>@report.rmserror,
67
- :avgerror=>@report.avgerror,
68
- :avgrelerror=>@report.avgrelerror,
69
- :cvrmserror=>@report.cvrmserror,
70
- :cvavgerror=>@report.cvavgerror,
71
- :cvavgrelerror=>@report.cvavgrelerror,
72
- :ncvdefects=>@report.ncvdefects,
73
- :cvdefects=>@report.cvdefects
21
+ module Alglib
22
+ VERSION = '1.0.1'
23
+
24
+ def self.array_to_real1darray(a)
25
+ v=Alglib_ext::Real1dArray.new
26
+ Alglib_ext.array_to_real1d(a,v)
27
+ v
28
+ end
29
+
30
+ def self.real2darray_to_array(a)
31
+ lr=a.getlowbound(1)
32
+ lc=a.getlowbound(2)
33
+ hr=a.gethighbound(1)
34
+ hc=a.gethighbound(2)
35
+ out=[]
36
+ (lr..hr).each{|x|
37
+ row=[]
38
+ (lc..hc).each{|y|
39
+ row[y]=a[x,y]
74
40
  }
75
- end
76
-
41
+ out[x]=row
42
+ }
43
+ out
77
44
  end
78
45
  end
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,63 @@
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
@@ -0,0 +1,42 @@
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