Class: Legion::Extensions::Temporal::Helpers::TemporalPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/temporal/helpers/temporal_pattern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain:, event:) ⇒ TemporalPattern

Returns a new instance of TemporalPattern.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 11

def initialize(domain:, event:)
  @domain = domain
  @event = event
  @intervals = []
  @pattern_type = :random
  @mean_interval = nil
  @observation_count = 0
  @last_predicted = nil
  @accuracy_count = 0
  @total_predictions = 0
end

Instance Attribute Details

#accuracy_countObject (readonly)

Returns the value of attribute accuracy_count.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def accuracy_count
  @accuracy_count
end

#domainObject (readonly)

Returns the value of attribute domain.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def domain
  @domain
end

#eventObject (readonly)

Returns the value of attribute event.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def event
  @event
end

#last_predictedObject (readonly)

Returns the value of attribute last_predicted.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def last_predicted
  @last_predicted
end

#mean_intervalObject (readonly)

Returns the value of attribute mean_interval.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def mean_interval
  @mean_interval
end

#observation_countObject (readonly)

Returns the value of attribute observation_count.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def observation_count
  @observation_count
end

#pattern_typeObject (readonly)

Returns the value of attribute pattern_type.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def pattern_type
  @pattern_type
end

#total_predictionsObject (readonly)

Returns the value of attribute total_predictions.



8
9
10
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 8

def total_predictions
  @total_predictions
end

Instance Method Details

#add_observation(timestamps) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 23

def add_observation(timestamps)
  return if timestamps.size < 2

  @intervals = compute_intervals(timestamps)
  @observation_count = timestamps.size
  @mean_interval = @intervals.sum / @intervals.size.to_f
  @pattern_type = classify_pattern
end

#bursty?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 59

def bursty?
  @pattern_type == :bursty
end

#periodic?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 55

def periodic?
  @pattern_type == :periodic
end

#predict_next(from: Time.now.utc) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 32

def predict_next(from: Time.now.utc)
  return nil unless @mean_interval && @observation_count >= Constants::MIN_PATTERN_OBSERVATIONS

  predicted = from + @mean_interval
  @last_predicted = predicted
  @total_predictions += 1
  { predicted_at: predicted, confidence: prediction_confidence, pattern: @pattern_type }
end

#prediction_accuracyObject



49
50
51
52
53
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 49

def prediction_accuracy
  return 0.0 if @total_predictions.zero?

  @accuracy_count.to_f / @total_predictions
end

#record_actual(actual_time) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 41

def record_actual(actual_time)
  return unless @last_predicted

  error = (actual_time - @last_predicted).abs
  tolerance = (@mean_interval || 60) * 0.3
  @accuracy_count += 1 if error <= tolerance
end

#to_hObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 63

def to_h
  {
    domain:            @domain,
    event:             @event,
    pattern_type:      @pattern_type,
    mean_interval:     @mean_interval,
    observation_count: @observation_count,
    accuracy:          prediction_accuracy
  }
end