12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/paleta/core_ext/math.rb', line 12
def multiple_regression(dx, dy, dz)
regression = {}
regression[:slope], regression[:offset] = {}, {}
size = dx.size
raise "arguments not same length!" unless size == dy.size && size == dz.size
if size == 1
regression[:slope] = { :x => dx[0], :y => dy[0], :z => dz[0] }
regression[:offset] = { :x => 0, :y => 0, :z => 0 }
return regression
end
sxx = syy = szz = sxy = szx = syz = sx = sy = sz = 0
dx.zip(dy, dz).each do |x, y, z|
sxx += x ** 2
syy += y ** 2
szz += z ** 2
sxy += x * y
szx += z * x
syz += y * z
sx += x
sy += y
sz += z
end
regression[:slope][:x] = ( size * sxy - sx * sz ) / ( size * sxx - sx ** 2 ).to_f
regression[:slope][:y] = ( size * syz - sz * sy ) / ( size * syy - sz ** 2 ).to_f
regression[:slope][:z] = ( size * syz - sz * sy ) / ( size * szz - sy ** 2 ).to_f
regression[:offset][:x] = (sz - regression[:slope][:x] * sx) / size
regression[:offset][:y] = (sy - regression[:slope][:y] * sz) / size
regression[:offset][:z] = (sx - regression[:slope][:z] * sy) / size
regression
end
|