Class: Legion::Extensions::Temporal::Helpers::TemporalPattern
- Inherits:
-
Object
- Object
- Legion::Extensions::Temporal::Helpers::TemporalPattern
- Defined in:
- lib/legion/extensions/temporal/helpers/temporal_pattern.rb
Instance Attribute Summary collapse
-
#accuracy_count ⇒ Object
readonly
Returns the value of attribute accuracy_count.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#event ⇒ Object
readonly
Returns the value of attribute event.
-
#last_predicted ⇒ Object
readonly
Returns the value of attribute last_predicted.
-
#mean_interval ⇒ Object
readonly
Returns the value of attribute mean_interval.
-
#observation_count ⇒ Object
readonly
Returns the value of attribute observation_count.
-
#pattern_type ⇒ Object
readonly
Returns the value of attribute pattern_type.
-
#total_predictions ⇒ Object
readonly
Returns the value of attribute total_predictions.
Instance Method Summary collapse
- #add_observation(timestamps) ⇒ Object
- #bursty? ⇒ Boolean
-
#initialize(domain:, event:) ⇒ TemporalPattern
constructor
A new instance of TemporalPattern.
- #periodic? ⇒ Boolean
- #predict_next(from: Time.now.utc) ⇒ Object
- #prediction_accuracy ⇒ Object
- #record_actual(actual_time) ⇒ Object
- #to_h ⇒ Object
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_count ⇒ Object (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 |
#domain ⇒ Object (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 |
#event ⇒ Object (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_predicted ⇒ Object (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_interval ⇒ Object (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_count ⇒ Object (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_type ⇒ Object (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_predictions ⇒ Object (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() return if .size < 2 @intervals = compute_intervals() @observation_count = .size @mean_interval = @intervals.sum / @intervals.size.to_f @pattern_type = classify_pattern end |
#bursty? ⇒ Boolean
59 60 61 |
# File 'lib/legion/extensions/temporal/helpers/temporal_pattern.rb', line 59 def bursty? @pattern_type == :bursty end |
#periodic? ⇒ 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_accuracy ⇒ Object
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_h ⇒ Object
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 |