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.
123456789101112131415161718192021
#=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 onesdefadd_constant(prepend=true)#for Matrix(0...column_size).eachdo|i|ifself.column(i).map(&:to_f)==Object::Vector.elements(Array.new(row_size,1.0))returnselfendend#append/prepend a column of one'svectors=(0...row_size).mapdo|r|ifprepend[1.0].concat(self.row(r).to_a)elseself.row(r).to_a.push(1.0)endendreturnMatrix.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.
1234567891011121314151617
#=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 dimensionsdefself.chain_dot(*args)#inspired by Statsmodelsbeginargs.reduce{|x,y|x*y}#perform matrix multiplication in orderrescueExceptionForMatrix::ErrDimensionMismatchputs"ExceptionForMatrix: Please provide matrices with proper multiplicative dimensions"endend
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:
123456
moduleStatsample::TimeSeries#Module for all Timeseries related stuffclassStatsample::TimeSeries::Series<Statsmple::Vector# Class containing a timeseries objects and general related methodsmoduleStatsample::TimeSeries::Pacf#Pacf related methodsclassStatsample::TimeSeries::Arima#Arima class, which is initialized by class methodclassStatsample::TimeSeries::Arima::KalmanFilter# For Kalman Filter on ARIMA
We are now reading and continuing to code Kalman filter. Hope it doesn’t stay tricky. :)