gslr 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ff1543c6434c221e210a9269d673c2b84632839f80e0ec0db047d30cc9a4b60
4
- data.tar.gz: 10967bdbb5ab82e61b90f3f528d6d947b854f3993fbb4ee5bc0c11d25e71b63c
3
+ metadata.gz: 659075d2214e0e64dd7689431a8b38492ff758b8369171d970c3d31ba9fe5ba9
4
+ data.tar.gz: e47f6cc7bedceb9f811444f38aa0bc0bc5cc9b8fbaeb0c9f20e853b03fa041f9
5
5
  SHA512:
6
- metadata.gz: b48d6c388ff6e93a1e0c2aa4c7f9406ab96a3e77ee4852dc24bd736e71cc864fa8f6eee1d9da8a8778a2653d624da860ef69fe32334915ff5cfef8d4759a1920
7
- data.tar.gz: 04b1cdb82277e30e4abfc10004605479bf6a0064cd29122c279221ff3d62483486bdac4c29b30e488fbf315ab0d5c59b612625db80ffc9dd5cf7aa89c99b419f
6
+ metadata.gz: 35c109f3a1c866d29722e89ae7ee12350fad09599daf1b717baabeb58caf2541a81524174ae023ba72836d868770b2cfcaf409483fa81c60e7ed63ad083cfb43
7
+ data.tar.gz: 7afa7abd2f7217e133de5b8a8baa6e6b7093072eb89bd30eb257f4db73990d4fdacd251de3d89c44969fbf20de61aab081fc989faf977d94c403ad563e4313ff
@@ -1,3 +1,7 @@
1
+ ## 0.1.2 (2019-12-08)
2
+
3
+ - Added `covariance` and `chi2` methods
4
+
1
5
  ## 0.1.1 (2019-12-07)
2
6
 
3
7
  - Fixed `undefined symbol` error on Linux
data/README.md CHANGED
@@ -49,6 +49,13 @@ model.coefficients
49
49
  model.intercept
50
50
  ```
51
51
 
52
+ Get the covariance matrix and chi-squared
53
+
54
+ ```ruby
55
+ model.covariance
56
+ model.chi2
57
+ ```
58
+
52
59
  Pass weights
53
60
 
54
61
  ```ruby
@@ -97,7 +104,7 @@ Check out [the options](https://www.gnu.org/software/gsl/extras/native_win_build
97
104
  sudo apt-get install libgsl-dev
98
105
  ```
99
106
 
100
- ## Heroku
107
+ ### Heroku
101
108
 
102
109
  Use the [Apt buildpack](https://github.com/heroku/heroku-buildpack-apt) and create an `Aptfile` with:
103
110
 
@@ -105,7 +112,7 @@ Use the [Apt buildpack](https://github.com/heroku/heroku-buildpack-apt) and crea
105
112
  libgsl-dev
106
113
  ```
107
114
 
108
- ## Travis CI
115
+ ### Travis CI
109
116
 
110
117
  Add to `.travis.yml`:
111
118
 
@@ -24,6 +24,9 @@ module GSLR
24
24
  raise LoadError, "Could not find GSL"
25
25
  end
26
26
 
27
+ # https://www.gnu.org/software/gsl/doc/html/err.html
28
+ extern "char * gsl_strerror(int gsl_errno)"
29
+
27
30
  # https://www.gnu.org/software/gsl/doc/html/vectors.html
28
31
  extern "gsl_vector * gsl_vector_alloc(size_t n)"
29
32
  extern "void gsl_vector_free(gsl_vector * v)"
@@ -73,5 +73,9 @@ module GSLR
73
73
  def dfloat(x)
74
74
  x.is_a?(Numo::DFloat) ? x : x.cast_to(Numo::DFloat)
75
75
  end
76
+
77
+ def check_status(status)
78
+ raise Error, FFI.gsl_strerror(status).to_s if status != 0
79
+ end
76
80
  end
77
81
  end
@@ -1,5 +1,7 @@
1
1
  module GSLR
2
2
  class OLS < Model
3
+ attr_reader :covariance, :chi2
4
+
3
5
  def fit(x, y, weight: nil)
4
6
  # set data
5
7
  xc, s1, s2 = set_matrix(x, intercept: @fit_intercept)
@@ -14,15 +16,17 @@ module GSLR
14
16
  # fit
15
17
  if weight
16
18
  wc = set_vector(weight)
17
- FFI.gsl_multifit_wlinear(xc, wc, yc, c, cov, chisq.ref, work)
19
+ check_status FFI.gsl_multifit_wlinear(xc, wc, yc, c, cov, chisq, work)
18
20
  else
19
- FFI.gsl_multifit_linear(xc, yc, c, cov, chisq.ref, work)
21
+ check_status FFI.gsl_multifit_linear(xc, yc, c, cov, chisq, work)
20
22
  end
21
23
 
22
24
  # read solution
23
25
  c_ptr = FFI.gsl_vector_ptr(c, 0)
24
26
  @coefficients = c_ptr[0, s2 * Fiddle::SIZEOF_DOUBLE].unpack("d*")
25
27
  @intercept = @fit_intercept ? @coefficients.shift : 0.0
28
+ @covariance = read_matrix(cov, s2)
29
+ @chi2 = chisq[0, Fiddle::SIZEOF_DOUBLE].unpack1("d")
26
30
 
27
31
  nil
28
32
  ensure
@@ -33,5 +37,15 @@ module GSLR
33
37
  FFI.gsl_matrix_free(cov) if cov
34
38
  FFI.gsl_multifit_linear_free(work) if work
35
39
  end
40
+
41
+ private
42
+
43
+ def read_matrix(cov, s2)
44
+ ptr = FFI.gsl_matrix_ptr(cov, 0, 0)
45
+ row_size = s2 * Fiddle::SIZEOF_DOUBLE
46
+ s2.times.map do |i|
47
+ ptr[i * row_size, row_size].unpack("d*")
48
+ end
49
+ end
36
50
  end
37
51
  end
@@ -24,8 +24,8 @@ module GSLR
24
24
  work = FFI.gsl_multifit_linear_alloc(s1, s2)
25
25
 
26
26
  # fit
27
- FFI.gsl_multifit_linear_svd(xc, work)
28
- FFI.gsl_multifit_linear_solve(Math.sqrt(@alpha), xc, yc, c, rnorm.ref, snorm.ref, work)
27
+ check_status FFI.gsl_multifit_linear_svd(xc, work)
28
+ check_status FFI.gsl_multifit_linear_solve(Math.sqrt(@alpha), xc, yc, c, rnorm, snorm, work)
29
29
 
30
30
  # read solution
31
31
  c_ptr = FFI.gsl_vector_ptr(c, 0)
@@ -83,7 +83,7 @@ module GSLR
83
83
  end
84
84
 
85
85
  yc = set_vector(y)
86
- FFI.gsl_vector_add_constant(yc, -y_offset)
86
+ check_status FFI.gsl_vector_add_constant(yc, -y_offset)
87
87
 
88
88
  [yc, y_offset]
89
89
  end
@@ -1,3 +1,3 @@
1
1
  module GSLR
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gslr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-08 00:00:00.000000000 Z
11
+ date: 2019-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler