Class: MOSAIK::Graph::Statistics
- Inherits:
-
Object
- Object
- MOSAIK::Graph::Statistics
- Defined in:
- lib/mosaik/graph/statistics.rb
Overview
Compute statistics
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(options, graph) ⇒ Statistics
constructor
A new instance of Statistics.
Constructor Details
#initialize(options, graph) ⇒ Statistics
Returns a new instance of Statistics.
13 14 15 16 |
# File 'lib/mosaik/graph/statistics.rb', line 13 def initialize(, graph) @options = @graph = graph end |
Instance Attribute Details
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
11 12 13 |
# File 'lib/mosaik/graph/statistics.rb', line 11 def graph @graph end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/mosaik/graph/statistics.rb', line 11 def @options end |
Instance Method Details
#call ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mosaik/graph/statistics.rb', line 18 def call # Compute metric statistics metrics = [:metrics].to_h do |metric| values = graph.clusters.each_value.map { |cluster| cluster.attributes[metric] } # Drop zero values values = values.reject { |v| v.nil? || v.zero? } mean = values.any? ? (values.sum.to_f / values.size) : 0.0 variance = values.any? ? (values.sum { |v| (v - mean)**2 } / values.size) : 0.0 statistics = { min: values.min || 0.0, max: values.max || 0.0, mean:, variance:, stdev: Math.sqrt(variance), q1: percentile(values, 25) || 0.0, q2: percentile(values, 50) || 0.0, q3: percentile(values, 75) || 0.0, } debug "Statistics for #{metric}: #{statistics.map { |k, v| "#{k} = #{v&.round(2)}" }.join(', ')}" [metric, statistics] end # Compute cluster statistics mean = graph.clusters.values.any? ? (graph.clusters.values.sum { |cluster| cluster.vertices.size }.to_f / graph.clusters.size) : 0.0 variance = graph.clusters.values.any? ? (graph.clusters.values.sum { |cluster| (cluster.vertices.size - mean)**2 } / graph.clusters.size) : 0.0 metrics[:clusters] = { count: graph.clusters.size, min: graph.clusters.values.map { |cluster| cluster.vertices.size }.min, max: graph.clusters.values.map { |cluster| cluster.vertices.size }.max, mean:, variance:, stdev: Math.sqrt(variance), size: graph.clusters.values.map { |cluster| cluster.vertices.size }.sort, } # Return statistics metrics.deep_stringify_keys end |