plot_statistics 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Asa Wilson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = plot_statistics
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Asa Wilson. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "plot_statistics"
8
+ gem.summary = %Q{Statistics on clam plots}
9
+ gem.description = %Q{This is a gem I built for my wife to analyze various clam plots.}
10
+ gem.email = "acvwilson@gmail.com"
11
+ gem.homepage = "http://github.com/acvwilson/plot_statistics"
12
+ gem.authors = ["Asa Wilson"]
13
+
14
+ gem.files.include %w(lib/plot_statistics/*.rb)
15
+
16
+ gem.add_development_dependency "rspec", ">= 1.2.9"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ require 'rake/rdoctask'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "plot_statistics #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'plot_statistics'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ gem 'plot_statistics'
8
+ require 'plot_statistics'
9
+ end
10
+
11
+ ARGV.each do |file_name|
12
+ file = File.new(file_name)
13
+
14
+ puts "\n"
15
+ puts '##########################'
16
+ puts "# #{file_name}"
17
+ puts '##########################'
18
+ puts "\n"
19
+
20
+ lines = file.readlines
21
+
22
+ clams = lines.map do |line|
23
+ x, y = line.strip.split("\t")
24
+ PlotStatistics::Clam.new(:x => x.to_i, :y => y.to_i)
25
+ end
26
+
27
+ actual_plot = PlotStatistics::ClamPlot.new(clams)
28
+ monte_carlo = PlotStatistics::MonteCarlo.new(actual_plot.number_of_clams)
29
+
30
+ puts actual_plot.inspect
31
+ puts monte_carlo.inspect
32
+ end
@@ -0,0 +1,7 @@
1
+ class PlotStatistics
2
+ require 'ostruct'
3
+
4
+ require File.dirname(__FILE__) + '/plot_statistics/clam.rb'
5
+ require File.dirname(__FILE__) + '/plot_statistics/clam_plot'
6
+ require File.dirname(__FILE__) + '/plot_statistics/monte_carlo'
7
+ end
@@ -0,0 +1,15 @@
1
+ class PlotStatistics
2
+ class Clam
3
+ attr_accessor :x, :y, :distances
4
+
5
+ def initialize(params={})
6
+ @x = params[:x]
7
+ @y = params[:y]
8
+ @distances = params[:distances] || []
9
+ end
10
+
11
+ def self.create_random
12
+ new( :x => rand(100), :y => rand(100) )
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,59 @@
1
+ class PlotStatistics
2
+ class ClamPlot
3
+ attr_accessor :clams, :stats
4
+
5
+ AREA_OF_PLOT = 10_000
6
+ MAX_RADIUS = 50
7
+
8
+ def initialize(clams)
9
+ @clams = clams
10
+ setup_clam_distances(@clams)
11
+ @stats = OpenStruct.new(:k_ts => [], :l_ts => [])
12
+ calculate_stats
13
+ end
14
+
15
+ def self.create_random(number_of_clams)
16
+ clams = (1..number_of_clams).map { Clam.create_random }
17
+ new(clams)
18
+ end
19
+
20
+ def number_of_clams
21
+ clams.size
22
+ end
23
+
24
+ def setup_clam_distances(clams)
25
+ clams.each_with_index do |reference_clam, i|
26
+ # next if (clams.size - 1) < i
27
+ clams[(i + 1)..-1].each do |other_clam|
28
+ distance = distance_between_clams(reference_clam, other_clam)
29
+ reference_clam.distances << distance
30
+ other_clam.distances << distance
31
+ end
32
+ end
33
+ end
34
+
35
+ def distance_between_clams(clam1, clam2)
36
+ Math.sqrt((clam1.x - clam2.x) ** 2 + (clam1.y - clam2.y) ** 2)
37
+ end
38
+
39
+ def calculate_stats
40
+ (1..MAX_RADIUS).each do |radius|
41
+ circle_proportion = 1.0
42
+
43
+ sums = clams.inject(0) do |sum, clam|
44
+ clams_inside_circle = clam.distances.select { |distance| distance <= radius }
45
+
46
+ clams_inside_circle.inject(0) do |sum, inside_clam|
47
+ ( circle_proportion * 1.0 ) / ( number_of_clams ** 2 )
48
+ end
49
+ end
50
+
51
+ k_t = AREA_OF_PLOT * sums
52
+ l_t = radius - Math.sqrt( k_t / Math::PI)
53
+
54
+ stats.k_ts << k_t
55
+ stats.l_ts << l_t
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,69 @@
1
+ class PlotStatistics
2
+ class MonteCarlo
3
+ attr_accessor :plots, :stats
4
+
5
+ def initialize(number_of_clams, number_of_plots=100)
6
+ @plots = (1..number_of_plots).map { ClamPlot.create_random number_of_clams }
7
+ @stats = OpenStruct.new(:mean => OpenStruct.new(:k_ts => [], :l_ts => []),
8
+ :upper_limit => OpenStruct.new(:k_ts => [], :l_ts => []),
9
+ :lower_limit => OpenStruct.new(:k_ts => [], :l_ts => []))
10
+ calculate_means
11
+ calculate_limits
12
+ end
13
+
14
+ def calculate_means
15
+ (0...ClamPlot::MAX_RADIUS).each do |radius|
16
+ stats.mean.k_ts << average_k(radius)
17
+ stats.mean.l_ts << average_l(radius)
18
+ end
19
+ end
20
+
21
+ def calculate_limits
22
+ (0...ClamPlot::MAX_RADIUS).each do |radius|
23
+ threshold_k = standard_deviation_k(radius) * 2
24
+ threshold_l = standard_deviation_l(radius) * 2
25
+
26
+ stats.upper_limit.k_ts << (stats.mean.k_ts[radius] + threshold_k)
27
+ stats.upper_limit.l_ts << (stats.mean.l_ts[radius] + threshold_l)
28
+ stats.lower_limit.k_ts << (stats.mean.k_ts[radius] - threshold_k)
29
+ stats.lower_limit.l_ts << (stats.mean.l_ts[radius] - threshold_l)
30
+ end
31
+ end
32
+
33
+ def standard_deviation_k(radius)
34
+ sum = plots.inject(0) do |sum, plot|
35
+ (plot.stats.k_ts[radius] - stats.mean.k_ts[radius]) ** 2
36
+ end
37
+
38
+ Math.sqrt(sum / number_of_plots)
39
+ end
40
+
41
+ def standard_deviation_l(radius)
42
+ sum = plots.inject(0) do |sum, plot|
43
+ (plot.stats.l_ts[radius] - stats.mean.l_ts[radius]) ** 2
44
+ end
45
+
46
+ Math.sqrt(sum / number_of_plots)
47
+ end
48
+
49
+ def number_of_plots
50
+ plots.size
51
+ end
52
+
53
+ def average_k(radius)
54
+ sum = plots.inject(0) do |sum, plot|
55
+ plot.stats.k_ts[radius]
56
+ end
57
+
58
+ sum / number_of_plots
59
+ end
60
+
61
+ def average_l(radius)
62
+ sum = plots.inject(0) do |sum, plot|
63
+ plot.stats.l_ts[radius]
64
+ end
65
+
66
+ sum / number_of_plots
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{plot_statistics}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Asa Wilson"]
12
+ s.date = %q{2010-03-03}
13
+ s.default_executable = %q{plot_statistics}
14
+ s.description = %q{This is a gem I built for my wife to analyze various clam plots.}
15
+ s.email = %q{acvwilson@gmail.com}
16
+ s.executables = ["plot_statistics"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/plot_statistics",
29
+ "lib/plot_statistics.rb",
30
+ "lib/plot_statistics/clam.rb",
31
+ "lib/plot_statistics/clam_plot.rb",
32
+ "lib/plot_statistics/monte_carlo.rb",
33
+ "plot_statistics.gemspec",
34
+ "spec/plot_statistics_spec.rb",
35
+ "spec/spec.opts",
36
+ "spec/spec_helper.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/acvwilson/plot_statistics}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.6}
42
+ s.summary = %q{Statistics on clam plots}
43
+ s.test_files = [
44
+ "spec/plot_statistics_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "PlotStatistics" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'plot_statistics'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plot_statistics
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Asa Wilson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-03 00:00:00 -05:00
18
+ default_executable: plot_statistics
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 9
31
+ version: 1.2.9
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: This is a gem I built for my wife to analyze various clam plots.
35
+ email: acvwilson@gmail.com
36
+ executables:
37
+ - plot_statistics
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - bin/plot_statistics
51
+ - lib/plot_statistics.rb
52
+ - lib/plot_statistics/clam.rb
53
+ - lib/plot_statistics/clam_plot.rb
54
+ - lib/plot_statistics/monte_carlo.rb
55
+ - plot_statistics.gemspec
56
+ - spec/plot_statistics_spec.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/acvwilson/plot_statistics
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.3.6
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Statistics on clam plots
89
+ test_files:
90
+ - spec/plot_statistics_spec.rb
91
+ - spec/spec_helper.rb