class GSLR::Model

Attributes

coefficients[R]
intercept[R]

Public Class Methods

new(intercept: true) click to toggle source
# File lib/gslr/model.rb, line 5
def initialize(intercept: true)
  @fit_intercept = intercept
end

Public Instance Methods

predict(x) click to toggle source
# File lib/gslr/model.rb, line 9
def predict(x)
  if numo?(x)
    x.dot(@coefficients) + @intercept
  else
    x.map do |xi|
      xi.zip(@coefficients).map { |xii, c| xii * c }.sum + @intercept
    end
  end
end

Private Instance Methods

check_status(status) click to toggle source
# File lib/gslr/model.rb, line 77
def check_status(status)
  raise Error, FFI.gsl_strerror(status).to_s if status != 0
end
dfloat(x) click to toggle source
# File lib/gslr/model.rb, line 73
def dfloat(x)
  x.is_a?(Numo::DFloat) ? x : x.cast_to(Numo::DFloat)
end
numo?(x) click to toggle source
# File lib/gslr/model.rb, line 69
def numo?(x)
  defined?(Numo::NArray) && x.is_a?(Numo::NArray)
end
set_data(ptr, x) click to toggle source
# File lib/gslr/model.rb, line 55
def set_data(ptr, x)
  if numo?(x)
    x = dfloat(x)
    ptr[0, x.byte_size] = x.to_string
  else
    str = x.pack("d*")
    ptr[0, str.bytesize] = str
  end
end
set_matrix(x, intercept:) click to toggle source
# File lib/gslr/model.rb, line 21
def set_matrix(x, intercept:)
  s1, s2 = shape(x)
  s2 += 1 if intercept

  xc = FFI.gsl_matrix_alloc(s1, s2)
  x_ptr = FFI.gsl_matrix_ptr(xc, 0, 0)

  if numo?(x)
    if intercept
      ones = Numo::DFloat.ones(s1, 1)
      x = ones.concatenate(x, axis: 1)
    end
    set_data(x_ptr, x)
  else
    # pack efficiently
    str = String.new
    one = [1].pack("d*")
    x.each do |xi|
      str << one if intercept
      xi.pack("d*", buffer: str)
    end
    x_ptr[0, str.bytesize] = str
  end

  [xc, s1, s2]
end
set_vector(x) click to toggle source
# File lib/gslr/model.rb, line 48
def set_vector(x)
  v = FFI.gsl_vector_alloc(x.size)
  ptr = FFI.gsl_vector_ptr(v, 0)
  set_data(ptr, x)
  v
end
shape(x) click to toggle source
# File lib/gslr/model.rb, line 65
def shape(x)
  numo?(x) ? x.shape : [x.size, x.first.size]
end