Class: PlotStatistics::ClamPlot

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

Constant Summary collapse

AREA_OF_PLOT =
10_000
MAX_RADIUS =
50
PLOT_CORNERS =
[[0,0], [0,100], [100,0], [100,100]]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clams, dead_clams = []) ⇒ ClamPlot

Returns a new instance of ClamPlot.



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

def initialize(clams, dead_clams=[])
  @clams = clams
  @dead_clams = dead_clams
end

Instance Attribute Details

#clamsObject

Returns the value of attribute clams.



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

def clams
  @clams
end

#dead_clamsObject

Returns the value of attribute dead_clams.



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

def dead_clams
  @dead_clams
end

#statsObject

Returns the value of attribute stats.



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

def stats
  @stats
end

Class Method Details

.create_random(number_of_clams) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/plot_statistics/clam_plot.rb', line 29

def self.create_random(number_of_clams)
  clams = []
  while clams.size < 100 do
    new_clam = Clam.create_random
    next if clams.any? { |clam| clam == new_clam }
    clams << new_clam
  end
  clams = (1..number_of_clams).map { Clam.create_random }
  new(clams)
end

.create_random_from(clam_plot) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/plot_statistics/clam_plot.rb', line 52

def self.create_random_from(clam_plot)
  number_dead = clam_plot.dead_clams.size
  all_clams = clam_plot.dead_clams | clam_plot.clams
  dead_clams = all_clams.shuffle.slice(0...number_dead)
  live_clams = all_clams - dead_clams
  new(live_clams, dead_clams)
end

.create_regular(distance = 5) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/plot_statistics/clam_plot.rb', line 40

def self.create_regular(distance=5)
  clams = []
  (0..100).each do |x|
    (0..100).each do |y|
      mod_x = x % distance
      mod_y = y % distance
      clams << Clam.new(:x => x, :y => y) if mod_x == 0 && mod_y == 0
    end
  end
  new(clams)
end

Instance Method Details

#bivariate_analysisObject



21
22
23
24
25
26
27
# File 'lib/plot_statistics/clam_plot.rb', line 21

def bivariate_analysis
  self.stats = OpenStruct.new(:k_ts => [], :dead_k_ts => [], :l_ts => [])
  setup_clam_distances
  setup_clam_distances(:dead_clams)
  calculate_bivariate_stats
  self
end

#calculate_bivariate_l_t(live_k_t, dead_k_t, radius) ⇒ Object



121
122
123
# File 'lib/plot_statistics/clam_plot.rb', line 121

def calculate_bivariate_l_t(live_k_t, dead_k_t, radius)
  radius - Math.sqrt( (live_k_t - dead_k_t).abs / Math::PI)
end

#calculate_bivariate_statsObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/plot_statistics/clam_plot.rb', line 79

def calculate_bivariate_stats
  (1..MAX_RADIUS).each do |radius|

    live_k_t = calculate_k_t(radius)
    dead_k_t = calculate_k_t(radius, :dead_clams)
    l_t      = calculate_bivariate_l_t(live_k_t, dead_k_t, radius)

    stats.k_ts << live_k_t
    stats.dead_k_ts << dead_k_t
    stats.l_ts << l_t
  end
end

#calculate_k_t(radius, clam_type = 'clams') ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/plot_statistics/clam_plot.rb', line 103

def calculate_k_t(radius, clam_type='clams')
  sums = send(clam_type).inject(0.0) do |sum, clam|
    clams_inside_circle = clam.distances.select { |distance| distance <= radius }

    circle_proportion = Circle.new(:clam => clam, :radius => radius).proportion_inside_plot
    reciprocal_proportion = 1.0 / circle_proportion

    inner_sum = reciprocal_proportion * clams_inside_circle.size
    sum += inner_sum
  end

  AREA_OF_PLOT * sums / (number_of_clams ** 2)
end

#calculate_univariate_l_t(k_t, radius) ⇒ Object



117
118
119
# File 'lib/plot_statistics/clam_plot.rb', line 117

def calculate_univariate_l_t(k_t, radius)
  radius - Math.sqrt( k_t / Math::PI)
end

#calculate_univariate_statsObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/plot_statistics/clam_plot.rb', line 92

def calculate_univariate_stats
  (1..MAX_RADIUS).each do |radius|

    k_t = calculate_k_t(radius)
    l_t = calculate_univariate_l_t(k_t, radius)

    stats.k_ts << k_t
    stats.l_ts << l_t
  end
end

#distance_between_clams(clam1, clam2) ⇒ Object



75
76
77
# File 'lib/plot_statistics/clam_plot.rb', line 75

def distance_between_clams(clam1, clam2)
  Math.sqrt((clam1.x - clam2.x) ** 2 + (clam1.y - clam2.y) ** 2)
end

#number_of_clamsObject



60
61
62
# File 'lib/plot_statistics/clam_plot.rb', line 60

def number_of_clams
  clams.size.to_f
end

#reset_clam_distancesObject



125
126
127
# File 'lib/plot_statistics/clam_plot.rb', line 125

def reset_clam_distances
  (clams | dead_clams).each { |clam| clam.reset_distances }
end

#setup_clam_distances(clam_type = 'clams') ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/plot_statistics/clam_plot.rb', line 64

def setup_clam_distances(clam_type='clams')
  reset_clam_distances
  send(clam_type).each_with_index do |reference_clam, i|
    send(clam_type)[(i + 1)..-1].each do |other_clam|
      distance = distance_between_clams(reference_clam, other_clam)
      reference_clam.distances << distance
      other_clam.distances << distance
    end
  end
end

#univariate_analysisObject



14
15
16
17
18
19
# File 'lib/plot_statistics/clam_plot.rb', line 14

def univariate_analysis
  self.stats = OpenStruct.new(:k_ts => [], :l_ts => [])
  setup_clam_distances
  calculate_univariate_stats
  self
end