darkhelmet-darkext 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/darkext/statistics.rb +40 -21
  2. metadata +4 -4
@@ -38,24 +38,22 @@ class Array
38
38
  map.keys.select { |x| map[x] == max }
39
39
  end
40
40
 
41
- # Population variance
42
- def pvariance
43
- self.sum_of_squares.to_f / self.size.to_f
41
+ # Variance
42
+ def population_variance
43
+ self.sum_of_squares.to_f / (self.size).to_f
44
44
  end
45
45
 
46
- # Sample variance
47
- def svariance
46
+ def sample_variance
48
47
  self.sum_of_squares.to_f / (self.size - 1).to_f
49
48
  end
50
49
 
51
- # Population standard deviation
52
- def pdeviation
53
- self.pvariance.abs.sqrt
50
+ # Standard deviation
51
+ def population_deviation
52
+ self.population_variance.abs.sqrt
54
53
  end
55
54
 
56
- # Sample standard deviation
57
- def sdeviation
58
- self.svariance.abs.sqrt
55
+ def sample_deviation
56
+ self.sample_variance.abs.sqrt
59
57
  end
60
58
 
61
59
  # Randomly samples n elements
@@ -63,13 +61,24 @@ class Array
63
61
  (1..n).collect { self[rand(self.size)] }
64
62
  end
65
63
 
66
- # Makes a two sided confidence interval for the array
67
- # Percent must be 0 < percent < 1 for this to work properly
68
- def ci(percent = 0.95, rho = 1)
64
+ # Generates a confidence interval
65
+ def ci(opts = { })
66
+ opts.with_defaults!({ :percent => 0.95, :rho => 1, :type => :center })
69
67
  m = self.mean
70
- i = ((Darkext::Statistics::zscore((1 - percent) / 2) * rho) /
68
+ ret = Array.new
69
+ div = (opts[:type] == :center ? 2 : 1)
70
+ i = ((Darkext::Statistics::zscore((1 - percent) / div) * rho) /
71
71
  self.size.sqrt).abs
72
- [m - i, m + i]
72
+ case opts[:type]
73
+ when :center
74
+ ret << m - i
75
+ ret << m + i
76
+ when :upper
77
+ ret << m + i
78
+ when :lower
79
+ ret << m - i
80
+ end
81
+ return ret
73
82
  end
74
83
 
75
84
  # Standardizes the array
@@ -80,17 +89,27 @@ class Array
80
89
  # Destructive standardize
81
90
  def standardize!
82
91
  m = self.mean.to_f
83
- rho = self.deviation.to_f
84
- self.map! do |v|
85
- (v.to_f - m) / rho
86
- end
92
+ rho = self.sample_deviation.to_f
93
+ self.map! { |v| (v.to_f - m) / rho }
87
94
  end
88
95
 
89
96
  def sum_of_squares
90
97
  m = self.mean
91
98
  self.map { |v| v - m }.map(&:square).sum
92
99
  end
100
+
101
+ # Normalize the Array
102
+ def normalize
103
+ self.clone.normalize!
104
+ end
105
+
106
+ # Normalize the Array destructive
107
+ def normalize!
108
+ m = self.mean.to_f
109
+ self.map! { |v| v / m }
110
+ end
93
111
  end
112
+
94
113
  module Darkext
95
114
  module Darkext::Statistics
96
115
  # Finds the probability of a z-score
@@ -108,7 +127,7 @@ module Darkext
108
127
  prob = 0
109
128
  while (maxz - minz) > epsilon
110
129
  prob = prob(zscore)
111
- if prob > p then maxz = zscore else minz = zscore end
130
+ prob > p ? maxz = zscore : minz = zscore
112
131
  zscore = (maxz + minz) * 0.5
113
132
  end
114
133
  return zscore
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darkhelmet-darkext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Huckstep
8
8
  autorequire:
9
9
  bindir: bin
10
- cert_chain:
11
- - /home/helmet/.gem/gem-public_cert.pem
12
- date: 2008-12-07 00:00:00 -08:00
10
+ cert_chain: []
11
+
12
+ date: 2008-12-11 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency