lib/eps/linear_regression.rb in eps-0.3.1 vs lib/eps/linear_regression.rb in eps-0.3.2
- old
+ new
@@ -48,21 +48,39 @@
raise "Number of data points must be at least two more than number of features"
end
x = data.map_rows(&:to_a)
+ gsl =
+ if options.key?(:gsl)
+ options[:gsl]
+ elsif defined?(GSL)
+ true
+ elsif defined?(GSLR)
+ :gslr
+ else
+ false
+ end
+
intercept = @options.key?(:intercept) ? @options[:intercept] : true
- if intercept
+ if intercept && gsl != :gslr
data.size.times do |i|
x[i].unshift(1)
end
end
- gsl = options.key?(:gsl) ? options[:gsl] : defined?(GSL)
-
v3 =
- if gsl
+ if gsl == :gslr
+ model = GSLR::OLS.new(intercept: intercept)
+ model.fit(x, data.label, weight: data.weight)
+
+ @covariance = model.covariance
+
+ coefficients = model.coefficients.dup
+ coefficients.unshift(model.intercept) if intercept
+ coefficients
+ elsif gsl
x = GSL::Matrix.alloc(*x)
y = GSL::Vector.alloc(data.label)
w = GSL::Vector.alloc(data.weight) if data.weight
c, @covariance, _, _ = w ? GSL::MultiFit.wlinear(x, w, y) : GSL::MultiFit.linear(x, y)
c.to_a
@@ -194,10 +212,14 @@
end
end
def diagonal
@diagonal ||= begin
- if covariance.respond_to?(:each)
+ if covariance.is_a?(Array)
+ covariance.size.times.map do |i|
+ covariance[i][i]
+ end
+ elsif covariance.respond_to?(:each)
d = covariance.each(:diagonal).to_a
@removed.each do |i|
d.insert(i, 0)
end
d