Class: Bullshit::LinearRegression

Inherits:
Object
  • Object
show all
Defined in:
lib/bullshit.rb

Overview

This class computes a linear regression for the given image and domain data sets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, domain = (0...image.size).to_a) ⇒ LinearRegression

Returns a new instance of LinearRegression.



972
973
974
975
976
977
# File 'lib/bullshit.rb', line 972

def initialize(image, domain = (0...image.size).to_a)
  image.size != domain.size and raise ArgumentError,
    "image and domain have unequal sizes"
  @image, @domain = image, domain
  compute
end

Instance Attribute Details

#aObject (readonly)

The slope of the line.



986
987
988
# File 'lib/bullshit.rb', line 986

def a
  @a
end

#bObject (readonly)

The offset of the line.



989
990
991
# File 'lib/bullshit.rb', line 989

def b
  @b
end

#domainObject (readonly)

The domain data as an array.



983
984
985
# File 'lib/bullshit.rb', line 983

def domain
  @domain
end

#imageObject (readonly)

The image data as an array.



980
981
982
# File 'lib/bullshit.rb', line 980

def image
  @image
end

Instance Method Details

#residuesObject

Returns the residues of this linear regression in relation to the given domain and image.



1004
1005
1006
1007
1008
1009
1010
# File 'lib/bullshit.rb', line 1004

def residues
  result = []
  @domain.zip(@image) do |x, y|
    result << y - (@a * x + @b)
  end
  result
end

#slope_zero?(alpha = 0.05) ⇒ Boolean

Return true if the slope of the underlying data (not the sample data passed into the constructor of this LinearRegression instance) is likely (with alpha level alpha) to be zero.

Returns:

  • (Boolean)


994
995
996
997
998
999
1000
# File 'lib/bullshit.rb', line 994

def slope_zero?(alpha = 0.05)
  df = @image.size - 2
  return true if df <= 0 # not enough values to check
  t = tvalue(alpha)
  td = TDistribution.new df
  t.abs <= td.inverse_probability(1 - alpha.abs / 2.0).abs
end