Class: Cicada::P3DObjectiveFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/cicada/fitting/p3d_fitter.rb

Overview

An objective function that calculates the negative log likelihood of supplied data points being generated from a p3d distribution with specified parameters.

The data points should be an array of (positive) scalars set using the attribute r.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeP3DObjectiveFunction

Constructs an empty P3DObjectiveFunction.



83
84
85
86
87
88
89
90
91
# File 'lib/cicada/fitting/p3d_fitter.rb', line 83

def initialize

  @r = nil
  @s = nil
  @min_prob = nil
  @use_min_prob = false
  @should_fit_s = true

end

Instance Attribute Details

#min_probObject

Returns the value of attribute min_prob.



95
96
97
# File 'lib/cicada/fitting/p3d_fitter.rb', line 95

def min_prob
  @min_prob
end

#rObject

Returns the value of attribute r.



93
94
95
# File 'lib/cicada/fitting/p3d_fitter.rb', line 93

def r
  @r
end

#sObject

Returns the value of attribute s.



95
96
97
# File 'lib/cicada/fitting/p3d_fitter.rb', line 95

def s
  @s
end

#should_fit_sObject

Returns the value of attribute should_fit_s.



93
94
95
# File 'lib/cicada/fitting/p3d_fitter.rb', line 93

def should_fit_s
  @should_fit_s
end

#use_min_probObject

Returns the value of attribute use_min_prob.



93
94
95
# File 'lib/cicada/fitting/p3d_fitter.rb', line 93

def use_min_prob
  @use_min_prob
end

Instance Method Details

#evaluate(point) ⇒ Float

Evaluates the negative log-likelihood of the data given the parameters specified.

Parameters:

  • point (Array<Numeric>)

    a 2-element array containing the mean- and standard deviation-like parameters. If a static standard deviation parameter is being used, something should still be provided here, but it will be ignored.

Returns:

  • (Float)

    the negative log-likelihood of the data.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cicada/fitting/p3d_fitter.rb', line 150

def evaluate(point)

  point = point.toArray unless point.is_a? Array

  m = point[0]
  s = point[1]
  s = @s unless @should_fit_s

  return Float::MAX if (m < 0 or s < 0)

  r.reduce(0.0) do |sum, ri|

    temp_neg_log_p = -1.0*Math.log( p3d(ri, m, s))

    if (@use_min_prob and temp_neg_log_p > @min_prob) then
      
      sum + @min_prob

    else

      sum + temp_neg_log_p

    end

  end
    
end

#p3d(r, m, s) ⇒ Float

Calculates the probability density of the p3d distribution at a given point.

Parameters:

  • r (Numeric)

    the distance at which to calculate the probability density

  • m (Numeric)

    the mean-like parameter of the p3d distribution

  • s (Numeric)

    the standard-deviation-like parameter of the p3d distribution

Returns:

  • (Float)

    the probability density at the given point



135
136
137
138
139
# File 'lib/cicada/fitting/p3d_fitter.rb', line 135

def p3d(r, m, s)

  (Math.sqrt(2.0/Math::PI)*r/(2*m*s))*(Math.exp(-1 * (m-r)**2/(2*s**2)) - Math.exp( -1 * (m+r)**2/(2*s**2)))

end