Module: LSModel
- Defined in:
- lib/lsmodel.rb
Overview
Modelling funtionality with the least square method
Constant Summary collapse
- MINIMAL_VARIATION_SIZE =
8
Class Method Summary collapse
-
.abstract_model(ins, outs, model, force = false) ⇒ Object
Abstract modelling method.
-
.abstract_modelD(ins, outs, model) ⇒ Object
mode abstract Modelling: multiple (but same) dimesions are allowed in input/output data arrays (ins, out) model should have the form:
"[1.0,input[0][i],input[1][i]]"
for affine model. -
.abstract_stability_index(ins, outs, model, model_coeffs) ⇒ Object
Computation of fjhgugui<F5>zt56789gfrihi.
-
.affine_model(x, y) ⇒ Object
Method for affine regression y = ax + b This is the direct version of what the abstract modelling method would do.
- .convertModelInputString(model) ⇒ Object
-
.linear_model(x, y) ⇒ Object
Function for simple computation of the slope of y over x, i.e.
-
.modelDim(model, forAdjustedStability = false) ⇒ Object
For the calculation of the adjusted stability index, a special Dimensionis needed: the number of coefficients without the constant term.
- .modelHasConstant?(model) ⇒ Boolean
- .modelOutput(model, model_coeffs, inputs) ⇒ Object
-
.stability_index(mode, s, y, *x) ⇒ Object
This function computed the know variance according the the model given by the ‘mode’ parameter: * ‘linear’ for the linear model y=s*x.
- .test_model(ins, out, model, model_coeffs) ⇒ Object
Class Method Details
.abstract_model(ins, outs, model, force = false) ⇒ Object
Abstract modelling method. See test files for usage.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/lsmodel.rb', line 154 def LSModel.abstract_model(ins, outs, model, force=false) minimal_variaion_size = (force) ? 2 : MINIMAL_VARIATION_SIZE unless (ins[0].size == outs.size and outs.size >= minimal_variaion_size) $stdout << "ERROR:\nMINIMAL_VARIATION_SIZE #{MINIMAL_VARIATION_SIZE} is not reached!\n" $stdout << "Only #{ins[0].size} measurements are present.\n" raise end params = [outs,ins] params.each_with_index {|param,index| if index == 0 # 'out' is a single array params[index] = param.each_with_index {|v,i| param[i] = v.to_f} else # 'ins' is an array of arrays param.each_with_index {|inn,i| param[i] = inn.each_with_index {|v,i| inn[i] = v.to_f} } params[index] = param end } out_vector = Vector.elements(outs) rows = [] (0..ins.first.size-1).each {|i| rows << eval(model) var = eval(model) } matrix = Matrix.rows(rows) model_coeffs = (matrix.transpose * matrix).inverse * matrix.transpose * out_vector r, = abstract_stability_index(ins, outs, model, model_coeffs) [model_coeffs,r] end |
.abstract_modelD(ins, outs, model) ⇒ Object
mode abstract Modelling: multiple (but same) dimesions are allowed in input/output data arrays (ins, out) model should have the form: "[1.0,input[0][i],input[1][i]]"
for affine model
!!EXPERIMENTAL!! Better use abstract_model
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lsmodel.rb', line 60 def LSModel.abstract_modelD(ins, outs, model) ########################################################################### # Calculation of the coefficients for the given model # # check for data quality # 1. Is there enough data? if ins[0].size < 2 puts "To few datasets!" raise end # 2. Has any dataset the same size? sizes = [] [ins,outs].each {|datas| datas.each {|dataset| sizes << dataset.size}} if sizes.uniq.size != 1 puts "Use datasets with same number of values!" raise end # 3. Convert everything from String to Float [ins,outs].each {|datas| datas.collect!{|data| data.collect {|data_| data_.to_f}}} # 4. preprocess the model model = convertModelInputString(model) # Create input/output matrices input = Matrix.columns(ins) output = Matrix.columns(outs) # Create the modelling Matrix rows = [] (0...sizes[0]).each {|i| rows << eval(model) } model_matrix = Matrix.rows(rows) # Computation of coefficients model_coeffs = (model_matrix.transpose * model_matrix).inverse * model_matrix.transpose * output ########################################################################### # Calculation of the stability r_total = 0.0 r_reg = 0.0 r_res = 0.0 modelDim = modelDim(model,true) #test p model + "=>" + "modelDim: " + modelDim.to_s # mean values for each measured variable n = output.row_size meanValues = [] output.column_vectors.each {|cv| meanValues << cv.to_a.inject {|sum,v| sum + v}/n} # each measurement variable has to be treated separately stability = [] output.column_vectors.each_with_index {|output_vector,k| modelOutput = model_matrix * model_coeffs.column(k) output_vector.to_a.each_with_index {|output_value,i| r_total += (output_value - meanValues[k])**2 r_res += (output_value - modelOutput[i])**2 r_reg += (modelOutput[i] - meanValues[k])**2 } r = r_reg/r_total r_improved = 1.0 - (1.0 - r**2)*(k-1.0)/(k - modelDim -1.0) stability << [r,r_improved] } return [model_coeffs, stability] end |
.abstract_stability_index(ins, outs, model, model_coeffs) ⇒ Object
Computation of fjhgugui<F5>zt56789gfrihi
,löl,00, <- this was nick ;)
Computation of stability index according the the given model with its coeffs
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/lsmodel.rb', line 196 def LSModel.abstract_stability_index(ins,outs,model,model_coeffs) r_total = 0.0 r_reg = 0.0 r_res = 0.0 n = outs.size.to_f outs_mid = outs.inject {|sum,i| sum + i } / n outs.each_with_index {|out,i| out_by_model = model_coeffs.inner_product(Vector.elements(eval(model))) r_total += (outs[i] - outs_mid) * (outs[i] - outs_mid) r_res += (outs[i] - out_by_model) * (outs[i] - out_by_model) r_reg += (out_by_model - outs_mid) * (out_by_model - outs_mid) } r_ = r_reg/r_total r = 1.0 - r_res/r_total [r, r_] end |
.affine_model(x, y) ⇒ Object
Method for affine regression y = ax + b This is the direct version of what the abstract modelling method would do
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lsmodel.rb', line 33 def LSModel.affine_model(x,y) return nil if x.size != y.size n = x.size.to_f params = [x, y] params.each_with_index {|param,index| params[index] = param.each_with_index {|v,i| param[i] = v.to_f} } x_V = Vector.elements(x) y_V = Vector.elements(y) x_S = x.inject {|sum,x_i| sum += x_i} y_S = y.inject {|sum,y_i| sum += y_i} # gradient a = (n*x_V.inner_product(y_V) - x_S*y_S)/(n*x_V.inner_product(x_V) - x_S*x_S) # offset b = (y_S - a*x_S)/n # fitnes r, = stability_index("affine",[a,b],y,x) [a,b,r] end |
.convertModelInputString(model) ⇒ Object
121 122 123 |
# File 'lib/lsmodel.rb', line 121 def LSModel.convertModelInputString(model) "[" + model.gsub(/\[(\d+)\]/,"\[i,\\1\]") + "]" end |
.linear_model(x, y) ⇒ Object
Function for simple computation of the slope of y over x, i.e. y = ax, x is input and y is the measured value. Both should behave like arrays. This is the direct version of what the abstract modelling method would do
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lsmodel.rb', line 12 def LSModel.linear_model(x,y) return nil if x.size != y.size # recursive to_f for x and y params = [x, y] params.each_with_index {|param,index| params[index] = param.each_with_index {|v,i| param[i] = v.to_f} } s = nil x_V = Vector.elements(x) y_V = Vector.elements(y) s = x_V.inner_product(y_V)/x_V.inner_product(x_V) r, = stability_index("linear",s,y,x) [s, r] end |
.modelDim(model, forAdjustedStability = false) ⇒ Object
For the calculation of the adjusted stability index, a special Dimensionis needed: the number of coefficients without the constant term.
134 135 136 137 138 139 |
# File 'lib/lsmodel.rb', line 134 def LSModel.modelDim(model,forAdjustedStability=false) input = Matrix.scalar(100,0.0) i = 0 dim = eval(model).size (forAdjustedStability and modelHasConstant?(model)) ? dim - 1 : dim end |
.modelHasConstant?(model) ⇒ Boolean
125 126 127 128 129 130 |
# File 'lib/lsmodel.rb', line 125 def LSModel.modelHasConstant?(model) input = Matrix.scalar(100,0.0) i = 0 modelArray = eval(model) modelArray.include?(1.0) end |
.modelOutput(model, model_coeffs, inputs) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/lsmodel.rb', line 141 def LSModel.modelOutput(model, model_coeffs, inputs) inputs.collect!{|input| input.collect {|inn| inn.to_f}} input = Matrix.rows(inputs) model = convertModelInputString(model) # Create the modelling Matrix rows = [] (0...input.row_size).each {|i| rows << eval(model) } model_matrix = Matrix.rows(rows) #test pp model_matrix modelOutput = model_matrix * model_coeffs end |
.stability_index(mode, s, y, *x) ⇒ Object
This function computed the know variance according the the model given by the ‘mode’ parameter:
-
‘linear’ for the linear model y=s*x
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/lsmodel.rb', line 219 def LSModel.stability_index(mode, s, y, *x) r_total = 0.0 r_reg = 0.0 r_res = 0.0 n = y.size.to_f y_mid = y.inject {|sum,i| sum + i } / n case mode when "linear" x = x[0] y.each_with_index {|y_i,i| ys_i = s*x[i] r_total += (y_i - y_mid) * (y_i - y_mid) r_res += (y_i - ys_i) * (y_i - ys_i) r_reg += (ys_i - y_mid) * (ys_i - y_mid) } when "affine" a, b = s y.each_with_index {|y_i,i| ys_i = a*x[0][i] + b r_total += (y_i - y_mid) * (y_i - y_mid) r_res += (y_i - ys_i) * (y_i - ys_i) r_reg += (ys_i - y_mid) * (ys_i - y_mid) } else puts "Wrong working mode in function 'stability_index'! " + "Use 'linear' or 'affine' instead.'" raise end r_ = r_reg/r_total r = 1.0 - r_res/r_total [r, r_] end |
.test_model(ins, out, model, model_coeffs) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/lsmodel.rb', line 254 def LSModel.test_model(ins, out, model, model_coeffs) params = [out,ins] params.each_with_index {|param,index| if index == 0 # 'out' is a single array params[index] = param.each_with_index {|v,i| param[i] = v.to_f} else # 'ins' is an array of arrays param.each_with_index {|inn,i| param[i] = inn.each_with_index {|v,i| inn[i] = v.to_f} } params[index] = param end } r_total = 0.0 r_reg = 0.0 r_res = 0.0 n = out.size.to_f out_mid = out.inject {|sum,i| sum + i } / n model_out = [] model_diff = [] out.each_with_index {|value,i| out_by_model = model_coeffs.inner_product(Vector.elements(eval(model))) model_out << out_by_model model_diff << out_by_model - value r_total += (value - out_mid) * (value - out_mid) r_res += (value - out_by_model) * (value - out_by_model) r_reg += (out_by_model - out_mid) * (out_by_model - out_mid) } r_ = r_reg/r_total r = 1.0 - r_res/r_total {:r => r,:model => model_out, :diff => model_diff} end |