lib/numru/ganalysis/fitting.rb in gphys-1.5.5 vs lib/numru/ganalysis/fitting.rb in gphys-1.5.6
- old
+ new
@@ -213,18 +213,20 @@
# (redundantly) in the +functions+ argument, as a matter of course.
# * Error is raised if the number of data is insuffcient for the
# number of functions (also unsolvable).
#
def least_square_fit(data, grid_locs, functions, ensemble_dims=nil,
- indep_dims=nil)
+ indep_dims=nil, with_offset=true)
#< argument check >
grid_locs.each_with_index{|x,i| self.ensure_1D_NArray(x, i)}
functions.each{|f| raise("Found non-Proc arg") if !f.is_a?(Proc)}
- functions = functions + [@@unity] # constanf offset
+ if with_offset
+ functions = functions + [@@unity] # constanf offset
+ end
ng = grid_locs.length
rank = data.rank
ensemble_dims = [ ensemble_dims ] if ensemble_dims.is_a?(Integer)
indep_dims = [ indep_dims ] if indep_dims.is_a?(Integer)
@@ -334,13 +336,20 @@
na = NArray.float(ms)
na[true] = c[true]
c = na
# best fit
- bf = c[-1] # the constant offset
- for i in 0...ms-1
- bf += c[i]*fv[i]
+ if with_offset
+ bf = c[-1] # the constant offset
+ for i in 0...ms-1
+ bf += c[i]*fv[i]
+ end
+ else
+ bf = 0.0
+ for i in 0...ms
+ bf += c[i]*fv[i]
+ end
end
else # fitting multiple times
# derive vectors
@@ -379,12 +388,19 @@
else
idshp_full[d] = 1
end
end
cs = c.reshape(ms, *idshp_full)
- bf = cs[-1,false]
- for i in 0...ms-1
- bf += cs[i,false]*fv[i]
+ if with_offset
+ bf = cs[-1,false]
+ for i in 0...ms-1
+ bf += cs[i,false]*fv[i]
+ end
+ else
+ bf = cs[-1,false] * 0
+ for i in 0...ms
+ bf += cs[i,false]*fv[i]
+ end
end
end
diff = Math.sqrt( ( (data + d0 - bf)**2 ).mean )