class Matrix
Public Class Methods
Chain Product¶ ↑
Class method Returns the chain product of two matrices
Usage:¶ ↑
Let `a` be 4 * 3 matrix, Let `b` be 3 * 3 matrix, Let `c` be 3 * 1 matrix, then `Matrix.chain_dot(a, b, c)`
NOTE:¶ ↑
Send the matrices in multiplicative order with proper dimensions
# File lib/statsample-timeseries/utility.rb, line 74 def self.chain_dot(*args) #inspired by Statsmodels begin args.reduce { |x, y| x * y } #perform matrix multiplication in order rescue ExceptionForMatrix::ErrDimensionMismatch puts "ExceptionForMatrix: Please provide matrices with proper multiplicative dimensions" end end
Public Instance Methods
Adds a column of constants.¶ ↑
Appends a column of ones to the matrix/array if first argument is false If an n-array, first checks if one column of ones is already present if present, then original(self) is returned, else, prepends with a vector of ones
# File lib/statsample-timeseries/utility.rb, line 88 def add_constant(prepend = true) #for Matrix (0...column_size).each do |i| if self.column(i).map(&:to_f) == Object::Vector.elements(Array.new(row_size, 1.0)) return self end end #append/prepend a column of one's vectors = (0...row_size).map do |r| if prepend [1.0].concat(self.row(r).to_a) else self.row(r).to_a.push(1.0) end end return Matrix.rows(vectors) end
Cholesky decomposition¶ ↑
Reference: en.wikipedia.org/wiki/Cholesky_decomposition
Description¶ ↑
Cholesky decomposition is reprsented by `M = L X L*`, where M is the symmetric matrix and `L` is the lower half of cholesky matrix, and `L*` is the conjugate form of `L`.
Returns¶ ↑
Cholesky decomposition for a given matrix(if symmetric)
Utility¶ ↑
Essential matrix function, requisite in kalman filter, least squares
# File lib/statsample-timeseries/utility.rb, line 45 def cholesky raise ArgumentError, "Given matrix should be symmetric" unless symmetric? c = Matrix.zero(row_size) 0.upto(row_size - 1).each do |k| 0.upto(row_size - 1).each do |i| if i == k sum = (0..(k-1)).inject(0.0){ |sum, j| sum + c[k, j] ** 2 } value = Math.sqrt(self[k,k] - sum) c[k, k] = value elsif i > k sum = (0..(k-1)).inject(0.0){ |sum, j| sum + c[i, j] * c[k, j] } value = (self[k,i] - sum) / c[k, k] c[i, k] = value end end end c end
populates column i of given matrix with arr
# File lib/statsample-timeseries/utility.rb, line 107 def set_column(i, arr) columns = self.column_vectors column = columns[i].to_a column[0...arr.size] = arr columns[i] = column return Matrix.columns(columns) end
populates row i of given matrix with arr
# File lib/statsample-timeseries/utility.rb, line 116 def set_row(i, arr) #similar implementation as set_column #writing and commenting metaprogrammed version #Please to give opinion :) rows = self.row_vectors row = rows[i].to_a row[0...arr.size] = arr rows[i] = row return Matrix.rows(rows) end