darkhelmet-darkext 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/darkext/statistics.rb +81 -60
- metadata +2 -2
data/lib/darkext/statistics.rb
CHANGED
@@ -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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
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
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
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
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
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.
|
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-
|
12
|
+
date: 2008-12-02 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|