darkhelmet-darkext 0.3.2 → 0.4.0

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 +81 -60
  2. metadata +2 -2
@@ -64,78 +64,99 @@ class Array
64
64
 
65
65
  # Standardizes the array
66
66
  def standardize
67
+ self.clone.standardize!
68
+ end
69
+
70
+ # Destructive standardize
71
+ def standardize!
67
72
  m = self.mean.to_f
68
73
  rho = self.deviation.to_f
69
- self.map do |v|
74
+ self.map! do |v|
70
75
  (v.to_f - m) / rho
71
76
  end
72
77
  end
73
- end
74
78
 
75
- module Statistics
76
- # Finds the probability of a z-score
77
- def self.prob(z)
78
- p = Math.erf(z.abs/2.sqrt) / 2
79
- return 0.5 + p if 0 < z
80
- return 0.5 - p
79
+ def sum_of_squares
80
+ m = self.mean
81
+ self.map do |v|
82
+ (v - m).square
83
+ end.sum
81
84
  end
85
+ end
86
+ module Darkext
87
+ module Darkext::Statistics
88
+ # Finds the probability of a z-score
89
+ def self.prob(z)
90
+ p = Math.erf(z.abs/2.sqrt) / 2
91
+ return 0.5 + p if 0 < z
92
+ return 0.5 - p
93
+ end
82
94
 
83
- # Finds the zscore of a probability
84
- def self.zscore(p, epsilon = 0.00000000000001)
85
- return -1 if (1 < p || 0 > p)
86
- minz, maxz = -6, 6
87
- zscore = 0.5
88
- prob = 0
89
- while (maxz - minz) > epsilon
90
- prob = prob(zscore)
91
- if prob > p then maxz = zscore else minz = zscore end
92
- zscore = (maxz + minz) * 0.5
95
+ # Finds the zscore of a probability
96
+ def self.zscore(p, epsilon = 0.00000000000001)
97
+ return -1 if (1 < p || 0 > p)
98
+ minz, maxz = -6, 6
99
+ zscore = 0.5
100
+ prob = 0
101
+ while (maxz - minz) > epsilon
102
+ prob = prob(zscore)
103
+ if prob > p then maxz = zscore else minz = zscore end
104
+ zscore = (maxz + minz) * 0.5
105
+ end
106
+ return zscore
93
107
  end
94
- return zscore
95
- end
96
108
 
97
- # Finds a two tail p-val for a high/low array
98
- def self.p_val(r, n = 30, rho = 1, mu = r.mean)
99
- probs = r.map do |x|
100
- (x - mu) / (rho / n.sqrt)
101
- end.map do |x|
102
- Statistics.prob(x)
109
+ # Finds a two tail p-val for a high/low array
110
+ def self.p_val(r, n = 30, rho = 1, mu = r.mean)
111
+ probs = r.map do |x|
112
+ (x - mu) / (rho / n.sqrt)
113
+ end.map do |x|
114
+ Statistics.prob(x)
115
+ end
116
+ return 1 - (probs[1] - probs[0])
103
117
  end
104
- return 1 - (probs[1] - probs[0])
105
- end
106
118
 
107
- module Statistics::Regression
108
- # Do a least squares linear regression on the two sets of x's and y's
109
- # Returns a hash containing many relevant values
110
- # * n (:n)
111
- # * B_1 (:b_1)
112
- # * B_0 (:b_0)
113
- # * predicted values (:predicted)
114
- # * residuals (:residuals)
115
- # * SS_E (:ss_e)
116
- # * unbiased estimator (:estimator)
117
- # * the equation as a lambda (:equation)
118
- # Raises an argument error if the arguments are not the same size
119
- def self.least_squares(xs,ys)
120
- raise ArgumentError("Arguments must be of equal size") if xs.size != ys.size
121
- n = xs.size
122
- b_1 = (xs.zip(ys).map(&:*).sum - ((ys.sum * xs.sum)/n))/(xs.map(&:square).sum - (xs.sum.square/n))
123
- b_0 = ys.mean - b_1 * xs.mean
124
- equation = lambda { |x| b_0 + b_1 * x }
125
- predicted = xs.map(&lambda)
126
- residuals = ys.zip(predicted).map(&:-)
127
- ss_e = residuals.map(&:square).sum
128
- estimator = ss_e/(n - 2)
129
- reg = Hash.new
130
- reg[:n] = n
131
- reg[:b_1] = b_1
132
- reg[:b_0] = b_0
133
- reg[:predicted] = predicted
134
- reg[:residuals] = residuals
135
- reg[:ss_e] = ss_e
136
- reg[:estimator] = estimator
137
- reg[:equation] = equation
138
- return reg
119
+ module Darkext::Statistics::Regression
120
+ # Do a least squares linear regression on the two sets of x's and y's
121
+ # Returns a hash containing many relevant values
122
+ # * n (:n)
123
+ # * B_1 (:b_1)
124
+ # * B_0 (:b_0)
125
+ # * predicted values (:predicted)
126
+ # * residuals (:residuals)
127
+ # * SSE (:ss_e)
128
+ # * SST (:ss_t)
129
+ # * R^2 (:r_2)
130
+ # * R (:r)
131
+ # * unbiased estimator (:estimator)
132
+ # * the equation as a lambda (:equation)
133
+ # Raises an argument error if the arguments are not the same size
134
+ def self.least_squares(xs,ys)
135
+ raise ArgumentError("Arguments must be of equal size") if xs.size != ys.size
136
+ n = xs.size
137
+ b_1 = (xs.zip(ys).map(&:product).sum - ((ys.sum * xs.sum)/n))/(xs.map(&:square).sum - (xs.sum.square/n))
138
+ b_0 = ys.mean - b_1 * xs.mean
139
+ equation = lambda { |x| b_0 + b_1 * x }
140
+ predicted = xs.map(&equation)
141
+ residuals = ys.zip(predicted).map { |y| y.shift - y.shift }
142
+ ss_e = residuals.map(&:square).sum
143
+ ss_t = ys.sum_of_squares
144
+ estimator = ss_e/(n - 2)
145
+ r_2 = 1 - (ss_e/ss_t)
146
+ r = r_2.sqrt
147
+ reg = {:n => n,
148
+ :b_1 => b_1,
149
+ :b_0 => b_0,
150
+ :predicted => predicted,
151
+ :residuals => residuals,
152
+ :ss_e => ss_e,
153
+ :ss_t => ss_t,
154
+ :estimator => estimator,
155
+ :equation => equation,
156
+ :r_2 => r_2,
157
+ :r => r}
158
+ return reg
159
+ end
139
160
  end
140
161
  end
141
162
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darkhelmet-darkext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Huckstep
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain:
11
11
  - /home/helmet/.gem/gem-public_cert.pem
12
- date: 2008-11-26 00:00:00 -08:00
12
+ date: 2008-12-02 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency