Included Modules

Class/Module Index [+]

Quicksearch

Statsample::CovariateMatrix

Module to add method for variance/covariance and correlation matrices

Usage

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

Public Instance Methods

_type() click to toggle source

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

# File lib/statsample/matrix.rb, line 210
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) click to toggle source
# File lib/statsample/matrix.rb, line 222
def _type=(t)
  @type=t
end
correlation() click to toggle source
# File lib/statsample/matrix.rb, line 225
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_name() click to toggle source
# File lib/statsample/matrix.rb, line 253
def get_new_name
  @@covariatematrix+=1
  _("Covariate matrix %d") % @@covariatematrix
end
report_building(generator) click to toggle source
# File lib/statsample/matrix.rb, line 302
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) click to toggle source

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.extends CovariateMatrix
a.labels=%w{a b c}
a.submatrix(%{c a}, %w{b})
=> Matrix[[0.5],[0.3]]
a.submatrix(%{c a})
=> Matrix[[1.0, 0.2] , [0.2, 1.0]]
# File lib/statsample/matrix.rb, line 275
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 labels on index
  row_index=rows.collect {|v| 
    r=v.is_a?(Numeric) ? v : fields_x.index(v)
    raise "Index #{v} doesn't exists on matrix" if r.nil?
    r
  }
  column_index=columns.collect {|v| 
    r=v.is_a?(Numeric) ? v : fields_y.index(v)
    raise "Index #{v} doesn't exists on matrix" if r.nil?
    r
  }
  
  
  fx=row_index.collect {|v| fields_x[v]}
  fy=column_index.collect {|v| fields_y[v]}
    
  matrix= Matrix.rows(row_index.collect {|i|
    row=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) click to toggle source

Get variance for field k

# File lib/statsample/matrix.rb, line 249
def variance(k)
  submatrix([k])[0,0]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.