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

Defined Under Namespace

Classes: AExpBX, AExpBX32, AXInv, AXInv2, AXInv3, AXInv32, 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
# 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
  @diff_abs_pairs = data_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

#equationObject

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



37
38
39
# File 'lib/malge/errorfittedfunction.rb', line 37

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.



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

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.



66
67
68
69
70
71
72
# File 'lib/malge/errorfittedfunction.rb', line 66

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

#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.



52
53
54
55
56
57
58
# File 'lib/malge/errorfittedfunction.rb', line 52

def variance
  sum = 0.0
  @diff_abs_pairs.each do |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’.



61
62
63
# File 'lib/malge/errorfittedfunction.rb', line 61

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