Class: DTW::BarycenterAveraging

Inherits:
Object
  • Object
show all
Defined in:
lib/dtwrb/barycenter_averaging.rb,
sig/dtwrb.rbs

Constant Summary collapse

DEFAULT_ITERATIONS =

DBA converges within a few refinement passes; further passes rarely move the prototype.

Returns:

  • (::Integer)
4
DEFAULT_TOLERANCE =

Mean absolute update per coordinate below which the prototype counts as settled.

Returns:

  • (::Float)
1e-4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aligner: Aligner.new, aggregator: :median, dispersion: :median_absolute_deviation, iterations: DEFAULT_ITERATIONS, tolerance: DEFAULT_TOLERANCE, sample_size: Medoid::DEFAULT_SAMPLE_SIZE) ⇒ BarycenterAveraging

Returns a new instance of BarycenterAveraging.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dtwrb/barycenter_averaging.rb', line 13

def initialize(
  aligner: Aligner.new,
  aggregator: :median,
  dispersion: :median_absolute_deviation,
  iterations: DEFAULT_ITERATIONS,
  tolerance: DEFAULT_TOLERANCE,
  sample_size: Medoid::DEFAULT_SAMPLE_SIZE
)
  @aligner = aligner
  @aggregator = Statistics.resolve(aggregator)
  @dispersion = Statistics.resolve(dispersion)
  @iterations = Integer(iterations)
  @tolerance = Float(tolerance)
  @medoid = Medoid.new(aligner: aligner, sample_size: sample_size)

  raise ArgumentError, "iterations must be non-negative, got #{@iterations}" if @iterations.negative?
  raise ArgumentError, "tolerance must be non-negative, got #{@tolerance}" if @tolerance.negative?

  freeze
end

Instance Attribute Details

#alignerAligner (readonly)

Returns the value of attribute aligner.

Returns:



11
12
13
# File 'lib/dtwrb/barycenter_averaging.rb', line 11

def aligner
  @aligner
end

#iterations::Integer (readonly)

Returns the value of attribute iterations.

Returns:

  • (::Integer)


11
12
13
# File 'lib/dtwrb/barycenter_averaging.rb', line 11

def iterations
  @iterations
end

#tolerance::Float (readonly)

Returns the value of attribute tolerance.

Returns:

  • (::Float)


11
12
13
# File 'lib/dtwrb/barycenter_averaging.rb', line 11

def tolerance
  @tolerance
end

Instance Method Details

#call(sequences, length: nil) ⇒ Barycenter

Parameters:

  • (::Array[untyped])
  • length: (::Integer, nil) (defaults to: nil)

Returns:

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
# File 'lib/dtwrb/barycenter_averaging.rb', line 34

def call(sequences, length: nil)
  sample = Sample.of(sequences)
  width = Integer(length || sample.mean_length)
  raise ArgumentError, "barycenter length must be positive, got #{width}" unless width.positive?

  center = refine(Resampler.call(@medoid.call(sample.sequences), width), sample)
  spread = summarize(associate(center, sample), @dispersion) { 0.0 }

  build(center, spread, sample)
end