Ankur Goel

On hunt of awesomeness!

Utility Functions and Documentation

I have been coding the utility functions for matrices and vectors which we are/will need frequently in further functionalities.
One of it is to add_constant to a matrix. add_constant prepends or appends a column of ones to a matrix if it already doesn’t have one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#=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
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

There are other such methods such as chain_dot which carries out dot multplication of matrices in chain. It uses the ruby’s reduce ability to reduce the available arguments(matrices) to consequential product.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#=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
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

Apart from adding such functionalities, I have covered entire documentation of statsample-timeseries. I made sure to explain role of each function, every input parameter, and return type of the function. In most of the cases, I also added the usage examples too.
Later, I will add the usage examples in all those which are still not equipped with that and details about parameters wherever it is still missing.

After this, with the great help from Claudio and Ra’s pointers about modular(namespace) hierarchial convention; we managed to make it more conventional. :)
Here are the final results:

1
2
3
4
5
6
module Statsample::TimeSeries #Module for all Timeseries related stuff
class Statsample::TimeSeries::Series < Statsmple::Vector  # Class containing a timeseries objects and general related methods

module Statsample::TimeSeries::Pacf #Pacf related methods
class Statsample::TimeSeries::Arima #Arima class, which is initialized by class method
class Statsample::TimeSeries::Arima::KalmanFilter # For Kalman Filter on ARIMA

We are now reading and continuing to code Kalman filter. Hope it doesn’t stay tricky. :)

Till next time,
Cheers,
- Ankur Goel