class MoreMath::LinearRegression

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

def compute

def compute
  size = @image.size
  sum_xx = sum_xy = sum_x = sum_y = 0.0
  @domain.zip(@image) do |x, y|
    x += 1
    sum_xx += x ** 2
    sum_xy += x * y
    sum_x += x
    sum_y += y
  end
  @a = (size * sum_xy - sum_x * sum_y) / (size * sum_xx - sum_x ** 2)
  @b = (sum_y - @a * sum_x) / size
  self
end

def initialize(image, domain = (0...image.size).to_a)

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

def residues

domain and image.
Returns the residues of this linear regression in relation to the given
def residues
  result = []
  @domain.zip(@image) do |x, y|
    result << y - (@a * x + @b)
  end
  result
end

def slope_zero?(alpha = 0.05)

(with alpha level _alpha_) to be zero.
passed into the constructor of this LinearRegression instance) is likely
Return true if the slope of the underlying data (not the sample data
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

def tvalue(alpha = 0.05)

def tvalue(alpha = 0.05)
  df = @image.size - 2
  return 0.0 if df <= 0
  sse_y = 0.0
  @domain.zip(@image) do |x, y|
    f_x = a * x + b
    sse_y += (y - f_x) ** 2
  end
  mean = @image.inject(0.0) { |s, y| s + y } / @image.size
  sse_x = @domain.inject(0.0) { |s, x| s + (x - mean) ** 2 }
  t = a / (Math.sqrt(sse_y / df) / Math.sqrt(sse_x))
  t.nan? ? 0.0 : t
end