tolerances 1.0.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.tar.gz.sig +3 -0
- data/History.txt +6 -0
- data/License.txt +263 -0
- data/Manifest.txt +13 -0
- data/README.txt +41 -0
- data/Rakefile +14 -0
- data/lib/quantiles.rb +57 -0
- data/lib/simple_stats.rb +102 -0
- data/lib/tolerance.rb +99 -0
- data/lib/tolerances.rb +60 -0
- data/test/test_quantiles.rb +99 -0
- data/test/test_simple_stats.rb +90 -0
- data/test/test_tolerance.rb +166 -0
- data/test/test_tolerances.rb +56 -0
- metadata +103 -0
- metadata.gz.sig +0 -0
data.tar.gz.sig
ADDED
data/History.txt
ADDED
data/License.txt
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
== LICENSE
|
|
2
|
+
|
|
3
|
+
NASA OPEN SOURCE AGREEMENT VERSION 1.3
|
|
4
|
+
|
|
5
|
+
THIS OPEN SOURCE AGREEMENT ("AGREEMENT") DEFINES THE RIGHTS OF USE,
|
|
6
|
+
REPRODUCTION, DISTRIBUTION, MODIFICATION AND REDISTRIBUTION OF CERTAIN
|
|
7
|
+
COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE UNITED STATES GOVERNMENT
|
|
8
|
+
AS REPRESENTED BY THE GOVERNMENT AGENCY LISTED BELOW ("GOVERNMENT
|
|
9
|
+
AGENCY"). THE UNITED STATES GOVERNMENT, AS REPRESENTED BY GOVERNMENT
|
|
10
|
+
AGENCY, IS AN INTENDED THIRD-PARTY BENEFICIARY OF ALL SUBSEQUENT
|
|
11
|
+
DISTRIBUTIONS OR REDISTRIBUTIONS OF THE SUBJECT SOFTWARE. ANYONE WHO
|
|
12
|
+
USES, REPRODUCES, DISTRIBUTES, MODIFIES OR REDISTRIBUTES THE SUBJECT
|
|
13
|
+
SOFTWARE, AS DEFINED HEREIN, OR ANY PART THEREOF, IS, BY THAT ACTION,
|
|
14
|
+
ACCEPTING IN FULL THE RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN
|
|
15
|
+
THIS AGREEMENT.
|
|
16
|
+
|
|
17
|
+
Government Agency: NASA Langley Research Center
|
|
18
|
+
Government Agency Original Software Designation: LAR-17546-1
|
|
19
|
+
Government Agency Original Software Title: Tolerance DSL
|
|
20
|
+
User Registration Requested: please see clause 3.F.
|
|
21
|
+
Government Agency Point of Contact: please see clause 5.F at end.
|
|
22
|
+
|
|
23
|
+
1. DEFINITIONS
|
|
24
|
+
|
|
25
|
+
A. "Contributor" means Government Agency, as the developer of the
|
|
26
|
+
Original Software, and any entity that makes a Modification.
|
|
27
|
+
|
|
28
|
+
B. "Covered Patents" mean patent claims licensable by a Contributor
|
|
29
|
+
that are necessarily infringed by the use or sale of its Modification
|
|
30
|
+
alone or when combined with the Subject Software.
|
|
31
|
+
|
|
32
|
+
C. "Display" means the showing of a copy of the Subject Software,
|
|
33
|
+
either directly or by means of an image, or any other device.
|
|
34
|
+
|
|
35
|
+
D. "Distribution" means conveyance or transfer of the Subject
|
|
36
|
+
Software, regardless of means, to another.
|
|
37
|
+
|
|
38
|
+
E. "Larger Work" means computer software that combines Subject
|
|
39
|
+
Software, or portions thereof, with software separate from the Subject
|
|
40
|
+
Software that is not governed by the terms of this Agreement.
|
|
41
|
+
|
|
42
|
+
F. "Modification" means any alteration of, including addition to or
|
|
43
|
+
deletion from, the substance or structure of either the Original
|
|
44
|
+
Software or Subject Software, and includes derivative works, as that
|
|
45
|
+
term is defined in the Copyright Statute, 17 USC 101. However, the
|
|
46
|
+
act of including Subject Software as part of a Larger Work does not in
|
|
47
|
+
and of itself constitute a Modification.
|
|
48
|
+
|
|
49
|
+
G. "Original Software" means the computer software first released
|
|
50
|
+
under this Agreement by Government Agency with Government Agency
|
|
51
|
+
designation NASA and entitled fUnit, including source code,
|
|
52
|
+
object code and accompanying documentation, if any.
|
|
53
|
+
|
|
54
|
+
H. "Recipient" means anyone who acquires the Subject Software under
|
|
55
|
+
this Agreement, including all Contributors.
|
|
56
|
+
|
|
57
|
+
I. "Redistribution" means Distribution of the Subject Software after a
|
|
58
|
+
Modification has been made.
|
|
59
|
+
|
|
60
|
+
J. "Reproduction" means the making of a counterpart, image or copy of
|
|
61
|
+
the Subject Software.
|
|
62
|
+
|
|
63
|
+
K. "Sale" means the exchange of the Subject Software for money or
|
|
64
|
+
equivalent value.
|
|
65
|
+
|
|
66
|
+
L. "Subject Software" means the Original Software, Modifications, or
|
|
67
|
+
any respective parts thereof.
|
|
68
|
+
|
|
69
|
+
M. "Use" means the application or employment of the Subject Software
|
|
70
|
+
for any purpose.
|
|
71
|
+
|
|
72
|
+
2. GRANT OF RIGHTS
|
|
73
|
+
|
|
74
|
+
A. Under Non-Patent Rights: Subject to the terms and conditions of
|
|
75
|
+
this Agreement, each Contributor, with respect to its own contribution
|
|
76
|
+
to the Subject Software, hereby grants to each Recipient a
|
|
77
|
+
non-exclusive, world-wide, royalty-free license to engage in the
|
|
78
|
+
following activities pertaining to the Subject Software:
|
|
79
|
+
|
|
80
|
+
1. Use
|
|
81
|
+
2. Distribution
|
|
82
|
+
3. Reproduction
|
|
83
|
+
4. Modification
|
|
84
|
+
5. Redistribution
|
|
85
|
+
6. Display
|
|
86
|
+
|
|
87
|
+
B. Under Patent Rights: Subject to the terms and conditions of this
|
|
88
|
+
Agreement, each Contributor, with respect to its own contribution to
|
|
89
|
+
the Subject Software, hereby grants to each Recipient under Covered
|
|
90
|
+
Patents a non-exclusive, world-wide, royalty-free license to engage in
|
|
91
|
+
the following activities pertaining to the Subject Software:
|
|
92
|
+
|
|
93
|
+
1. Use
|
|
94
|
+
2. Distribution
|
|
95
|
+
3. Reproduction
|
|
96
|
+
4. Sale
|
|
97
|
+
5. Offer for Sale
|
|
98
|
+
|
|
99
|
+
C. The rights granted under Paragraph B. also apply to the combination
|
|
100
|
+
of a Contributor's Modification and the Subject Software if, at the
|
|
101
|
+
time the Modification is added by the Contributor, the addition of
|
|
102
|
+
such Modification causes the combination to be covered by the Covered
|
|
103
|
+
Patents. It does not apply to any other combinations that include a
|
|
104
|
+
Modification.
|
|
105
|
+
|
|
106
|
+
D. The rights granted in Paragraphs A. and B. allow the Recipient to
|
|
107
|
+
sublicense those same rights. Such sublicense must be under the same
|
|
108
|
+
terms and conditions of this Agreement.
|
|
109
|
+
|
|
110
|
+
3. OBLIGATIONS OF RECIPIENT
|
|
111
|
+
|
|
112
|
+
A. Distribution or Redistribution of the Subject Software must be made
|
|
113
|
+
under this Agreement except for additions covered under paragraph 3H.
|
|
114
|
+
|
|
115
|
+
1. Whenever a Recipient distributes or redistributes the Subject
|
|
116
|
+
Software, a copy of this Agreement must be included with each copy
|
|
117
|
+
of the Subject Software; and
|
|
118
|
+
|
|
119
|
+
2. If Recipient distributes or redistributes the Subject Software in
|
|
120
|
+
any form other than source code, Recipient must also make the
|
|
121
|
+
source code freely available, and must provide with each copy of
|
|
122
|
+
the Subject Software information on how to obtain the source code
|
|
123
|
+
in a reasonable manner on or through a medium customarily used for
|
|
124
|
+
software exchange.
|
|
125
|
+
|
|
126
|
+
B. Each Recipient must ensure that the following copyright notice
|
|
127
|
+
appears prominently in the Subject Software:
|
|
128
|
+
|
|
129
|
+
Copyright 2007-2008 United States Government as represented by
|
|
130
|
+
NASA Langley Research Center. No copyright is claimed in
|
|
131
|
+
the United States under Title 17, U.S. Code. All Other Rights
|
|
132
|
+
Reserved.
|
|
133
|
+
|
|
134
|
+
C. Each Contributor must characterize its alteration of the Subject
|
|
135
|
+
Software as a Modification and must identify itself as the originator
|
|
136
|
+
of its Modification in a manner that reasonably allows subsequent
|
|
137
|
+
Recipients to identify the originator of the Modification. In
|
|
138
|
+
fulfillment of these requirements, Contributor must include a file
|
|
139
|
+
(e.g., a change log file) that describes the alterations made and the
|
|
140
|
+
date of the alterations, identifies Contributor as originator of the
|
|
141
|
+
alterations, and consents to characterization of the alterations as a
|
|
142
|
+
Modification, for example, by including a statement that the
|
|
143
|
+
Modification is derived, directly or indirectly, from Original
|
|
144
|
+
Software provided by Government Agency. Once consent is granted, it
|
|
145
|
+
may not thereafter be revoked.
|
|
146
|
+
|
|
147
|
+
D. A Contributor may add its own copyright notice to the Subject
|
|
148
|
+
Software. Once a copyright notice has been added to the Subject
|
|
149
|
+
Software, a Recipient may not remove it without the express permission
|
|
150
|
+
of the Contributor who added the notice.
|
|
151
|
+
|
|
152
|
+
E. A Recipient may not make any representation in the Subject Software
|
|
153
|
+
or in any promotional, advertising or other material that may be
|
|
154
|
+
construed as an endorsement by Government Agency or by any prior
|
|
155
|
+
Recipient of any product or service provided by Recipient, or that may
|
|
156
|
+
seek to obtain commercial advantage by the fact of Government Agency's
|
|
157
|
+
or a prior Recipient's participation in this Agreement.
|
|
158
|
+
|
|
159
|
+
F. In an effort to track usage and maintain accurate records of the
|
|
160
|
+
Subject Software, each Recipient, upon receipt of the Subject Software,
|
|
161
|
+
is requested to provide Government Agency, by e-mail to the Government
|
|
162
|
+
Agency Point of Contact listed in clause 5.F., the following information:
|
|
163
|
+
name, institution (if applicable), postal code, and country.
|
|
164
|
+
Recipient's name and personal information shall be used for statistical
|
|
165
|
+
purposes only. Once a Recipient makes a Modification available, it is
|
|
166
|
+
requested that the Recipient inform Government Agency, by e-mail to the
|
|
167
|
+
Government Agency Point of Contact listed in clause 5.F., how to
|
|
168
|
+
access the Modification.
|
|
169
|
+
|
|
170
|
+
G. Each Contributor represents that that its Modification is believed
|
|
171
|
+
to be Contributor's original creation and does not violate any
|
|
172
|
+
existing agreements, regulations, statutes or rules, and further that
|
|
173
|
+
Contributor has sufficient rights to grant the rights conveyed by this
|
|
174
|
+
Agreement.
|
|
175
|
+
|
|
176
|
+
H. A Recipient may choose to offer, and to charge a fee for, warranty,
|
|
177
|
+
support, indemnity and/or liability obligations to one or more other
|
|
178
|
+
Recipients of the Subject Software. A Recipient may do so, however,
|
|
179
|
+
only on its own behalf and not on behalf of Government Agency or any
|
|
180
|
+
other Recipient. Such a Recipient must make it absolutely clear that
|
|
181
|
+
any such warranty, support, indemnity and/or liability obligation is
|
|
182
|
+
offered by that Recipient alone. Further, such Recipient agrees to
|
|
183
|
+
indemnify Government Agency and every other Recipient for any
|
|
184
|
+
liability incurred by them as a result of warranty, support, indemnity
|
|
185
|
+
and/or liability offered by such Recipient.
|
|
186
|
+
|
|
187
|
+
I. A Recipient may create a Larger Work by combining Subject Software
|
|
188
|
+
with separate software not governed by the terms of this agreement and
|
|
189
|
+
distribute the Larger Work as a single product. In such case, the
|
|
190
|
+
Recipient must make sure Subject Software, or portions thereof,
|
|
191
|
+
included in the Larger Work is subject to this Agreement.
|
|
192
|
+
|
|
193
|
+
J. Notwithstanding any provisions contained herein, Recipient is
|
|
194
|
+
hereby put on notice that export of any goods or technical data from
|
|
195
|
+
the United States may require some form of export license from the
|
|
196
|
+
U.S. Government. Failure to obtain necessary export licenses may
|
|
197
|
+
result in criminal liability under U.S. laws. Government Agency
|
|
198
|
+
neither represents that a license shall not be required nor that, if
|
|
199
|
+
required, it shall be issued. Nothing granted herein provides any
|
|
200
|
+
such export license.
|
|
201
|
+
|
|
202
|
+
4. DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION
|
|
203
|
+
|
|
204
|
+
A. No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY
|
|
205
|
+
WARRANTY OF ANY KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY,
|
|
206
|
+
INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE
|
|
207
|
+
WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
|
|
208
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR FREEDOM FROM
|
|
209
|
+
INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR
|
|
210
|
+
FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO
|
|
211
|
+
THE SUBJECT SOFTWARE. THIS AGREEMENT DOES NOT, IN ANY MANNER,
|
|
212
|
+
CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT
|
|
213
|
+
OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY
|
|
214
|
+
OTHER APPLICATIONS RESULTING FROM USE OF THE SUBJECT SOFTWARE.
|
|
215
|
+
FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND LIABILITIES
|
|
216
|
+
REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE,
|
|
217
|
+
AND DISTRIBUTES IT "AS IS."
|
|
218
|
+
|
|
219
|
+
B. Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS
|
|
220
|
+
AGAINST THE UNITED STATES GOVERNMENT, ITS CONTRACTORS AND
|
|
221
|
+
SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE OF
|
|
222
|
+
THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES,
|
|
223
|
+
EXPENSES OR LOSSES ARISING FROM SUCH USE, INCLUDING ANY DAMAGES FROM
|
|
224
|
+
PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE SUBJECT
|
|
225
|
+
SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS THE UNITED
|
|
226
|
+
STATES GOVERNMENT, ITS CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY
|
|
227
|
+
PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY LAW. RECIPIENT'S SOLE
|
|
228
|
+
REMEDY FOR ANY SUCH MATTER SHALL BE THE IMMEDIATE, UNILATERAL
|
|
229
|
+
TERMINATION OF THIS AGREEMENT.
|
|
230
|
+
|
|
231
|
+
5. GENERAL TERMS
|
|
232
|
+
|
|
233
|
+
A. Termination: This Agreement and the rights granted hereunder will
|
|
234
|
+
terminate automatically if a Recipient fails to comply with these
|
|
235
|
+
terms and conditions, and fails to cure such noncompliance within
|
|
236
|
+
thirty (30) days of becoming aware of such noncompliance. Upon
|
|
237
|
+
termination, a Recipient agrees to immediately cease use and
|
|
238
|
+
distribution of the Subject Software. All sublicenses to the Subject
|
|
239
|
+
Software properly granted by the breaching Recipient shall survive any
|
|
240
|
+
such termination of this Agreement.
|
|
241
|
+
|
|
242
|
+
B. Severability: If any provision of this Agreement is invalid or
|
|
243
|
+
unenforceable under applicable law, it shall not affect the validity
|
|
244
|
+
or enforceability of the remainder of the terms of this Agreement.
|
|
245
|
+
|
|
246
|
+
C. Applicable Law: This Agreement shall be subject to United States
|
|
247
|
+
federal law only for all purposes, including, but not limited to,
|
|
248
|
+
determining the validity of this Agreement, the meaning of its
|
|
249
|
+
provisions and the rights, obligations and remedies of the parties.
|
|
250
|
+
|
|
251
|
+
D. Entire Understanding: This Agreement constitutes the entire
|
|
252
|
+
understanding and agreement of the parties relating to release of the
|
|
253
|
+
Subject Software and may not be superseded, modified or amended except
|
|
254
|
+
by further written agreement duly executed by the parties.
|
|
255
|
+
|
|
256
|
+
E. Binding Authority: By accepting and using the Subject Software
|
|
257
|
+
under this Agreement, a Recipient affirms its authority to bind the
|
|
258
|
+
Recipient to all terms and conditions of this Agreement and that that
|
|
259
|
+
Recipient hereby agrees to all terms and conditions herein.
|
|
260
|
+
|
|
261
|
+
F. Point of Contact: Any Recipient contact with Government Agency is
|
|
262
|
+
to be directed to the designated representative as follows:
|
|
263
|
+
Bil dot Kleb at NASA gov
|
data/Manifest.txt
ADDED
data/README.txt
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
= tolerances
|
|
2
|
+
|
|
3
|
+
* http://rubyforge.org/projects/nasarb
|
|
4
|
+
* http://nasarb.rubyforge.org/tolerances
|
|
5
|
+
* mailto:nasarb-developers@rubyforge.org
|
|
6
|
+
|
|
7
|
+
== DESCRIPTION:
|
|
8
|
+
|
|
9
|
+
Provides a Tolerance object and a Tolerances collection to facilitate
|
|
10
|
+
specifying and analyzing variabilities and uncertainties.
|
|
11
|
+
|
|
12
|
+
To aid in sampling for Monte-Carlos applications, this package also
|
|
13
|
+
includes some inverse cummulative probability distribution functions.
|
|
14
|
+
|
|
15
|
+
== FEATURES/PROBLEMS:
|
|
16
|
+
|
|
17
|
+
* Document uncertainties in situ with tolerance markup, e.g., 1.0+/-0.2
|
|
18
|
+
* Can be used by Monte Carlos black box techniques for propogating
|
|
19
|
+
uncertainty intervals or computing sensitivities.
|
|
20
|
+
|
|
21
|
+
== SYNOPSIS:
|
|
22
|
+
|
|
23
|
+
require 'rubygems'
|
|
24
|
+
require 'tolerances'
|
|
25
|
+
|
|
26
|
+
p Tolerance.parse("Uncertainty: 10+/-1")
|
|
27
|
+
|
|
28
|
+
#<Tolerance:0x52f788 @center=10.0, @distribution=nil, @format=nil,
|
|
29
|
+
@halfwidth=1.0, @tag="untagged_1", @type=:raw>
|
|
30
|
+
|
|
31
|
+
== REQUIREMENTS:
|
|
32
|
+
|
|
33
|
+
* rubygems
|
|
34
|
+
|
|
35
|
+
== INSTALL:
|
|
36
|
+
|
|
37
|
+
* sudo gem install tolerances
|
|
38
|
+
|
|
39
|
+
== LICENSE:
|
|
40
|
+
|
|
41
|
+
Tolerance is released under the NASA Open Source Agreement -- see License.txt[link:files/License_txt.html] for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# -*- ruby -*-
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'hoe'
|
|
5
|
+
require './lib/tolerances.rb'
|
|
6
|
+
|
|
7
|
+
Hoe.new('tolerances', Tolerances::VERSION) do |p|
|
|
8
|
+
p.rubyforge_name = 'nasarb'
|
|
9
|
+
p.remote_rdoc_dir = 'tolerances'
|
|
10
|
+
p.developer('Bil Kleb', 'Bil.Kleb@nasa.gov')
|
|
11
|
+
p.rsync_args = '-rpv --delete' # to perserve group permissions
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# vim: syntax=Ruby
|
data/lib/quantiles.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module Quantiles
|
|
2
|
+
|
|
3
|
+
# approximation from Peter J. Acklam
|
|
4
|
+
def normal(p)
|
|
5
|
+
(Float::EPSILON..1-Float::EPSILON).include? p or
|
|
6
|
+
raise "#{p}: outside probabiliy domain (0,1)"
|
|
7
|
+
a = [ -3.969683028665376e+01, 2.209460984245205e+02,
|
|
8
|
+
-2.759285104469687e+02, 1.383577518672690e+02,
|
|
9
|
+
-3.066479806614716e+01, 2.506628277459239e+00 ]
|
|
10
|
+
b = [ -5.447609879822406e+01, 1.615858368580409e+02,
|
|
11
|
+
-1.556989798598866e+02, 6.680131188771972e+01,
|
|
12
|
+
-1.328068155288572e+01 ]
|
|
13
|
+
c = [ -7.784894002430293e-03, -3.223964580411365e-01,
|
|
14
|
+
-2.400758277161838e+00, -2.549732539343734e+00,
|
|
15
|
+
4.374664141464968e+00, 2.938163982698783e+00 ]
|
|
16
|
+
d = [ 7.784695709041462e-03, 3.224671290700398e-01,
|
|
17
|
+
2.445134137142996e+00, 3.754408661907416e+00 ]
|
|
18
|
+
if p < 0.02425 then # lower region
|
|
19
|
+
q = Math.sqrt(-2*Math.log(p))
|
|
20
|
+
(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
|
|
21
|
+
((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1)
|
|
22
|
+
elsif p < 0.97575 then # central region (symmetrical)
|
|
23
|
+
q = p - 0.5
|
|
24
|
+
r = q*q
|
|
25
|
+
(((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
|
|
26
|
+
(((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1)
|
|
27
|
+
else # upper region
|
|
28
|
+
q = Math.sqrt(-2*Math.log(1-p))
|
|
29
|
+
-(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
|
|
30
|
+
((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
module_function :normal
|
|
34
|
+
|
|
35
|
+
def uniform(p)
|
|
36
|
+
(0..1).include? p or raise "#{p}: outside probability domain [0,1]"
|
|
37
|
+
p-0.5
|
|
38
|
+
end
|
|
39
|
+
module_function :uniform
|
|
40
|
+
|
|
41
|
+
def triangular(p)
|
|
42
|
+
(0..1).include? p or raise "#{p}: outside probability domain [0,1]"
|
|
43
|
+
if p < 0.5
|
|
44
|
+
-0.5 + Math.sqrt( 0.5*p )
|
|
45
|
+
else # symmetric reflection
|
|
46
|
+
0.5 - Math.sqrt( 0.5*(1-p) )
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
module_function :triangular
|
|
50
|
+
|
|
51
|
+
def cauchy(p)
|
|
52
|
+
(0..1).include? p or raise "#{p}: outside probability domain [0,1]"
|
|
53
|
+
Math.tan(Math::PI*(0.5-p))
|
|
54
|
+
end
|
|
55
|
+
module_function :cauchy
|
|
56
|
+
|
|
57
|
+
end
|
data/lib/simple_stats.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
##
|
|
2
|
+
# Statistical methods to be used with numeric Arrays
|
|
3
|
+
|
|
4
|
+
module SimpleStats
|
|
5
|
+
|
|
6
|
+
def mean
|
|
7
|
+
self.inject(0) { |sum, x| sum + x.to_f / self.size }
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def median
|
|
11
|
+
sorted = self.sort
|
|
12
|
+
mid = sorted.size / 2
|
|
13
|
+
sorted.size % 2 == 0 ? [ sorted[mid], sorted[mid-1] ].mean : sorted[mid]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def variance
|
|
17
|
+
mean = self.mean
|
|
18
|
+
self.inject(0) { |sum, x| sum + (x-mean)**2 } / self.size
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def standard_deviation
|
|
22
|
+
Math::sqrt(self.variance)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def coefficient_of_variation
|
|
26
|
+
mean = self.mean
|
|
27
|
+
mean == 0 ? 0.0 : self.standard_deviation/mean
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def skewness
|
|
31
|
+
mean, variance = self.mean, self.variance
|
|
32
|
+
variance == 0 ? 0.0 :
|
|
33
|
+
self.inject(0) { |sum, x| sum + (x-mean)**3 } /
|
|
34
|
+
self.size / variance**(3/2.0)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def kurtosis
|
|
38
|
+
mean, variance = self.mean, self.variance
|
|
39
|
+
variance == 0 ? 0.0 :
|
|
40
|
+
self.inject(0) { |sum, x| sum + (x-mean)**4 } /
|
|
41
|
+
self.size / variance**2
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def percentile(percentage)
|
|
45
|
+
self.sort[ (self.size-1)*percentage/100 + 0.5 ]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def histogram(num_of_bins=20)
|
|
49
|
+
min, max = self.min, self.max
|
|
50
|
+
bin_width = Float(max-min)/num_of_bins
|
|
51
|
+
bins = []
|
|
52
|
+
num_of_bins.times do
|
|
53
|
+
bins << (min..min+bin_width)
|
|
54
|
+
min += bin_width
|
|
55
|
+
end
|
|
56
|
+
histohash = {}
|
|
57
|
+
bins.each{ |b| histohash[b]=0 }
|
|
58
|
+
self.each do |e|
|
|
59
|
+
bin = bins.find{ |b| b.include? e }
|
|
60
|
+
bin = bins.last unless bin # round off
|
|
61
|
+
histohash[ bin ] += 1
|
|
62
|
+
end
|
|
63
|
+
histohash.sort do |x,y|
|
|
64
|
+
0.5*(x[0].begin+x[0].end) <=> 0.5*(y[0].begin+y[0].end)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def within?(tolerance,options={})
|
|
69
|
+
last_value = self.last
|
|
70
|
+
max_difference = self.inject(0) do |max,value|
|
|
71
|
+
difference = ( last_value - value ).abs
|
|
72
|
+
max >= difference ? max : difference
|
|
73
|
+
end
|
|
74
|
+
scale = options[:percent] ? 100.0/last_value : 1
|
|
75
|
+
scale = 0 if scale == 1.0/0
|
|
76
|
+
max_difference * scale <= tolerance
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
##
|
|
82
|
+
# Sample usage, complete with ASCII histogram plot
|
|
83
|
+
|
|
84
|
+
if __FILE__ == $0 then
|
|
85
|
+
require 'quantiles'
|
|
86
|
+
class Array; include SimpleStats; end
|
|
87
|
+
N = 10000
|
|
88
|
+
samples = []
|
|
89
|
+
(1..N).each{ |i| samples << QuantileFunctions.normal(0.5) }
|
|
90
|
+
puts " samples: #{N}"
|
|
91
|
+
puts " range: #{samples.min}...#{samples.max}"
|
|
92
|
+
puts " mean: #{samples.mean}"
|
|
93
|
+
puts " median: #{samples.median}"
|
|
94
|
+
puts "standard deviation: #{samples.standard_deviation}"
|
|
95
|
+
puts " skewness: #{samples.skewness}"
|
|
96
|
+
puts " kurtosis: #{samples.kurtosis}"
|
|
97
|
+
bins = Hash.new{ |h,k| h[k]=0 }
|
|
98
|
+
samples.each{ |s| bins[((s*10).round/10.0).to_s] += 1 }
|
|
99
|
+
bins.to_a.sort{ |x,y| x[0].to_f <=> y[0].to_f }.each do |pair|
|
|
100
|
+
puts pair.first.to_s.rjust(4) + " " + "*" * ( pair.last.to_f * 800 / N )
|
|
101
|
+
end
|
|
102
|
+
end
|
data/lib/tolerance.rb
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require 'scanf'
|
|
2
|
+
require 'quantiles'
|
|
3
|
+
|
|
4
|
+
class Tolerance
|
|
5
|
+
|
|
6
|
+
DEFAULTS = { :type=>:raw, :quantile=>nil, :format=>nil, :tag=>nil }
|
|
7
|
+
|
|
8
|
+
FLOAT = /[-+]*(?=\d|\.\d)\d*(?:\.\d*)?(?:[eE][-+]\d+)*/
|
|
9
|
+
|
|
10
|
+
REGEX = /(#{FLOAT}) # $1 : number
|
|
11
|
+
\s*\+\/-\s*
|
|
12
|
+
(#{FLOAT}) # $2 : tolerance
|
|
13
|
+
(%|o)? # $3 : type of tolerance (optional)
|
|
14
|
+
(U|N|I|T)? # $4 : quantile sampling function (optional)
|
|
15
|
+
(?:_(\d?[.]?\d*[#{Scanf::FormatString::SPECIFIERS}]))?
|
|
16
|
+
# $5 : format specifier (optional)
|
|
17
|
+
(?:\s*(["'])(.*?)\6)? # $7 : tag (funky stuff is "-matching)
|
|
18
|
+
/x
|
|
19
|
+
|
|
20
|
+
@@untagged = 0
|
|
21
|
+
|
|
22
|
+
attr_reader :center, :halfwidth
|
|
23
|
+
attr_reader :type, :quantile, :format, :tag
|
|
24
|
+
attr_accessor :sample_index
|
|
25
|
+
|
|
26
|
+
def initialize( center, halfwidth, options={} )
|
|
27
|
+
@center, @halfwidth = center.to_f, halfwidth.to_f
|
|
28
|
+
set_options DEFAULTS.merge(options)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.parse(string)
|
|
32
|
+
tolerances = []
|
|
33
|
+
string.scan(REGEX).each do |ctr, width, type, quantile, format, _, tag|
|
|
34
|
+
tolerances <<
|
|
35
|
+
Tolerance.new( ctr, width, :type => type, :quantile => quantile,
|
|
36
|
+
:format => format, :tag => tag )
|
|
37
|
+
end
|
|
38
|
+
case tolerances.size
|
|
39
|
+
when 0: nil
|
|
40
|
+
when 1: tolerances[0]
|
|
41
|
+
else tolerances
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def value_at( halfwidth_fraction )
|
|
46
|
+
case type
|
|
47
|
+
when :raw then
|
|
48
|
+
@center + halfwidth_fraction*@halfwidth
|
|
49
|
+
when :percent then
|
|
50
|
+
@center + halfwidth_fraction*@center*(@halfwidth/100)
|
|
51
|
+
when :order then
|
|
52
|
+
if @center < 0 then
|
|
53
|
+
-10**( log10(-@center) + halfwidth_fraction*@halfwidth )
|
|
54
|
+
elsif @center > 0 then
|
|
55
|
+
10**( log10( @center) + halfwidth_fraction*@halfwidth )
|
|
56
|
+
else # necessary for Ruby < 1.8.6
|
|
57
|
+
0
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def self.untagged=(index)
|
|
63
|
+
@@untagged = index
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def set_options(options)
|
|
69
|
+
@type = set_type options[:type]
|
|
70
|
+
@quantile = set_quantile options[:quantile]
|
|
71
|
+
@format = options[:format]
|
|
72
|
+
@tag = options[:tag] || "untagged_#{@@untagged+=1}"
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def set_type( type )
|
|
76
|
+
case type
|
|
77
|
+
when 'o' then :order
|
|
78
|
+
when '%' then :percent
|
|
79
|
+
when nil then DEFAULTS[:type]
|
|
80
|
+
when :raw,:order,:percent then type
|
|
81
|
+
else
|
|
82
|
+
raise "unknown tolerance type: >>#{type}<<"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def set_quantile( quantile )
|
|
87
|
+
case quantile
|
|
88
|
+
when 'U' then :uniform
|
|
89
|
+
when 'N' then :normal
|
|
90
|
+
when 'I' then :interval
|
|
91
|
+
when 'T' then :triangular
|
|
92
|
+
when nil then DEFAULTS[:quantile]
|
|
93
|
+
when :uniform,:normal,:interval,:triangular then quantile
|
|
94
|
+
else
|
|
95
|
+
raise "unknown tolerance quantile: >>#{quantile}<<"
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
data/lib/tolerances.rb
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require 'tolerance'
|
|
2
|
+
|
|
3
|
+
class Tolerances
|
|
4
|
+
|
|
5
|
+
VERSION = '1.0.0'
|
|
6
|
+
|
|
7
|
+
attr_reader :tolerances, :tags
|
|
8
|
+
|
|
9
|
+
def initialize( contents )
|
|
10
|
+
@contents = contents
|
|
11
|
+
@tolerances = []
|
|
12
|
+
@template = ''
|
|
13
|
+
@tags = Hash.new{ |h,k| h[k]=[] }
|
|
14
|
+
find_tolerances
|
|
15
|
+
create_template
|
|
16
|
+
collect_tags
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
alias parse new
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def values_at( samples )
|
|
24
|
+
if samples.size != tags.size
|
|
25
|
+
raise "samples (#{samples.size}) != tags (#{@tags.size})"
|
|
26
|
+
end
|
|
27
|
+
values = []
|
|
28
|
+
@tolerances.each_with_index do |tol,i|
|
|
29
|
+
values << tol.value_at( samples[tol.sample_index] )
|
|
30
|
+
end
|
|
31
|
+
values
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def result( values )
|
|
35
|
+
@template % values
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
def create_template
|
|
41
|
+
@template = @contents.gsub(Tolerance::REGEX) do
|
|
42
|
+
$5 ? "%#$5" : '%s'
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def find_tolerances
|
|
47
|
+
@tolerances = [Tolerance.parse(@contents)].flatten.compact
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def collect_tags
|
|
51
|
+
sample_index = -1
|
|
52
|
+
@tolerances.each do |tolerance|
|
|
53
|
+
sample_index +=1 unless tags.keys.include? tolerance.tag
|
|
54
|
+
tolerance.sample_index = sample_index
|
|
55
|
+
tags[tolerance.tag] << tolerance
|
|
56
|
+
end
|
|
57
|
+
self
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'simple_stats'
|
|
3
|
+
require 'quantiles'
|
|
4
|
+
|
|
5
|
+
class Array # mixin statistical functions
|
|
6
|
+
include SimpleStats
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class TestQuantiles < Test::Unit::TestCase
|
|
10
|
+
|
|
11
|
+
include Quantiles
|
|
12
|
+
|
|
13
|
+
def setup
|
|
14
|
+
srand 1234
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Normal quantile
|
|
18
|
+
|
|
19
|
+
def test_normal_quantile_domain_checking
|
|
20
|
+
assert_nothing_thrown{normal(Float::EPSILON)}
|
|
21
|
+
assert_raises(RuntimeError){normal(0)}
|
|
22
|
+
assert_raises(RuntimeError){normal(1)}
|
|
23
|
+
assert_nothing_thrown{normal(1-Float::EPSILON)}
|
|
24
|
+
end
|
|
25
|
+
def test_normal_quantile_at_specific_values
|
|
26
|
+
assert_in_delta( -4.264890795, normal(0.00001), 1e-9 )
|
|
27
|
+
assert_in_delta( -0.674489750, normal(0.25000), 1e-9 )
|
|
28
|
+
assert_in_delta( 0.0, normal(0.50000), 1e-9 )
|
|
29
|
+
assert_in_delta( 0.674489750, normal(0.75000), 1e-9 )
|
|
30
|
+
assert_in_delta( 4.264890795, normal(0.99999), 1e-9 )
|
|
31
|
+
end
|
|
32
|
+
def test_normal_quantile_nominal_statistics
|
|
33
|
+
samples = []
|
|
34
|
+
1000.times{ samples << normal(rand) }
|
|
35
|
+
assert_in_delta( 0.0, samples.mean, 0.05, "mean" )
|
|
36
|
+
assert_in_delta( 0.0, samples.median, 0.10, "median" )
|
|
37
|
+
assert_in_delta( 1.0, samples.standard_deviation, 0.05, "sigma" )
|
|
38
|
+
assert_in_delta( 0.0, samples.skewness, 0.10, "skewness" )
|
|
39
|
+
assert_in_delta( 3.0, samples.kurtosis, 0.10, "kurtosis" )
|
|
40
|
+
end
|
|
41
|
+
def test_normal_quantile_specified_sigma_statistics
|
|
42
|
+
samples = []
|
|
43
|
+
1000.times{ samples << 0.2*normal(rand) }
|
|
44
|
+
assert_in_delta( 0.0, samples.mean, 0.05, "mean" )
|
|
45
|
+
assert_in_delta( 0.0, samples.median, 0.05, "median" )
|
|
46
|
+
assert_in_delta( 0.2, samples.standard_deviation, 0.05, "sigma" )
|
|
47
|
+
assert_in_delta( 0.0, samples.skewness, 0.10, "skewness" )
|
|
48
|
+
assert_in_delta( 3.0, samples.kurtosis, 0.10, "kurtosis" )
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Uniform quantile
|
|
52
|
+
|
|
53
|
+
def test_uniform_quantile_nominal_range
|
|
54
|
+
100.times { assert_in_delta( 0.0, uniform(rand), 0.5 ) }
|
|
55
|
+
end
|
|
56
|
+
def test_uniform_quantile_specified_width
|
|
57
|
+
100.times { assert_in_delta( 0.0, 1.0*2*uniform(rand), 1) }
|
|
58
|
+
end
|
|
59
|
+
def test_uniform_quantile_specified_0p4_fractional_width_range
|
|
60
|
+
100.times { assert_in_delta( 0.0, 0.4*2*uniform(rand), 0.4 ) }
|
|
61
|
+
end
|
|
62
|
+
def test_uniform_quantile_statistics
|
|
63
|
+
samples = []
|
|
64
|
+
1000.times{ samples << uniform(rand) }
|
|
65
|
+
assert_in_delta( 0.0, samples.mean, 0.01, "mean" )
|
|
66
|
+
assert_in_delta( 0.0, samples.median, 0.05, "median" )
|
|
67
|
+
assert_in_delta( 1/12.0, samples.variance, 0.05, "sigma" )
|
|
68
|
+
assert_in_delta( 0.0, samples.skewness, 0.10, "skewness" )
|
|
69
|
+
assert_in_delta( 9/5.0, samples.kurtosis, 0.05, "kurtosis" )
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Triangular quantile
|
|
73
|
+
|
|
74
|
+
def test_triangular_quantile_nominal_range
|
|
75
|
+
100.times { assert_in_delta( 0.0, triangular(rand), 0.5 ) }
|
|
76
|
+
end
|
|
77
|
+
def test_triangular_quantile_specified_halfwidth
|
|
78
|
+
100.times { assert_in_delta( 0.0, 1.0*2*triangular(rand), 1.0 ) }
|
|
79
|
+
end
|
|
80
|
+
def test_triangular_quantile_specified_fractional_width_range
|
|
81
|
+
100.times { assert_in_delta( 0.0, 0.2*2*triangular(rand), 0.2 ) }
|
|
82
|
+
end
|
|
83
|
+
def test_triangular_quantile_statistics
|
|
84
|
+
samples = []
|
|
85
|
+
1000.times { samples << triangular(rand) }
|
|
86
|
+
assert_in_delta( 0.0, samples.mean, 0.010, "mean" )
|
|
87
|
+
assert_in_delta( 0.0, samples.median, 0.020, "median" )
|
|
88
|
+
assert_in_delta( 3/72.0, samples.variance, 0.005, "variance" )
|
|
89
|
+
assert_in_delta( 0.0, samples.skewness, 0.050, "skewness" )
|
|
90
|
+
assert_in_delta( 2.4, samples.kurtosis, 0.050, "kurtosis" )
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Cauchy quantile
|
|
94
|
+
|
|
95
|
+
def test_cauchy_quantile_function_with_known_values
|
|
96
|
+
assert_equal 0, cauchy(0.5)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'simple_stats'
|
|
3
|
+
|
|
4
|
+
# FIXME: no monkey patching please
|
|
5
|
+
class Array # mixin statistics
|
|
6
|
+
include SimpleStats
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
class TestArrayStatistics < Test::Unit::TestCase
|
|
10
|
+
|
|
11
|
+
def test_median
|
|
12
|
+
assert_equal(0, [0].median)
|
|
13
|
+
assert_equal(1, [1].median)
|
|
14
|
+
assert_equal(0.5, [0,1].median)
|
|
15
|
+
assert_equal(0.5, [0,1.0].median)
|
|
16
|
+
assert_equal(1, [0,2].median)
|
|
17
|
+
assert_equal(1, [0,1,2].median)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_mean
|
|
21
|
+
assert_equal(0, [0].mean)
|
|
22
|
+
assert_equal(0.5, [0,1].mean)
|
|
23
|
+
assert_equal(0.5, [0,1.0].mean)
|
|
24
|
+
assert_equal(0.5, [0.0,0.25,0.50,0.75,1.0].mean)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def test_variance
|
|
28
|
+
assert_equal(0, [0].variance)
|
|
29
|
+
assert_equal((0.25+0.25)/2, [0,1].variance)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def test_standard_deviation
|
|
33
|
+
assert_equal(0, [0].standard_deviation)
|
|
34
|
+
assert_equal(Math::sqrt((0.25+0.25)/2), [0,1].standard_deviation)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_coefficient_of_variation
|
|
38
|
+
assert_equal(0, [0].coefficient_of_variation)
|
|
39
|
+
assert_equal(Math.sqrt(0.25)/0.5, [0,1].coefficient_of_variation)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_skewness
|
|
43
|
+
assert_equal(0, [0].skewness)
|
|
44
|
+
assert_equal(0, [0,2].skewness)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_kurtosis
|
|
48
|
+
assert_equal(0, [0].kurtosis)
|
|
49
|
+
assert_equal(1, [0,2].kurtosis)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_histogram
|
|
53
|
+
histo = [-3,-1,0,0,4,3,2].histogram(7)
|
|
54
|
+
assert_equal( 7, histo.size )
|
|
55
|
+
assert_equal( 2, histo[2].last)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_percentile
|
|
59
|
+
samples = (1..10).to_a
|
|
60
|
+
assert_equal( 1, samples.percentile(0) )
|
|
61
|
+
assert_equal( 1, samples.percentile(2.5) )
|
|
62
|
+
assert_equal( 1, samples.percentile(10) )
|
|
63
|
+
assert_equal( 2, samples.percentile(20) )
|
|
64
|
+
assert_equal( 5, samples.percentile(50) )
|
|
65
|
+
assert_equal( 8, samples.percentile(80) )
|
|
66
|
+
assert_equal( 10, samples.percentile(97.5) )
|
|
67
|
+
assert_equal( 10, samples.percentile(100) )
|
|
68
|
+
samples = [ 2, 3, 1, 4, 5 ]
|
|
69
|
+
assert_equal( 3, samples.percentile(50) )
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def test_within?
|
|
73
|
+
samples = [ 3, 2, 1, 1, 1 ]
|
|
74
|
+
assert( samples.last(3).within?(0) )
|
|
75
|
+
assert( samples.last(4).within?(1) )
|
|
76
|
+
assert( samples.within?(2) )
|
|
77
|
+
assert( !samples.within?(1) )
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def test_within_percent
|
|
81
|
+
samples = [ 3, 2, 1, 1, 1 ]
|
|
82
|
+
assert( samples.last(3).within?(0,:percent=>true) )
|
|
83
|
+
assert( samples.last(4).within?(100,:percent=>true) )
|
|
84
|
+
assert( samples.within?(200,:percent=>true) )
|
|
85
|
+
assert( !samples.within?(100,:percent=>true) )
|
|
86
|
+
samples = [ 2, 1, 0, 0, 0 ]
|
|
87
|
+
assert( samples.last(3).within?(0, :percent=>true) )
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'tolerance'
|
|
3
|
+
|
|
4
|
+
class TestTolerance < Test::Unit::TestCase
|
|
5
|
+
def setup
|
|
6
|
+
Tolerance.untagged = 0
|
|
7
|
+
end
|
|
8
|
+
def test_should_create_vanilla_tolerance_with_only_center_and_halfwidth
|
|
9
|
+
tol = Tolerance.new 5, 1
|
|
10
|
+
assert_equal 5, tol.center
|
|
11
|
+
assert_equal 1, tol.halfwidth
|
|
12
|
+
assert_equal :raw, tol.type
|
|
13
|
+
assert_nil tol.quantile
|
|
14
|
+
assert_nil tol.format
|
|
15
|
+
assert_equal 'untagged_1', tol.tag
|
|
16
|
+
end
|
|
17
|
+
def test_should_accept_specified_quantile
|
|
18
|
+
tol = Tolerance.new 5, 1, :quantile => :normal
|
|
19
|
+
assert_equal :normal, tol.quantile
|
|
20
|
+
tol = Tolerance.new 5, 1, :quantile => 'N'
|
|
21
|
+
assert_equal :normal, tol.quantile
|
|
22
|
+
end
|
|
23
|
+
def test_should_allow_tag_assignment
|
|
24
|
+
tol = Tolerance.new 5, 1, :tag => 'tag'
|
|
25
|
+
assert_equal 'tag', tol.tag
|
|
26
|
+
end
|
|
27
|
+
def test_should_allow_percent_type
|
|
28
|
+
tol = Tolerance.new 5, 50, :type => :percent
|
|
29
|
+
assert_equal :percent, tol.type
|
|
30
|
+
tol = Tolerance.new 5, 50, :type => '%'
|
|
31
|
+
assert_equal :percent, tol.type
|
|
32
|
+
end
|
|
33
|
+
def test_should_allow_order_type
|
|
34
|
+
tol = Tolerance.new 5, 1, :type => 'o'
|
|
35
|
+
assert_equal :order, tol.type
|
|
36
|
+
tol = Tolerance.new 5, 1, :type => :order
|
|
37
|
+
assert_equal :order, tol.type
|
|
38
|
+
end
|
|
39
|
+
def test_should_allow_uniform_quantile
|
|
40
|
+
tol = Tolerance.new 5, 1, :quantile => "U"
|
|
41
|
+
assert_equal :uniform, tol.quantile
|
|
42
|
+
tol = Tolerance.new 5, 1, :quantile => :uniform
|
|
43
|
+
assert_equal :uniform, tol.quantile
|
|
44
|
+
end
|
|
45
|
+
def test_should_have_uniform_and_tag
|
|
46
|
+
tol = Tolerance.new 0.7, 0.25, :quantile => "U", :tag => 'tag'
|
|
47
|
+
assert_equal 0.7, tol.center
|
|
48
|
+
assert_equal 0.25, tol.halfwidth
|
|
49
|
+
assert_equal :uniform, tol.quantile
|
|
50
|
+
assert_equal 'tag', tol.tag
|
|
51
|
+
end
|
|
52
|
+
def test_should_set_percent_type_and_uniform_quantile
|
|
53
|
+
tol = Tolerance.new 5, 50, :type => "%", :quantile => "U"
|
|
54
|
+
assert_equal :percent, tol.type
|
|
55
|
+
assert_equal :uniform, tol.quantile
|
|
56
|
+
end
|
|
57
|
+
def test_should_set_order_type_and_uniform_quantile
|
|
58
|
+
tol = Tolerance.new 5, 1, :type => "o", :quantile => "U"
|
|
59
|
+
assert_equal :order, tol.type
|
|
60
|
+
assert_equal :uniform, tol.quantile
|
|
61
|
+
end
|
|
62
|
+
def test_should_set_raw_type_with_triangular_quantile
|
|
63
|
+
tol = Tolerance.new 5, 1, :quantile => "T"
|
|
64
|
+
assert_equal :raw, tol.type
|
|
65
|
+
assert_equal :triangular, tol.quantile
|
|
66
|
+
end
|
|
67
|
+
def test_should_sample_at_center_point
|
|
68
|
+
tol = Tolerance.new 5, 1
|
|
69
|
+
assert_equal 5, tol.value_at(0)
|
|
70
|
+
end
|
|
71
|
+
def test_should_parse_a_single_tolerance
|
|
72
|
+
tol = Tolerance.parse '5+/-1'
|
|
73
|
+
assert_equal 5, tol.center
|
|
74
|
+
assert_equal 1, tol.halfwidth
|
|
75
|
+
end
|
|
76
|
+
def test_should_parse_more_than_one_tolerance
|
|
77
|
+
tols = Tolerance.parse '5+/-1 10+/-1'
|
|
78
|
+
assert_equal 2, tols.size
|
|
79
|
+
end
|
|
80
|
+
def test_should_return_nil_if_no_tolerances_found_during_parse
|
|
81
|
+
tol = Tolerance.parse ''
|
|
82
|
+
assert_nil tol
|
|
83
|
+
end
|
|
84
|
+
def test_integer_tolerance
|
|
85
|
+
tol = Tolerance.parse '5+/-1'
|
|
86
|
+
assert_equal 5, tol.center
|
|
87
|
+
assert_equal 1, tol.halfwidth
|
|
88
|
+
end
|
|
89
|
+
def test_float_tolerance
|
|
90
|
+
tol = Tolerance.parse '5.1+/-1.2'
|
|
91
|
+
assert_equal 5.1, tol.center
|
|
92
|
+
assert_equal 1.2, tol.halfwidth
|
|
93
|
+
end
|
|
94
|
+
def test_exponential_tolerance
|
|
95
|
+
tol = Tolerance.parse '5.1e+01+/-1.2'
|
|
96
|
+
assert_equal 5.1e+01, tol.center
|
|
97
|
+
assert_equal 1.2, tol.halfwidth
|
|
98
|
+
end
|
|
99
|
+
def test_tolerance_with_format
|
|
100
|
+
tol = Tolerance.parse '1.35+/-0.2_4.2f'
|
|
101
|
+
assert_equal '4.2f', tol.format
|
|
102
|
+
end
|
|
103
|
+
def test_tolerance_with_single_quoted_tag
|
|
104
|
+
tol = Tolerance.parse "5+/-1 'tag'"
|
|
105
|
+
assert_equal 'tag', tol.tag
|
|
106
|
+
end
|
|
107
|
+
def test_should_allow_no_space_between_tag_and_tolerance
|
|
108
|
+
tol = Tolerance.parse "5+/-1'tag'"
|
|
109
|
+
assert_equal 1, tol.halfwidth
|
|
110
|
+
assert_equal 'tag', tol.tag
|
|
111
|
+
end
|
|
112
|
+
def test_tolerance_with_double_quoted_tag_with_single_quote
|
|
113
|
+
tol = Tolerance.parse '5+/-1 "mix\'d"'
|
|
114
|
+
assert_equal "mix\'d", tol.tag
|
|
115
|
+
end
|
|
116
|
+
def test_tolerance_with_more_general_tag
|
|
117
|
+
tol = Tolerance.parse "5+/-1 'tag^2_2+3->4*1.0'"
|
|
118
|
+
assert_equal 'tag^2_2+3->4*1.0', tol.tag
|
|
119
|
+
end
|
|
120
|
+
def test_tolerance_with_more_general_tag_followed_by_single_quote
|
|
121
|
+
tol = Tolerance.parse "5+/-1 'tag' I'm here"
|
|
122
|
+
assert_equal 'tag', tol.tag
|
|
123
|
+
end
|
|
124
|
+
def test_tolerance_with_leading_debris
|
|
125
|
+
tol = Tolerance.parse "exp1 = 0.7+/-0.25U 'Eta_O2'"
|
|
126
|
+
assert_equal 0.7, tol.center
|
|
127
|
+
end
|
|
128
|
+
def test_tolerance_with_trailing_debris
|
|
129
|
+
tol = Tolerance.parse "0.7+/-0.25U 'Eta_O2' and things"
|
|
130
|
+
assert_equal 'Eta_O2', tol.tag
|
|
131
|
+
end
|
|
132
|
+
def test_percentage_tolerance
|
|
133
|
+
tol = Tolerance.parse "5+/-1%"
|
|
134
|
+
assert_equal :percent, tol.type
|
|
135
|
+
end
|
|
136
|
+
def test_zero_padded_percentage_tolerance
|
|
137
|
+
tol = Tolerance.parse "5+/-03%"
|
|
138
|
+
assert_equal 3, tol.halfwidth
|
|
139
|
+
assert_equal :percent, tol.type
|
|
140
|
+
end
|
|
141
|
+
def test_order_tolerance
|
|
142
|
+
tol = Tolerance.parse "5+/-1o"
|
|
143
|
+
assert_equal :order, tol.type
|
|
144
|
+
end
|
|
145
|
+
def test_tolerance_with_normal_quantile
|
|
146
|
+
tol = Tolerance.parse "5+/-1N"
|
|
147
|
+
assert_equal :normal, tol.quantile
|
|
148
|
+
end
|
|
149
|
+
def test_tolerance_with_uniform_quantile
|
|
150
|
+
tol = Tolerance.parse "5+/-1U"
|
|
151
|
+
assert_equal :uniform, tol.quantile
|
|
152
|
+
end
|
|
153
|
+
def test_tolerance_with_triangular_quantile
|
|
154
|
+
tol = Tolerance.parse "2+/-0.5T"
|
|
155
|
+
assert_equal :triangular, tol.quantile
|
|
156
|
+
end
|
|
157
|
+
def test_tolerance_with_only_interval
|
|
158
|
+
tol = Tolerance.parse "2+/-0.5I"
|
|
159
|
+
assert_equal :interval, tol.quantile
|
|
160
|
+
end
|
|
161
|
+
def test_order_tolerance_with_normal_quantile
|
|
162
|
+
tol = Tolerance.parse "5+/-1oN"
|
|
163
|
+
assert_equal :order, tol.type
|
|
164
|
+
assert_equal :normal, tol.quantile
|
|
165
|
+
end
|
|
166
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require 'tolerances'
|
|
3
|
+
|
|
4
|
+
class TestTolerances < Test::Unit::TestCase
|
|
5
|
+
def setup
|
|
6
|
+
Tolerance.untagged = 0
|
|
7
|
+
end
|
|
8
|
+
def test_should_find_simple_tolerance
|
|
9
|
+
tols = Tolerances.parse('1+/-0.1')
|
|
10
|
+
assert_equal 1, tols.tolerances.size
|
|
11
|
+
tol = tols.tolerances.first
|
|
12
|
+
assert_equal 1, tol.center
|
|
13
|
+
assert_equal 0.1, tol.halfwidth
|
|
14
|
+
assert_equal :raw, tol.type
|
|
15
|
+
assert_nil tol.quantile
|
|
16
|
+
assert_nil tol.format
|
|
17
|
+
assert_equal 'untagged_1', tol.tag
|
|
18
|
+
end
|
|
19
|
+
def test_should_find_two_tolerances_on_same_line
|
|
20
|
+
tols = Tolerances.parse('1+/-0.1 2+/-0.1')
|
|
21
|
+
assert_equal 2, tols.tolerances.size
|
|
22
|
+
end
|
|
23
|
+
def test_should_find_groups_of_tags
|
|
24
|
+
tols = Tolerances.parse('1+/-0.1 "group" 2+/-0.1 "group"')
|
|
25
|
+
assert_equal 2, tols.tags['group'].size
|
|
26
|
+
end
|
|
27
|
+
def test_should_return_interpolated_string_with_values
|
|
28
|
+
tols = Tolerances.parse('2+/-0.1 x 4+/-0.1')
|
|
29
|
+
assert_equal '2.0 x 4.0', tols.result(tols.values_at([0,0]))
|
|
30
|
+
end
|
|
31
|
+
def test_should_return_interpolated_string_with_formatted_values
|
|
32
|
+
tols = Tolerances.parse('2+/-0.1_4.1f x 4+/-0.1_.3f')
|
|
33
|
+
assert_equal ' 2.0 x 4.000', tols.result(tols.values_at([0,0]))
|
|
34
|
+
end
|
|
35
|
+
def test_should_sample_according_to_specified_halfwidth_fraction
|
|
36
|
+
tols = Tolerances.parse('1+/-0.1')
|
|
37
|
+
assert_equal [1.0], tols.values_at([ 0])
|
|
38
|
+
assert_equal [1.1], tols.values_at([ 1])
|
|
39
|
+
assert_equal [0.9], tols.values_at([-1])
|
|
40
|
+
end
|
|
41
|
+
def test_should_fail_if_samples_and_tags_not_same_size
|
|
42
|
+
tols = Tolerances.parse('1+/-0.1')
|
|
43
|
+
assert_raises(RuntimeError){tols.values_at([])}
|
|
44
|
+
end
|
|
45
|
+
def test_should_sample_per_tag_according_to_specified_halfwidth_fraction
|
|
46
|
+
tols = Tolerances.parse('1+/-0.1 "DUP_TAG" 2+/-0.2 "DUP_TAG"')
|
|
47
|
+
assert_equal [ 1.0, 2.0 ], tols.values_at([ 0])
|
|
48
|
+
assert_equal [ 1.1, 2.2 ], tols.values_at([ 1])
|
|
49
|
+
assert_equal [ 0.9, 1.8 ], tols.values_at([-1])
|
|
50
|
+
tols = Tolerances.parse('1+/-0.1 2+/-0.2')
|
|
51
|
+
assert_equal [ 1.0, 1.8 ], tols.values_at([0,-1])
|
|
52
|
+
assert_equal [ 1.1, 2.0 ], tols.values_at([1, 0])
|
|
53
|
+
assert_equal [ 1.1, 1.8 ], tols.values_at([1,-1])
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: tolerances
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Bil Kleb
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain:
|
|
11
|
+
- |
|
|
12
|
+
-----BEGIN CERTIFICATE-----
|
|
13
|
+
MIIDMDCCAhigAwIBAgIBADANBgkqhkiG9w0BAQUFADA+MREwDwYDVQQDDAhfaWwu
|
|
14
|
+
X2xlYjEUMBIGCgmSJomT8ixkARkWBG5hc2ExEzARBgoJkiaJk/IsZAEZFgNnb3Yw
|
|
15
|
+
HhcNMDgwNjI4MTUzNDQ4WhcNMDkwNjI4MTUzNDQ4WjA+MREwDwYDVQQDDAhfaWwu
|
|
16
|
+
X2xlYjEUMBIGCgmSJomT8ixkARkWBG5hc2ExEzARBgoJkiaJk/IsZAEZFgNnb3Yw
|
|
17
|
+
ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCfYLFuBq7r82ByFeDxO8tN
|
|
18
|
+
Jlj48V/gDge4O3GA8ixGNIAYm3f1WZ9Y/I5wVPgdTLbtx6bzoxxsbWSxDBjEQfPc
|
|
19
|
+
A3tw3SaDBf6e3fZKulvWnZfQRFXykuVqsNDVN6Gptuz6AkKSZSxjBL+qva8+9PPB
|
|
20
|
+
tFjwsjHTwSZJoKQascjDr7lwdDZjQJD1PIzXNoN1wtGhungmD2T1ILKyNHuh/qr5
|
|
21
|
+
BQcfYrinKg9IbxJ0+rG5k5l6/0ybNp9G982+QE5JORENHcriCkVn8OoJHJpfuO+o
|
|
22
|
+
Zf4P0nTq9bM7cXLJBlnxBcb9Kj06o6583rtpL/H6wdnDuUMtBZ2QyENBfbSYGo9H
|
|
23
|
+
AgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQWBBSksDR+
|
|
24
|
+
lQd7lJO74TFtKv/cIHPaHDANBgkqhkiG9w0BAQUFAAOCAQEAKUaF/Yk4Y44D8/zD
|
|
25
|
+
4aee+CHm/gIQQfpvoAsSqDOBFIQibegx3c5GmNuc5JOK04Jp0oNt0I0EjTJhlbwO
|
|
26
|
+
HM5YQKNOXzRYW+E0FRqgExGkXa1BXSSglPo3GDSXHGMEUXUOCxanxzzJQPxL/wvR
|
|
27
|
+
sQfMNhZ0YmPB9slt+jzcEP0IWmV4Dwy83DdkPQjEh+mKvjmFRFqcwI3bYWYvALTF
|
|
28
|
+
t43RKq41H2ZFHxJTMGEOjzLYNkbsntV+XjrV8CMvJUs2X9aF/iCNpWr4AGIfglyC
|
|
29
|
+
EoMJU0ZoSzyJuoa35pscBdI6j7mzIDMX22WAsmR+S5ktzq4Bdn+Vmhk+EIZGkZ3I
|
|
30
|
+
55z1bA==
|
|
31
|
+
-----END CERTIFICATE-----
|
|
32
|
+
|
|
33
|
+
date: 2008-07-03 00:00:00 +02:00
|
|
34
|
+
default_executable:
|
|
35
|
+
dependencies:
|
|
36
|
+
- !ruby/object:Gem::Dependency
|
|
37
|
+
name: hoe
|
|
38
|
+
type: :runtime
|
|
39
|
+
version_requirement:
|
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: 1.6.0
|
|
45
|
+
version:
|
|
46
|
+
description: Provides a Tolerance object and a Tolerances collection to facilitate specifying and analyzing variabilities and uncertainties. To aid in sampling for Monte-Carlos applications, this package also includes some inverse cummulative probability distribution functions.
|
|
47
|
+
email:
|
|
48
|
+
- Bil.Kleb@nasa.gov
|
|
49
|
+
executables: []
|
|
50
|
+
|
|
51
|
+
extensions: []
|
|
52
|
+
|
|
53
|
+
extra_rdoc_files:
|
|
54
|
+
- History.txt
|
|
55
|
+
- License.txt
|
|
56
|
+
- Manifest.txt
|
|
57
|
+
- README.txt
|
|
58
|
+
files:
|
|
59
|
+
- History.txt
|
|
60
|
+
- License.txt
|
|
61
|
+
- Manifest.txt
|
|
62
|
+
- README.txt
|
|
63
|
+
- Rakefile
|
|
64
|
+
- lib/quantiles.rb
|
|
65
|
+
- lib/simple_stats.rb
|
|
66
|
+
- lib/tolerance.rb
|
|
67
|
+
- lib/tolerances.rb
|
|
68
|
+
- test/test_quantiles.rb
|
|
69
|
+
- test/test_simple_stats.rb
|
|
70
|
+
- test/test_tolerance.rb
|
|
71
|
+
- test/test_tolerances.rb
|
|
72
|
+
has_rdoc: true
|
|
73
|
+
homepage: http://rubyforge.org/projects/nasarb
|
|
74
|
+
post_install_message:
|
|
75
|
+
rdoc_options:
|
|
76
|
+
- --main
|
|
77
|
+
- README.txt
|
|
78
|
+
require_paths:
|
|
79
|
+
- lib
|
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: "0"
|
|
85
|
+
version:
|
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: "0"
|
|
91
|
+
version:
|
|
92
|
+
requirements: []
|
|
93
|
+
|
|
94
|
+
rubyforge_project: nasarb
|
|
95
|
+
rubygems_version: 1.2.0
|
|
96
|
+
signing_key:
|
|
97
|
+
specification_version: 2
|
|
98
|
+
summary: Provides a Tolerance object and a Tolerances collection to facilitate specifying and analyzing variabilities and uncertainties
|
|
99
|
+
test_files:
|
|
100
|
+
- test/test_quantiles.rb
|
|
101
|
+
- test/test_simple_stats.rb
|
|
102
|
+
- test/test_tolerance.rb
|
|
103
|
+
- test/test_tolerances.rb
|
metadata.gz.sig
ADDED
|
Binary file
|