Class: Autotuner::DataStructure::DataPoints

Inherits:
Object
  • Object
show all
Defined in:
lib/autotuner/data_structure/data_points.rb

Constant Summary collapse

STABLE_RATIO =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ DataPoints

Returns a new instance of DataPoints.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/autotuner/data_structure/data_points.rb', line 10

def initialize(capacity)
  raise "Capacity must be even" if capacity.odd?

  @samples = Array.new(capacity)

  @temp_sample = 0
  @temp_sample_count = 0

  @length = 0
  @compression_ratio = 1
end

Instance Attribute Details

#compression_ratioObject (readonly)

Returns the value of attribute compression_ratio.



8
9
10
# File 'lib/autotuner/data_structure/data_points.rb', line 8

def compression_ratio
  @compression_ratio
end

#lengthObject (readonly)

Returns the value of attribute length.



8
9
10
# File 'lib/autotuner/data_structure/data_points.rb', line 8

def length
  @length
end

#samplesObject (readonly)

Returns the value of attribute samples.



8
9
10
# File 'lib/autotuner/data_structure/data_points.rb', line 8

def samples
  @samples
end

Instance Method Details

#correlation(y) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/autotuner/data_structure/data_points.rb', line 51

def correlation(y)
  raise "Length not equal" unless length == y.length
  raise "Compression ratio not equal" unless compression_ratio == y.compression_ratio

  # Find the correlation between this and y
  # https://www.mathsisfun.com/data/correlation.html
  sum_x = 0
  sum_y = 0
  sum_x_2 = 0
  sum_y_2 = 0
  sum_x_y = 0
  length.times do |i|
    x_val = @samples[i]
    y_val = y.samples[i]

    sum_x += x_val
    sum_y += y_val
    sum_x_2 += x_val**2
    sum_y_2 += y_val**2
    sum_x_y += x_val * y_val
  end

  ((length * sum_x_y) - (sum_x * sum_y)).to_f / \
    (Math.sqrt((length * sum_x_2) - (sum_x**2)) * Math.sqrt((length * sum_y_2) - (sum_y**2)))
end

#debug_stateObject



77
78
79
80
81
82
83
84
85
# File 'lib/autotuner/data_structure/data_points.rb', line 77

def debug_state
  {
    samples: @samples,
    length: @length,
    compression_ratio: @compression_ratio,
    temp_sample: @temp_sample,
    temp_sample_count: @temp_sample_count,
  }
end

#insert(value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/autotuner/data_structure/data_points.rb', line 22

def insert(value)
  compress if @length == @samples.length

  @temp_sample += value
  @temp_sample_count += 1

  return unless @temp_sample_count == @compression_ratio

  @samples[@length] = @temp_sample / @temp_sample_count

  @length += 1
  @temp_sample = 0
  @temp_sample_count = 0
end

#plateaued?(delta = 0.1) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/autotuner/data_structure/data_points.rb', line 44

def plateaued?(delta = 0.1)
  # Not enough data until filled
  return false if @compression_ratio == 1

  stable_region_slope.abs <= delta
end

#stable_region_slopeObject



37
38
39
40
41
42
# File 'lib/autotuner/data_structure/data_points.rb', line 37

def stable_region_slope
  # Find line of best fit for the last 50%
  range = (@length * STABLE_RATIO).to_i...@length

  slope(range)
end