Module: Statsample::CovariateMatrix

Includes:
NamedMatrix
Defined in:
lib/statsample/matrix.rb

Overview

Module to add method for variance/covariance and correlation matrices

Usage

matrix=Matrix[[1,2],[2,3]]
matrix.extend CovariateMatrix

Constant Summary collapse

@@covariatematrix =
0

Instance Method Summary collapse

Methods included from NamedMatrix

#fields, #fields=, #fields_x, #fields_x=, #fields_y, #fields_y=, #name, #name=

Methods included from Summarizable

#summary

Instance Method Details

#_typeObject

Get type of covariate matrix. Could be :covariance or :correlation



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/statsample/matrix.rb', line 226

def _type
  if row_size==column_size
    if row_size.times.find {|i| self[i,i]!=1.0}
      :covariance
    else
      :correlation
    end
  else
    @type
  end

end

#_type=(t) ⇒ Object



238
239
240
# File 'lib/statsample/matrix.rb', line 238

def _type=(t)
  @type=t
end

#correlationObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/statsample/matrix.rb', line 241

def correlation
  if(_type==:covariance)
    matrix=Matrix.rows(row_size.times.collect { |i|
      column_size.times.collect { |j|
        if i==j
          1.0
        else
          self[i,j].quo(Math::sqrt(self[i,i])*Math::sqrt(self[j,j]))
        end
      }
    })
    matrix.extend CovariateMatrix
    matrix.fields_x=fields_x
    matrix.fields_y=fields_y
    matrix._type=:correlation
    matrix
  else
    self
  end
end

#get_new_nameObject



269
270
271
272
# File 'lib/statsample/matrix.rb', line 269

def get_new_name
  @@covariatematrix+=1
  _("Covariate matrix %d") % @@covariatematrix
end

#report_building(generator) ⇒ Object



318
319
320
321
322
323
324
325
326
327
# File 'lib/statsample/matrix.rb', line 318

def report_building(generator)
  @name||= (_type==:correlation ? _("Correlation"):_("Covariance"))+_(" Matrix")
  generator.table(:name=>@name, :header=>[""]+fields_y) do |t|
    row_size.times {|i|
      t.row([fields_x[i]]+row(i).to_a.collect {|i1|
          i1.nil? ? "--" : sprintf("%0.3f",i1).gsub("0.",".")
      })
    }
  end
end

#submatrix(rows, columns = nil) ⇒ Object

Select a submatrix of factors. If you have a correlation matrix with a, b and c, you could obtain a submatrix of correlations of a and b, b and c or a and b

You could use labels or index to select the factors. If you don’t specify columns, its will be equal to rows.

Example:

a=Matrix[[1.0, 0.3, 0.2],
         [0.3, 1.0, 0.5],
         [0.2, 0.5, 1.0]]
a.extend CovariateMatrix
a.fields=%w{a b c}
a.submatrix(%w{c a}, %w{b})
=> Matrix[[0.5],[0.3]]
a.submatrix(%w{c a})
=> Matrix[[1.0, 0.2] , [0.2, 1.0]]

Raises:

  • (ArgumentError)


291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/statsample/matrix.rb', line 291

def submatrix(rows,columns = nil)
  raise ArgumentError, "rows shouldn't be empty" if rows.respond_to? :size and rows.size == 0
  columns ||= rows
  # Convert all fields on index
  row_index = rows.collect do |v|
    r = v.is_a?(Numeric) ? v : fields_x.index(v)
    raise "Index #{v} doesn't exists on matrix" if r.nil?
    r
  end

  column_index = columns.collect do |v|
    r = v.is_a?(Numeric) ? v : fields_y.index(v)
    raise "Index #{v} doesn't exists on matrix" if r.nil?
    r
  end


  fx=row_index.collect {|v| fields_x[v]}
  fy=column_index.collect {|v| fields_y[v]}

  matrix = Matrix.rows(row_index.collect { |i| column_index.collect { |j| self[i, j] }})
  matrix.extend CovariateMatrix
  matrix.fields_x = fx
  matrix.fields_y = fy
  matrix._type = _type
  matrix
end

#variance(k) ⇒ Object

Get variance for field k



265
266
267
# File 'lib/statsample/matrix.rb', line 265

def variance(k)
  submatrix([k])[0,0]
end