Class: Malge::ErrorFittedFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/malge/errorfittedfunction.rb

Overview

Class for function to which x and y fit. This class include mathematical dealing for errors. Not simple fitting; this class assume true value with the expected most precise value and use absolute value of difference between a data point and the assumed true value.

Direct Known Subclasses

AExpBX, AExpBX32, AXInv, AXInv2, AXInv3, AXInv32, AXInv4

Defined Under Namespace

Classes: AExpBX, AExpBX32, AXInv, AXInv2, AXInv3, AXInv32, AXInv4, NotImplementedError, SizeMismatchError, TypeError, UnableCalculationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_pairs) ⇒ ErrorFittedFunction

Generate error fitted function from x and y. Errors are generated y and y_true assumed by the expected most precise y.

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/malge/errorfittedfunction.rb', line 19

def initialize(data_pairs)

  raise TypeError unless data_pairs.class == Array
  data_pairs.each do |pair|
    raise TypeError unless pair.class == Array
    raise SizeMismatchError unless pair.size == 2
  end

  #@raw_pairs = data_pairs
  @raw_pairs = data_pairs.sort_by{|xy| xy[0]}
  most_strict_x = most_strict_pair[0]
  tmp_pairs = Marshal.load(Marshal.dump( @raw_pairs))
  tmp_pairs.delete_if {|pair| pair[0] == most_strict_x}

  @diff_abs_pairs = tmp_pairs.map { |pair| [pair[0], (pair[1] - most_strict_pair[1]).abs] }
  fit

  @coefficients.each do |coef|
    raise UnableCalculationError unless coef.finite?
  end
end

Instance Attribute Details

#coefficientsObject (readonly)

Returns the value of attribute coefficients.



10
11
12
# File 'lib/malge/errorfittedfunction.rb', line 10

def coefficients
  @coefficients
end

#diff_abs_pairsObject (readonly)

Returns the value of attribute diff_abs_pairs.



10
11
12
# File 'lib/malge/errorfittedfunction.rb', line 10

def diff_abs_pairs
  @diff_abs_pairs
end

#raw_pairsObject (readonly)

Returns the value of attribute raw_pairs.



10
11
12
# File 'lib/malge/errorfittedfunction.rb', line 10

def raw_pairs
  @raw_pairs
end

Instance Method Details

#count_equal_under_overObject

Compare expected error and @raw_pairs and return a count of estimations of [equal, over, under]. (order is like as result of <=>. ) Excluding last data.



84
85
86
87
88
89
90
# File 'lib/malge/errorfittedfunction.rb', line 84

def count_equal_under_over
  results = [0,0,0]
  @diff_abs_pairs.each do |x,y|
    results[expected_error(x) <=> y ] += 1
  end
  results
end

#equationObject

Return string which is easily readable for people to know the function.



42
43
44
# File 'lib/malge/errorfittedfunction.rb', line 42

def equation
  raise NotImplementedError, "Define #{__method__}() in the inherited class."
end

#expected_error(x) ⇒ Object

Return expected error at x, condition variable, on the fitted function. Note that this method does not return the error between actual and true value.



48
49
50
# File 'lib/malge/errorfittedfunction.rb', line 48

def expected_error(x)
  raise NotImplementedError, "Define #{__method__}() in the inherited class."
end

#most_strict_pairObject

Return the value of y at which the most precise data is expected to be obtained.



72
73
74
75
76
77
78
# File 'lib/malge/errorfittedfunction.rb', line 72

def most_strict_pair
  raise NotImplementedError, "Define #{__method__}() in the inherited class."

  #In the most case, it would be sufficient to select belows.
  @raw_pairs.max_by{ |pair| pair[0] }
  @raw_pairs.mix_by{ |pair| pair[0] }
end

#summary(io = $stdout) ⇒ Object



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
# File 'lib/malge/errorfittedfunction.rb', line 92

def summary(io = $stdout)
 io.puts "Fitted function: #{equation}"
 io.printf("%15s, %15s, %15s, %15s\n",
    "x",
    "raw_y",
    "|diff_y_best|",
    "expected_error"
    )

 datalist = []
 @raw_pairs.size.times do |i|
  if i != (@raw_pairs.size - 1)
   diff = sprintf("%15.10f", @diff_abs_pairs[i][1])
  else
   diff = '-'*12
  end

  datalist << sprintf("%15.10f, %15.10f, %15s, %15.10f\n",
     @raw_pairs[i][0],
     @raw_pairs[i][1],
     diff, 
     expected_error(@raw_pairs[i][0])
     )
 end
 io.puts datalist.sort.join
end

#varianceObject

Return variance of distribution between each pair of expected error and y, actual data point. Note that this method does not return variance of difference between each pair of TRUE value and y. Ignore last value of x and y.



57
58
59
60
61
62
63
64
# File 'lib/malge/errorfittedfunction.rb', line 57

def variance
  sum = 0.0
  @diff_abs_pairs.each do |pair|
    #pp pair
    sum += (pair[1] - expected_error(pair[0]) )**2
  end
  sum
end

#x(y) ⇒ Object

Return value of ‘x’, assumed condition value, correspond to extpected value of ‘y’.



67
68
69
# File 'lib/malge/errorfittedfunction.rb', line 67

def x(y)
  raise NotImplementedError, "Define #{__method__}() in the inherited class."
end