Class: Quant::Statistics::Correlation

Inherits:
Object
  • Object
show all
Defined in:
lib/quant/statistics/correlation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCorrelation

Returns a new instance of Correlation.



6
7
8
9
10
11
12
13
# File 'lib/quant/statistics/correlation.rb', line 6

def initialize
  @length = 0.0
  @sx = 0.0
  @sy = 0.0
  @sxx = 0.0
  @sxy = 0.0
  @syy = 0.0
end

Instance Attribute Details

#lengthObject

Returns the value of attribute length.



4
5
6
# File 'lib/quant/statistics/correlation.rb', line 4

def length
  @length
end

#sxObject

Returns the value of attribute sx.



4
5
6
# File 'lib/quant/statistics/correlation.rb', line 4

def sx
  @sx
end

#sxxObject

Returns the value of attribute sxx.



4
5
6
# File 'lib/quant/statistics/correlation.rb', line 4

def sxx
  @sxx
end

#sxyObject

Returns the value of attribute sxy.



4
5
6
# File 'lib/quant/statistics/correlation.rb', line 4

def sxy
  @sxy
end

#syObject

Returns the value of attribute sy.



4
5
6
# File 'lib/quant/statistics/correlation.rb', line 4

def sy
  @sy
end

#syyObject

Returns the value of attribute syy.



4
5
6
# File 'lib/quant/statistics/correlation.rb', line 4

def syy
  @syy
end

Instance Method Details

#add(x, y) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/quant/statistics/correlation.rb', line 15

def add(x, y)
  @length += 1
  @sx += x
  @sy += y
  @sxx += x * x
  @sxy += x * y
  @syy += y * y
end

#coefficientObject



29
30
31
32
33
# File 'lib/quant/statistics/correlation.rb', line 29

def coefficient
  (length * sxy - sx * sy) / Math.sqrt(devisor)
rescue Math::DomainError
  0.0
end

#devisorObject



24
25
26
27
# File 'lib/quant/statistics/correlation.rb', line 24

def devisor
  value = (length * sxx - sx**2) * (length * syy - sy**2)
  value.zero? ? 1.0 : value
end