Class: PlotStatistics::MonteCarlo

Inherits:
Object
  • Object
show all
Defined in:
lib/plot_statistics/monte_carlo.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ MonteCarlo

Returns a new instance of MonteCarlo.



5
6
7
# File 'lib/plot_statistics/monte_carlo.rb', line 5

def initialize(params={})
  @plots = params[:plots]
end

Instance Attribute Details

#plotsObject

Returns the value of attribute plots.



3
4
5
# File 'lib/plot_statistics/monte_carlo.rb', line 3

def plots
  @plots
end

#statsObject

Returns the value of attribute stats.



3
4
5
# File 'lib/plot_statistics/monte_carlo.rb', line 3

def stats
  @stats
end

Class Method Details

.new_bivariate(actual_plot, number_of_plots = 500.0) ⇒ Object



14
15
16
17
# File 'lib/plot_statistics/monte_carlo.rb', line 14

def self.new_bivariate(actual_plot, number_of_plots=500.0)
  plots = (1..number_of_plots).map { ClamPlot.create_random_from(actual_plot).bivariate_analysis }
  new(:plots => plots)
end

.new_univariate(number_of_clams, number_of_plots = 500.0) ⇒ Object



9
10
11
12
# File 'lib/plot_statistics/monte_carlo.rb', line 9

def self.new_univariate(number_of_clams, number_of_plots=500.0)
  plots = (1..number_of_plots).map { ClamPlot.create_random(number_of_clams).univariate_analysis }
  new(:plots => plots)
end

Instance Method Details

#average_stat(radius, stat) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/plot_statistics/monte_carlo.rb', line 64

def average_stat(radius, stat)
  sum = plots.inject(0.0) do |sum, plot|
    plot.stats.send(stat)[radius]
  end

  sum / number_of_plots
end

#bivariate_analysisObject



19
20
21
22
23
24
25
26
# File 'lib/plot_statistics/monte_carlo.rb', line 19

def bivariate_analysis
  self.stats = OpenStruct.new(:mean        => OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => []),
                              :upper_limit => OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => []),
                              :lower_limit => OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => []))
  calculate_means([:k_ts, :dead_k_ts, :l_ts])
  calculate_limits([:k_ts, :dead_k_ts, :l_ts])
  self
end

#calculate_limits(limits_for) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/plot_statistics/monte_carlo.rb', line 45

def calculate_limits(limits_for)
  (0...ClamPlot::MAX_RADIUS).each do |radius|
    limits_for.each do |stat|
      threshold = standard_deviation_stat(radius, stat) * 2.0

      stats.upper_limit.send(stat) << (stats.mean.send(stat)[radius] + threshold)
      stats.lower_limit.send(stat) << (stats.mean.send(stat)[radius] - threshold)
    end
  end
end

#calculate_means(means_for) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/plot_statistics/monte_carlo.rb', line 37

def calculate_means(means_for)
  (0...ClamPlot::MAX_RADIUS).each do |radius|
    means_for.each do |stat|
      stats.mean.send(stat) << average_stat(radius, stat)
    end
  end
end

#number_of_plotsObject



72
73
74
# File 'lib/plot_statistics/monte_carlo.rb', line 72

def number_of_plots
  plots.size
end

#standard_deviation_stat(radius, stat) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/plot_statistics/monte_carlo.rb', line 56

def standard_deviation_stat(radius, stat)
  sum = plots.inject(0.0) do |sum, plot|
    (plot.stats.send(stat)[radius] - stats.mean.send(stat)[radius]) ** 2
  end

  Math.sqrt(sum / number_of_plots)
end

#univariate_analysisObject



28
29
30
31
32
33
34
35
# File 'lib/plot_statistics/monte_carlo.rb', line 28

def univariate_analysis
  self.stats = OpenStruct.new(:mean        => OpenStruct.new(:k_ts => [], :l_ts => []),
                              :upper_limit => OpenStruct.new(:k_ts => [], :l_ts => []),
                              :lower_limit => OpenStruct.new(:k_ts => [], :l_ts => []))
  calculate_means([:k_ts, :l_ts])
  calculate_limits([:k_ts, :l_ts])
  self
end