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.



77
78
79
80
81
82
83
# File 'lib/cicada/fitting/p3d_fitter.rb', line 77

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.



86
87
88
# File 'lib/cicada/fitting/p3d_fitter.rb', line 86

def min_prob
  @min_prob
end

#rObject

Returns the value of attribute r.



85
86
87
# File 'lib/cicada/fitting/p3d_fitter.rb', line 85

def r
  @r
end

#sObject

Returns the value of attribute s.



86
87
88
# File 'lib/cicada/fitting/p3d_fitter.rb', line 86

def s
  @s
end

#should_fit_sObject

Returns the value of attribute should_fit_s.



85
86
87
# File 'lib/cicada/fitting/p3d_fitter.rb', line 85

def should_fit_s
  @should_fit_s
end

#use_min_probObject

Returns the value of attribute use_min_prob.



85
86
87
# File 'lib/cicada/fitting/p3d_fitter.rb', line 85

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.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cicada/fitting/p3d_fitter.rb', line 137

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



124
125
126
# File 'lib/cicada/fitting/p3d_fitter.rb', line 124

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