Module: Statsample::CovariateMatrix
- 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
Instance Method Summary collapse
- #correlation ⇒ Object
- #fields ⇒ Object
- #fields=(v) ⇒ Object
- #fields_x ⇒ Object
- #fields_x=(v) ⇒ Object
- #fields_y ⇒ Object
- #fields_y=(v) ⇒ Object
- #name ⇒ Object
- #name=(v) ⇒ Object
- #report_building(generator) ⇒ Object
-
#submatrix(rows, columns = nil) ⇒ Object
Select a submatrix of factors.
-
#summary ⇒ Object
Gives a nice.
-
#type ⇒ Object
Get type of covariate matrix.
- #type=(t) ⇒ Object
Instance Method Details
#correlation ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/statsample/matrix.rb', line 123 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 |
#fields ⇒ Object
143 144 145 146 |
# File 'lib/statsample/matrix.rb', line 143 def fields raise "Should be square" if !square? @fields_x end |
#fields=(v) ⇒ Object
147 148 149 150 151 |
# File 'lib/statsample/matrix.rb', line 147 def fields=(v) raise "Matrix should be square" if !square? @fields_x=v @fields_y=v end |
#fields_x ⇒ Object
160 161 162 163 164 165 |
# File 'lib/statsample/matrix.rb', line 160 def fields_x if @fields_x.nil? @fields_x=row_size.times.collect {|i| i} end @fields_x end |
#fields_x=(v) ⇒ Object
152 153 154 155 |
# File 'lib/statsample/matrix.rb', line 152 def fields_x=(v) raise "Size of fields != row_size" if v.size!=row_size @fields_x=v end |
#fields_y ⇒ Object
166 167 168 169 170 171 |
# File 'lib/statsample/matrix.rb', line 166 def fields_y if @fields_y.nil? @fields_y=column_size.times.collect {|i| i} end @fields_y end |
#fields_y=(v) ⇒ Object
156 157 158 159 |
# File 'lib/statsample/matrix.rb', line 156 def fields_y=(v) raise "Size of fields != column_size" if v.size!=column_size @fields_y=v end |
#name ⇒ Object
176 177 178 |
# File 'lib/statsample/matrix.rb', line 176 def name @name end |
#name=(v) ⇒ Object
173 174 175 |
# File 'lib/statsample/matrix.rb', line 173 def name=(v) @name=v end |
#report_building(generator) ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/statsample/matrix.rb', line 212 def report_building(generator) @name||= (type==:correlation ? "Correlation":"Covariance")+" Matrix" t=ReportBuilder::Table.new(:name=>@name, :header=>[""]+fields_y) row_size.times {|i| t.row([fields_x[i]]+@rows[i].collect {|i1| sprintf("%0.3f",i1).gsub("0.",".")}) } generator.parse_element(t) end |
#submatrix(rows, columns = nil) ⇒ Object
Select a submatrix of factors. You could use labels or index to select the factors. If you don’t specify columns, 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]]
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/statsample/matrix.rb', line 190 def submatrix(rows,columns=nil) columns||=rows # Convert all labels on index row_index=rows.collect {|v| v.is_a?(Numeric) ? v : fields_x.index(v) } column_index=columns.collect {|v| v.is_a?(Numeric) ? v : fields_y.index(v) } 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 |
#summary ⇒ Object
Gives a nice
102 103 104 105 106 |
# File 'lib/statsample/matrix.rb', line 102 def summary rp=ReportBuilder.new() rp.add(self) rp.to_text end |
#type ⇒ Object
Get type of covariate matrix. Could be :covariance or :correlation
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/statsample/matrix.rb', line 108 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
120 121 122 |
# File 'lib/statsample/matrix.rb', line 120 def type=(t) @type=t end |