apriori 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/History.txt +16 -0
  2. data/License.txt +20 -0
  3. data/Manifest.txt +121 -0
  4. data/README.txt +149 -0
  5. data/Rakefile +15 -0
  6. data/TODO.txt +60 -0
  7. data/attic/c_ext_test1/MyTest/MyTest.c +23 -0
  8. data/attic/c_ext_test1/MyTest/extconf.rb +11 -0
  9. data/attic/c_ext_test1/mytest.rb +10 -0
  10. data/attic/test.c +12 -0
  11. data/config/hoe.rb +81 -0
  12. data/config/requirements.rb +29 -0
  13. data/examples/01_simple_example.rb +32 -0
  14. data/examples/02_small_file_example.rb +17 -0
  15. data/examples/03_large_file_example.rb +22 -0
  16. data/examples/test_data/market_basket_basic_test.dat +9 -0
  17. data/ext/Apriori.c +149 -0
  18. data/ext/Makefile +149 -0
  19. data/ext/apriori/doc/apriori.html +1301 -0
  20. data/ext/apriori/doc/arem.gp +68 -0
  21. data/ext/apriori/doc/c_rev.gp +89 -0
  22. data/ext/apriori/doc/chi2.tex +156 -0
  23. data/ext/apriori/doc/copying +504 -0
  24. data/ext/apriori/doc/line.gif +0 -0
  25. data/ext/apriori/doc/uparrow.gif +0 -0
  26. data/ext/apriori/ex/flg2set +15 -0
  27. data/ext/apriori/ex/hdr2set +13 -0
  28. data/ext/apriori/ex/readme +71 -0
  29. data/ext/apriori/ex/row2set +7 -0
  30. data/ext/apriori/ex/rulesort +24 -0
  31. data/ext/apriori/ex/tab2set +9 -0
  32. data/ext/apriori/ex/test.app +2 -0
  33. data/ext/apriori/ex/test.rul +9 -0
  34. data/ext/apriori/ex/test1.rul +43 -0
  35. data/ext/apriori/ex/test1.tab +10 -0
  36. data/ext/apriori/ex/test2.tab +10 -0
  37. data/ext/apriori/ex/test3.tab +30 -0
  38. data/ext/apriori/ex/test4.tab +11 -0
  39. data/ext/apriori/ex/test5.tab +39 -0
  40. data/ext/apriori/ex/tid2set +23 -0
  41. data/ext/apriori/ex/xhdr2set +33 -0
  42. data/ext/apriori/src/apriori.c +750 -0
  43. data/ext/apriori/src/apriori.dsp +120 -0
  44. data/ext/apriori/src/apriori.dsw +29 -0
  45. data/ext/apriori/src/apriori.mak +99 -0
  46. data/ext/apriori/src/istree.c +1411 -0
  47. data/ext/apriori/src/istree.h +160 -0
  48. data/ext/apriori/src/makefile +105 -0
  49. data/ext/apriori/src/tract.c +870 -0
  50. data/ext/apriori/src/tract.h +261 -0
  51. data/ext/apriori_wrapper.c +757 -0
  52. data/ext/apriori_wrapper.h +10 -0
  53. data/ext/extconf.rb +32 -0
  54. data/ext/math/doc/copying +504 -0
  55. data/ext/math/src/chi2.c +151 -0
  56. data/ext/math/src/chi2.h +27 -0
  57. data/ext/math/src/choose.c +71 -0
  58. data/ext/math/src/choose.h +16 -0
  59. data/ext/math/src/gamma.c +446 -0
  60. data/ext/math/src/gamma.h +39 -0
  61. data/ext/math/src/intexp.c +35 -0
  62. data/ext/math/src/intexp.h +15 -0
  63. data/ext/math/src/makefile +164 -0
  64. data/ext/math/src/math.mak +48 -0
  65. data/ext/math/src/normal.c +387 -0
  66. data/ext/math/src/normal.h +44 -0
  67. data/ext/math/src/radfn.c +113 -0
  68. data/ext/math/src/radfn.h +34 -0
  69. data/ext/math/src/zeta.c +49 -0
  70. data/ext/math/src/zeta.h +15 -0
  71. data/ext/pre-clean.rb +8 -0
  72. data/ext/pre-setup.rb +9 -0
  73. data/ext/util/doc/copying +504 -0
  74. data/ext/util/src/listops.c +76 -0
  75. data/ext/util/src/listops.h +26 -0
  76. data/ext/util/src/makefile +103 -0
  77. data/ext/util/src/memsys.c +84 -0
  78. data/ext/util/src/memsys.h +42 -0
  79. data/ext/util/src/nstats.c +288 -0
  80. data/ext/util/src/nstats.h +69 -0
  81. data/ext/util/src/params.c +86 -0
  82. data/ext/util/src/params.h +19 -0
  83. data/ext/util/src/parse.c +133 -0
  84. data/ext/util/src/parse.h +81 -0
  85. data/ext/util/src/scan.c +767 -0
  86. data/ext/util/src/scan.h +111 -0
  87. data/ext/util/src/symtab.c +443 -0
  88. data/ext/util/src/symtab.h +121 -0
  89. data/ext/util/src/tabscan.c +279 -0
  90. data/ext/util/src/tabscan.h +99 -0
  91. data/ext/util/src/util.mak +91 -0
  92. data/ext/util/src/vecops.c +317 -0
  93. data/ext/util/src/vecops.h +42 -0
  94. data/lib/apriori.rb +133 -0
  95. data/lib/apriori/adapter.rb +13 -0
  96. data/lib/apriori/association_rule.rb +89 -0
  97. data/lib/apriori/version.rb +9 -0
  98. data/script/console +10 -0
  99. data/script/destroy +14 -0
  100. data/script/generate +14 -0
  101. data/script/txt2html +82 -0
  102. data/setup.rb +1585 -0
  103. data/tasks/apriori.rake +20 -0
  104. data/tasks/attic.rake +28 -0
  105. data/tasks/deployment.rake +34 -0
  106. data/tasks/environment.rake +7 -0
  107. data/tasks/install.rake +13 -0
  108. data/tasks/website.rake +17 -0
  109. data/test/apriori_test.rb +13 -0
  110. data/test/fixtures/market_basket_results_test.txt +5 -0
  111. data/test/fixtures/market_basket_string_test.txt +7 -0
  112. data/test/fixtures/results.txt +2 -0
  113. data/test/fixtures/sample.txt +7 -0
  114. data/test/test_helper.rb +5 -0
  115. data/test/unit/test_apriori.rb +68 -0
  116. data/test/unit/test_itemsets_and_parsing.rb +82 -0
  117. data/website/index.html +248 -0
  118. data/website/index.txt +152 -0
  119. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  120. data/website/stylesheets/screen.css +142 -0
  121. data/website/template.html.erb +49 -0
  122. metadata +226 -0
@@ -0,0 +1,39 @@
1
+ /*----------------------------------------------------------------------
2
+ File : gamma.h
3
+ Contents: computation of the (incomplete) gamma function
4
+ Author : Christian Borgelt
5
+ History : 2002.07.04 file created
6
+ 2002.08.02 function Gamma added
7
+ 2003.05.19 incomplete Gamma function added
8
+ 2008.03.14 more incomplete Gamma functions added
9
+ 2008.03.17 gamma distribution functions added
10
+ ----------------------------------------------------------------------*/
11
+ #ifndef __GAMMA__
12
+ #define __GAMMA__
13
+
14
+ /*----------------------------------------------------------------------
15
+ Functions
16
+ ----------------------------------------------------------------------*/
17
+ extern double logGamma (double n);
18
+ extern double Gamma (double n);
19
+ extern double lowerGamma (double n, double x);
20
+ extern double upperGamma (double n, double x);
21
+ extern double GammaP (double n, double x);
22
+ extern double GammaQ (double n, double x);
23
+
24
+ extern double Gammapdf (double x, double k, double theta);
25
+ extern double Gammacdf (double x, double k, double theta);
26
+ extern double GammacdfP (double x, double k, double theta);
27
+ extern double GammacdfQ (double x, double k, double theta);
28
+ extern double Gammaqtl (double prob, double k, double theta);
29
+ extern double GammaqtlP (double prob, double k, double theta);
30
+ extern double GammaqtlQ (double prob, double k, double theta);
31
+
32
+ /*----------------------------------------------------------------------
33
+ Preprocessor Definitions
34
+ ----------------------------------------------------------------------*/
35
+ #define Gammacdf(x,k,t) GammaP(k,(x)/(t))
36
+ #define GammacdfP(x,k,t) GammaP(k,(x)/(t))
37
+ #define GammacdfQ(x,k,t) GammaQ(k,(x)/(t))
38
+
39
+ #endif
@@ -0,0 +1,35 @@
1
+ /*----------------------------------------------------------------------
2
+ File : intexp.c
3
+ Contents: raise x to the power of n, n integer
4
+ Author : Christian Borgelt
5
+ History : 17.05.2003 file created
6
+ ----------------------------------------------------------------------*/
7
+ #include "intexp.h"
8
+
9
+ /*----------------------------------------------------------------------
10
+ Functions
11
+ ----------------------------------------------------------------------*/
12
+ #if 0
13
+
14
+ double intexp (double x, int n)
15
+ { /* --- raise x to the power of n */
16
+ if (n <= 0) return 0; /* check for immediate cases, otherw. */
17
+ if (n <= 1) return x; /* apply the recursive formula */
18
+ if (n & 1) return intexp(x*x, n >> 1) *x;
19
+ else return intexp(x*x, n >> 1);
20
+ } /* intexp() */
21
+
22
+ #else /*--------------------------------------------------------------*/
23
+
24
+ double intexp (double x, int n)
25
+ { /* --- raise x to the power of n */
26
+ double r = (n & 1) ? x : 1; /* result */
27
+
28
+ for (n >>= 1; n > 0; n >>= 1) {
29
+ x *= x; /* traverse the powers of 2 and */
30
+ if (n & 1) r *= x; /* multiply them into the result */
31
+ } /* if the corr. exponent bit is set */
32
+ return r; /* return the result */
33
+ } /* intexp() */
34
+
35
+ #endif
@@ -0,0 +1,15 @@
1
+ /*----------------------------------------------------------------------
2
+ File : intexp.h
3
+ Contents: raise x to the power of n, n integer
4
+ Author : Christian Borgelt
5
+ History : 17.05.2003 file created
6
+ ----------------------------------------------------------------------*/
7
+ #ifndef __INTEXP__
8
+ #define __INTEXP__
9
+
10
+ /*----------------------------------------------------------------------
11
+ Functions
12
+ ----------------------------------------------------------------------*/
13
+ extern double intexp (double x, int n);
14
+
15
+ #endif
@@ -0,0 +1,164 @@
1
+ #-----------------------------------------------------------------------
2
+ # File : makefile (directory: math)
3
+ # Contents: build math utility modules
4
+ # Author : Christian Borgelt
5
+ # History : 2002.07.04 file created from makefile in util directory
6
+ # 2003.05.19 intexp, choose, zeta, quantile, and normd added
7
+ # 2003.08.15 module radfn added
8
+ # 2008.03.14 main programs added, quantile.c split/removed
9
+ # 2008.03.17 gamma distribution functions added
10
+ #-----------------------------------------------------------------------
11
+ CC = gcc
12
+ CFBASE = -ansi -Wall -pedantic $(ADDFLAGS)
13
+ CFLAGS = $(CFBASE) -DNDEBUG -O3
14
+ # CFLAGS = $(CFBASE) -g
15
+ LDFLAGS =
16
+ LIBS = -lm
17
+
18
+ GAMMA = gammapdf gammacdf gammaqtl
19
+ NORMAL = normpdf normcdf normqtl
20
+ CHI2 = chi2pdf chi2cdf chi2qtl
21
+ PROGS = $(GAMMA) $(NORMAL) $(CHI2) gamma choose zeta
22
+
23
+ #-----------------------------------------------------------------------
24
+ # Build Programs
25
+ #-----------------------------------------------------------------------
26
+ all: $(PROGS)
27
+
28
+ gamma: gammain.o makefile
29
+ $(CC) $(LDFLAGS) $(LIBS) gammain.o -o $@
30
+
31
+ choose: chsmain.o makefile
32
+ $(CC) $(LDFLAGS) $(LIBS) chsmain.o -o $@
33
+
34
+ zeta: zetamain.o makefile
35
+ $(CC) $(LDFLAGS) $(LIBS) zetamain.o -o $@
36
+
37
+ gammapdf: gammapdf.o makefile
38
+ $(CC) $(LDFLAGS) $(LIBS) gammapdf.o -o $@
39
+
40
+ gammacdf: gammacdf.o makefile
41
+ $(CC) $(LDFLAGS) $(LIBS) gammacdf.o -o $@
42
+
43
+ gammaqtl: gammaqtl.o normal.o makefile
44
+ $(CC) $(LDFLAGS) $(LIBS) normal.o gammaqtl.o -o $@
45
+
46
+ normpdf: normpdf.o makefile
47
+ $(CC) $(LDFLAGS) $(LIBS) normpdf.o -o $@
48
+
49
+ normcdf: normcdf.o makefile
50
+ $(CC) $(LDFLAGS) $(LIBS) normcdf.o -o $@
51
+
52
+ normqtl: normqtl.o gamma.o makefile
53
+ $(CC) $(LDFLAGS) $(LIBS) gamma.o normqtl.o -o $@
54
+
55
+ chi2pdf: chi2pdf.o gamma.o makefile
56
+ $(CC) $(LDFLAGS) $(LIBS) gamma.o chi2pdf.o -o $@
57
+
58
+ chi2cdf: chi2cdf.o gamma.o makefile
59
+ $(CC) $(LDFLAGS) $(LIBS) gamma.o chi2cdf.o -o $@
60
+
61
+ chi2qtl: chi2qtl.o gammall.o normal.o makefile
62
+ $(CC) $(LDFLAGS) $(LIBS) gammall.o normal.o chi2qtl.o -o $@
63
+
64
+ #-----------------------------------------------------------------------
65
+ # Programs
66
+ #-----------------------------------------------------------------------
67
+ gammain.o: gamma.h
68
+ gammain.o: gamma.c makefile
69
+ $(CC) $(CFLAGS) -DGAMMA_MAIN -c gamma.c -o $@
70
+
71
+ chsmain.o: choose.h
72
+ chsmain.o: choose.c makefile
73
+ $(CC) $(CFLAGS) -DCHOOSE_MAIN -c choose.c -o $@
74
+
75
+ zetamain.o: zeta.h
76
+ zetamain.o: zeta.c makefile
77
+ $(CC) $(CFLAGS) -DZETA_MAIN -c zeta.c -o $@
78
+
79
+ gammapdf.o: gamma.h
80
+ gammapdf.o: gamma.c makefile
81
+ $(CC) $(CFLAGS) -DGAMMAPDF_MAIN -c gamma.c -o $@
82
+
83
+ gammacdf.o: gamma.h
84
+ gammacdf.o: gamma.c makefile
85
+ $(CC) $(CFLAGS) -DGAMMACDF_MAIN -c gamma.c -o $@
86
+
87
+ gammaqtl.o: gamma.h
88
+ gammaqtl.o: gamma.c makefile
89
+ $(CC) $(CFLAGS) -DGAMMAQTL_MAIN -c gamma.c -o $@
90
+
91
+ normpdf.o: normal.h
92
+ normpdf.o: normal.c makefile
93
+ $(CC) $(CFLAGS) -DNORMPDF_MAIN -c normal.c -o $@
94
+
95
+ normcdf.o: normal.h
96
+ normcdf.o: normal.c makefile
97
+ $(CC) $(CFLAGS) -DNORMCDF_MAIN -c normal.c -o $@
98
+
99
+ normqtl.o: normal.h
100
+ normqtl.o: normal.c makefile
101
+ $(CC) $(CFLAGS) -DNORMQTL_MAIN -c normal.c -o $@
102
+
103
+ chi2pdf.o: chi2.h
104
+ chi2pdf.o: chi2.c makefile
105
+ $(CC) $(CFLAGS) -DCHI2PDF_MAIN -c chi2.c -o $@
106
+
107
+ chi2cdf.o: chi2.h
108
+ chi2cdf.o: chi2.c makefile
109
+ $(CC) $(CFLAGS) -DCHI2CDF_MAIN -c chi2.c -o $@
110
+
111
+ chi2qtl.o: chi2.h
112
+ chi2qtl.o: chi2.c makefile
113
+ $(CC) $(CFLAGS) -DCHI2QTL_MAIN -c chi2.c -o $@
114
+
115
+ #-----------------------------------------------------------------------
116
+ # Mathematical Functions
117
+ #-----------------------------------------------------------------------
118
+ choose.o: choose.h
119
+ choose.o: choose.c makefile
120
+ $(CC) $(CFLAGS) -c choose.c -o $@
121
+
122
+ zeta.o: zeta.h
123
+ zeta.o: zeta.c makefile
124
+ $(CC) $(CFLAGS) -c zeta.c -o $@
125
+
126
+ intexp.o: intexp.h
127
+ intexp.o: intexp.c makefile
128
+ $(CC) $(CFLAGS) -c intexp.c -o $@
129
+
130
+ gamma.o: gamma.h
131
+ gamma.o: gamma.c makefile
132
+ $(CC) $(CFLAGS) -c gamma.c -o $@
133
+
134
+ gammall.o: gamma.h
135
+ gammall.o: gamma.c makefile
136
+ $(CC) $(CFLAGS) -DGAMMAQTL -c gamma.c -o $@
137
+
138
+ normal.o: normal.h
139
+ normal.o: normal.c makefile
140
+ $(CC) $(CFLAGS) -c normal.c -o $@
141
+
142
+ chi2.o: chi2.h
143
+ chi2.o: chi2.c makefile
144
+ $(CC) $(CFLAGS) -c chi2.c -o $@
145
+
146
+ chi2all.o: chi2.h gamma.h normal.h
147
+ chi2all.o: chi2.c makefile
148
+ $(CC) $(CFLAGS) -DCHI2QTL -c chi2.c -o $@
149
+
150
+ radfn.o: radfn.h gamma.h
151
+ radfn.o: radfn.c makefile
152
+ $(CC) $(CFLAGS) -c radfn.c -o $@
153
+
154
+ #-----------------------------------------------------------------------
155
+ # Install
156
+ #-----------------------------------------------------------------------
157
+ install:
158
+ cp $(PROGS) $(HOME)/bin
159
+
160
+ #-----------------------------------------------------------------------
161
+ # Clean up
162
+ #-----------------------------------------------------------------------
163
+ clean:
164
+ rm -f $(PROGS) *.o *~ *.flc core
@@ -0,0 +1,48 @@
1
+ #-----------------------------------------------------------------------
2
+ # File : math.mak
3
+ # Contents: build math utility modules
4
+ # Author : Christian Borgelt
5
+ # History : 26.01.2003 file created
6
+ # 19.05.2003 intexp, choose, zeta, quantile, and normd added
7
+ # 15.08.2003 module radfn added
8
+ #-----------------------------------------------------------------------
9
+ CC = cl.exe
10
+ LD = link.exe
11
+ DEFS = /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS"
12
+ CFLAGS = /nologo /W3 /EHsc /O2 $(DEFS) /FD /c /D _CRT_SECURE_NO_DEPRECATE
13
+
14
+ #-----------------------------------------------------------------------
15
+ # Build Modules
16
+ #-----------------------------------------------------------------------
17
+ all: intexp.obj choose.obj gamma.obj zeta.obj quantile.obj \
18
+ normd.obj radfn.obj
19
+
20
+ #-----------------------------------------------------------------------
21
+ # Gamma Function
22
+ #-----------------------------------------------------------------------
23
+ intexp.obj: intexp.h intexp.c math.mak
24
+ $(CC) $(CFLAGS) intexp.c /Fo$@
25
+
26
+ choose.obj: choose.h choose.c math.mak
27
+ $(CC) $(CFLAGS) choose.c /Fo$@
28
+
29
+ gamma.obj: gamma.h gamma.c math.mak
30
+ $(CC) $(CFLAGS) gamma.c /Fo$@
31
+
32
+ zeta.obj: zeta.h zeta.c math.mak
33
+ $(CC) $(CFLAGS) zeta.c /Fo$@
34
+
35
+ quantile.obj: quantile.h quantile.c math.mak
36
+ $(CC) $(CFLAGS) quantile.c /Fo$@
37
+
38
+ normd.obj: normd.h normd.c math.mak
39
+ $(CC) $(CFLAGS) normd.c /Fo$@
40
+
41
+ radfn.obj: radfn.h radfn.c math.mak
42
+ $(CC) $(CFLAGS) radfn.c /Fo$@
43
+
44
+ #-----------------------------------------------------------------------
45
+ # Clean up
46
+ #-----------------------------------------------------------------------
47
+ clean:
48
+ -@erase /Q *~ *.obj *.idb *.pch
@@ -0,0 +1,387 @@
1
+ /*----------------------------------------------------------------------
2
+ File : normal.c
3
+ Contents: normal distribution functions
4
+ Author : Christian Borgelt
5
+ History : 2002.11.04 file created
6
+ 2008.03.14 quantile function, main function added
7
+ 2008.03.15 cumulative distribution function added
8
+ ----------------------------------------------------------------------*/
9
+ #if defined(NORMPDF_MAIN) \
10
+ || defined(NORMCDF_MAIN) \
11
+ || defined(NORMQTL_MAIN)
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+ #endif
15
+ #include <float.h>
16
+ #include <assert.h>
17
+ #include <math.h>
18
+ #include "normal.h"
19
+
20
+ /*----------------------------------------------------------------------
21
+ Preprocessor Definitions
22
+ ----------------------------------------------------------------------*/
23
+ #define SQRT_2 1.41421356237309504880168872421 /* \sqrt(2) */
24
+ #define _1_SQRT_2PI 0.39894228040143267793994605993 /* 1/\sqrt(2\pi) */
25
+
26
+ /*----------------------------------------------------------------------
27
+ Functions
28
+ ----------------------------------------------------------------------*/
29
+
30
+ double unitpdf (double x)
31
+ { /* --- probability density function */
32
+ return exp(-0.5*x*x) *_1_SQRT_2PI;
33
+ } /* unitpdf() */
34
+
35
+ /*--------------------------------------------------------------------*/
36
+
37
+ double unitcdfP (double x)
38
+ { /* --- cumulative distribution fn. */
39
+ double y, z, u; /* square, absolute value of x */
40
+
41
+ if (x > 8.572) return 1.0; /* if outside proper interval, */
42
+ if (x < -37.519) return 0.0; /* return the limiting values */
43
+ z = fabs(x); /* exploit the symmetry */
44
+ if (z < 0.5*2.2204460492503131e-16)
45
+ return 0.5; /* treat 0 as a special case */
46
+ if (z < 0.66291) { /* if fairly close to zero */
47
+ y = x*x; /* compute the square of x */
48
+ z = (((( 0.065682337918207449113
49
+ *y + 2.2352520354606839287)
50
+ *y + 161.02823106855587881)
51
+ *y + 1067.6894854603709582)
52
+ *y + 18154.981253343561249)
53
+ / ((((
54
+ y + 47.20258190468824187)
55
+ *y + 976.09855173777669322)
56
+ *y + 10260.932208618978205)
57
+ *y + 45507.789335026729956);
58
+ return 0.5 +x *z; /* compute with rational function */
59
+ }
60
+ if (z < 4*SQRT_2) { /* if medium value */
61
+ u = (((((((( 1.0765576773720192317e-8
62
+ *z + 0.39894151208813466764)
63
+ *z + 8.8831497943883759412)
64
+ *z + 93.506656132177855979)
65
+ *z + 597.27027639480026226)
66
+ *z + 2494.5375852903726711)
67
+ *z + 6848.1904505362823326)
68
+ *z + 11602.651437647350124)
69
+ *z + 9842.7148383839780218)
70
+ / ((((((((
71
+ z + 22.266688044328115691)
72
+ *z + 235.38790178262499861)
73
+ *z + 1519.377599407554805)
74
+ *z + 6485.558298266760755)
75
+ *z + 18615.571640885098091)
76
+ *z + 34900.952721145977266)
77
+ *z + 38912.003286093271411)
78
+ *z + 19685.429676859990727);
79
+ z = u *exp(-0.5*x*x); } /* compute with rational function */
80
+ else { /* if tail value */
81
+ y = 1 /(x*x); /* compute the inverse square of x */
82
+ u = ((((( 0.02307344176494017303
83
+ *y + 0.21589853405795699)
84
+ *y + 0.1274011611602473639)
85
+ *y + 0.022235277870649807)
86
+ *y + 0.001421619193227893466)
87
+ *y + 2.9112874951168792e-5)
88
+ / (((((
89
+ y + 1.28426009614491121)
90
+ *y + 0.468238212480865118)
91
+ *y + 0.0659881378689285515)
92
+ *y + 0.00378239633202758244)
93
+ *y + 7.29751555083966205e-5);
94
+ z = (((_1_SQRT_2PI) -y*u) /z) *exp(-0.5*x*x);
95
+ } /* compute with rational function */
96
+ return (x > 0) ? 1-z : z; /* exploit the symmetry */
97
+ } /* unitcdfP() */
98
+
99
+ /*----------------------------------------------------------------------
100
+ References: - W.J. Cody.
101
+ Rational Chebyshev Approximations for the Error Function.
102
+ Mathematics of Computation, 23(107):631-637, 1969
103
+ - W. Fraser and J.F Hart.
104
+ On the Computation of Rational Approximations
105
+ to Continuous Functions.
106
+ Communications of the ACM 5, 1962
107
+ - W.J. Kennedy Jr. and J.E. Gentle.
108
+ Statistical Computing.
109
+ Marcel Dekker, 1980
110
+ ----------------------------------------------------------------------*/
111
+ #if 0
112
+
113
+ double unitqtlP (double prob)
114
+ { /* --- quantile of normal distrib. */
115
+ double p, x; /* with mean 0 and variance 1 */
116
+
117
+ assert((prob >= 0) && (prob <= 1)); /* check the function argument */
118
+ if (prob >= 1.0) return DBL_MAX; /* check for limiting values */
119
+ if (prob <= 0.0) return -DBL_MAX; /* and return extrema */
120
+ if (prob == 0.5) return 0; /* treat prob = 0.5 as a special case */
121
+ p = (prob > 0.5) ? 1-prob : prob; /* transform to left tail if nec. */
122
+ if (p < 1e-20) return (prob < 0.5) ? -10 : 10;
123
+ x = sqrt(log(1/(p*p))); /* compute quotient of polynomials */
124
+ x += ((((-0.453642210148e-4 /* (rational function approximation) */
125
+ *x -0.0204231210245)
126
+ *x -0.342242088547)
127
+ *x -1.0)
128
+ *x -0.322232431088)
129
+ / (((( 0.0038560700634
130
+ *x +0.103537752850)
131
+ *x +0.531103462366)
132
+ *x +0.588581570495)
133
+ *x +0.0993484626060);
134
+ return (prob < 0.5) ? -x : x; /* retransform to right tail if nec. */
135
+ } /* unitqtlP() */
136
+
137
+ #else /*--------------------------------------------------------------*/
138
+
139
+ double unitqtlP (double prob)
140
+ { /* --- quantile of normal distrib. */
141
+ double p, x; /* with mean 0 and variance 1 */
142
+
143
+ assert((prob >= 0) && (prob <= 1)); /* check the function argument */
144
+ if (prob >= 1.0) return DBL_MAX; /* check for limiting values */
145
+ if (prob <= 0.0) return -DBL_MAX; /* and return extrema */
146
+ p = prob -0.5;
147
+ if (fabs(p) <= 0.425) { /* if not tail */
148
+ x = 0.180625 - p*p; /* get argument of rational function */
149
+ x = (((((((2509.0809287301226727
150
+ *x + 33430.575583588128105)
151
+ *x + 67265.770927008700853)
152
+ *x + 45921.953931549871457)
153
+ *x + 13731.693765509461125)
154
+ *x + 1971.5909503065514427)
155
+ *x + 133.14166789178437745)
156
+ *x + 3.387132872796366608)
157
+ / (((((((5226.495278852854561
158
+ *x + 28729.085735721942674)
159
+ *x + 39307.89580009271061)
160
+ *x + 21213.794301586595867)
161
+ *x + 5394.1960214247511077)
162
+ *x + 687.1870074920579083)
163
+ *x + 42.313330701600911252)
164
+ *x + 1.0); /* evaluate the rational function */
165
+ return p *x; /* and return the computed value */
166
+ }
167
+ p = (prob > 0.5) ? 1-prob : prob;
168
+ x = sqrt(-log(p)); /* transform to left tail if nec. */
169
+ if (x <= 5) { /* if not extreme tail */
170
+ x -= 1.6; /* get argument of rational function */
171
+ x = ((((((( 7.7454501427834140764e-4
172
+ *x + 0.0227238449892691845833)
173
+ *x + 0.24178072517745061177)
174
+ *x + 1.27045825245236838258)
175
+ *x + 3.64784832476320460504)
176
+ *x + 5.7694972214606914055)
177
+ *x + 4.6303378461565452959)
178
+ *x + 1.42343711074968357734)
179
+ / ((((((( 1.05075007164441684324e-9
180
+ *x + 5.475938084995344946e-4)
181
+ *x + 0.0151986665636164571966)
182
+ *x + 0.14810397642748007459)
183
+ *x + 0.68976733498510000455)
184
+ *x + 1.6763848301838038494)
185
+ *x + 2.05319162663775882187)
186
+ *x + 1.0); } /* evaluate the rational function */
187
+ else { /* if extreme tail */
188
+ x -= 5; /* get argument of rational function */
189
+ x = ((((((( 2.01033439929228813265e-7
190
+ *x + 2.71155556874348757815e-5)
191
+ *x + 0.0012426609473880784386)
192
+ *x + 0.026532189526576123093)
193
+ *x + 0.29656057182850489123)
194
+ *x + 1.7848265399172913358)
195
+ *x + 5.4637849111641143699)
196
+ *x + 6.6579046435011037772)
197
+ / ((((((( 2.04426310338993978564e-15
198
+ *x + 1.4215117583164458887e-7)
199
+ *x + 1.8463183175100546818e-5)
200
+ *x + 7.868691311456132591e-4)
201
+ *x + 0.0148753612908506148525)
202
+ *x + 0.13692988092273580531)
203
+ *x + 0.59983220655588793769)
204
+ *x + 1.0); /* evaluate the rational function */
205
+ }
206
+ return (prob < 0.5) ? -x : x; /* retransform to right tail if nec. */
207
+ } /* unitqtlP() */
208
+
209
+ #endif
210
+ /*----------------------------------------------------------------------
211
+ References: - R.E. Odeh and J.O. Evans.
212
+ The percentage points of the normal distribution.
213
+ Applied Statistics 22:96--97, 1974
214
+ - J.D. Beasley and S.G. Springer.
215
+ Algorithm AS 111:
216
+ The percentage points of the normal distribution.
217
+ Applied Statistics 26:118--121, 1977
218
+ - M.J. Wichura
219
+ Algorithm AS 241:
220
+ The percentage points of the normal distribution.
221
+ Applied Statistics 37:477--484, 1988
222
+ ----------------------------------------------------------------------*/
223
+
224
+ double unitrand (double randfn(void))
225
+ { /* --- compute N(0,1) distrib. number */
226
+ static double b; /* buffer for random number */
227
+ double x, y, r; /* coordinates and radius */
228
+
229
+ if (b != 0.0) { /* if the buffer is full, */
230
+ x = b; b = 0; return x; } /* return the buffered number */
231
+ do { /* pick a random point */
232
+ x = 2.0*randfn()-1.0; /* in the unit square [-1,1]^2 */
233
+ y = 2.0*randfn()-1.0; /* and check whether it lies */
234
+ r = x*x +y*y; /* inside the unit circle */
235
+ } while ((r > 1) || (r == 0));
236
+ r = sqrt(-2*log(r)/r); /* factor for Box-Muller transform */
237
+ b = x *r; /* save one of the random numbers */
238
+ return y *r; /* and return the other */
239
+ } /* unitrand() */
240
+
241
+ /*----------------------------------------------------------------------
242
+ Source for the polar method to generate normally distributed numbers:
243
+ D.E. Knuth.
244
+ The Art of Computer Programming, Vol. 2: Seminumerical Algorithms
245
+ Addison-Wesley, Reading, MA, USA 1998
246
+ pp. 122-123
247
+ ----------------------------------------------------------------------*/
248
+
249
+ double normpdf (double x, double mean, double var)
250
+ { /* --- probability density function */
251
+ assert(var >= 0); /* check the function arguments */
252
+ if (var == 1) return unitpdf(x-mean);
253
+ return unitpdf((x-mean) /sqrt(var));
254
+ } /* normpdf() */
255
+
256
+ /*--------------------------------------------------------------------*/
257
+
258
+ double normcdfP (double x, double mean, double var)
259
+ { /* --- cumulative distribution fn. */
260
+ assert(var >= 0); /* check the function arguments */
261
+ if (var == 1) return unitcdf(x-mean);
262
+ return unitcdf((x-mean) /sqrt(var));
263
+ } /* normcdfP() */
264
+
265
+ /*--------------------------------------------------------------------*/
266
+
267
+ double normcdfQ (double x, double mean, double var)
268
+ { /* --- cumulative distribution fn. */
269
+ assert(var >= 0); /* check the function arguments */
270
+ if (var == 1) return unitcdfP(mean-x);
271
+ return unitcdfP((mean-x) /sqrt(var));
272
+ } /* normcdfQ() */
273
+
274
+ /*--------------------------------------------------------------------*/
275
+
276
+ double normqtlP (double prob, double mean, double var)
277
+ { /* --- quantile of normal distrib. */
278
+ assert((var > 0) /* check the function arguments */
279
+ && (prob >= 0) && (prob <= 1));
280
+ if (var == 1) return mean +unitqtl(prob);
281
+ return mean +unitqtlP(prob) *sqrt(var);
282
+ } /* normqtlP() */
283
+
284
+ /*--------------------------------------------------------------------*/
285
+
286
+ double normqtlQ (double prob, double mean, double var)
287
+ { /* --- quantile of normal distrib. */
288
+ assert((var > 0) /* check the function arguments */
289
+ && (prob >= 0) && (prob <= 1));
290
+ if (var == 1) return mean -unitqtlP(prob);
291
+ return mean -unitqtlP(prob) *sqrt(var);
292
+ } /* normqtlQ() */
293
+
294
+ /*--------------------------------------------------------------------*/
295
+
296
+ double normrand (double randfn(void), double mean, double var)
297
+ { /* --- cumulative distribution fn. */
298
+ assert(var >= 0); /* check the function arguments */
299
+ if (var == 1) return mean +unitrand(randfn);
300
+ return mean +unitrand(randfn) *sqrt(var);
301
+ } /* normrand() */
302
+
303
+ /*----------------------------------------------------------------------
304
+ Main Functions
305
+ ----------------------------------------------------------------------*/
306
+ #ifdef NORMPDF_MAIN
307
+
308
+ int main (int argc, char *argv[])
309
+ { /* --- main function */
310
+ double mean = 0; /* mean value */
311
+ double var = 1; /* variance */
312
+ double x; /* argument value */
313
+
314
+ if ((argc < 2) || (argc > 4)){/* if wrong number of arguments */
315
+ printf("usage: %s arg [mean variance]\n", argv[0]);
316
+ printf("compute probability density function "
317
+ "of the normal distribution\n");
318
+ return 0; /* print a usage message */
319
+ } /* and abort the program */
320
+ x = atof(argv[1]); /* get the argument value */
321
+ if (argc > 2) mean = atof(argv[2]);
322
+ if (argc > 3) var = atof(argv[3]);
323
+ if (var < 0) { /* get the parameters */
324
+ printf("%s: invalid variance\n", argv[0]); return -1; }
325
+ printf("normal: f(%.16g; %.16g, %.16g) = %.16g\n",
326
+ x, mean, var, normpdf(x, mean, var));
327
+ return 0; /* compute and print density */
328
+ } /* main() */
329
+
330
+ #endif
331
+ /*--------------------------------------------------------------------*/
332
+ #ifdef NORMCDF_MAIN
333
+
334
+ int main (int argc, char *argv[])
335
+ { /* --- main function */
336
+ double mean = 0; /* mean value */
337
+ double var = 1; /* variance */
338
+ double x; /* argument value */
339
+
340
+ if ((argc < 2) || (argc > 4)){/* if wrong number of arguments */
341
+ printf("usage: %s arg [mean variance]\n", argv[0]);
342
+ printf("compute cumulative distribution function "
343
+ "of the normal distribution\n");
344
+ return 0; /* print a usage message */
345
+ } /* and abort the program */
346
+ x = atof(argv[1]); /* get the argument value */
347
+ if (argc > 2) mean = atof(argv[2]);
348
+ if (argc > 3) var = atof(argv[3]);
349
+ if (var < 0) { /* get the parameters */
350
+ printf("%s: invalid variance\n", argv[0]); return -1; }
351
+ printf("normal: F(% .16g; %.16g, %.16g) = %.16g\n",
352
+ x, mean, var, normcdfP(x, mean, var));
353
+ printf(" 1 - F(% .16g; %.16g, %.16g) = %.16g\n",
354
+ x, mean, var, normcdfQ(x, mean, var));
355
+ return 0; /* compute and print probability */
356
+ } /* main() */
357
+
358
+ #endif
359
+ /*--------------------------------------------------------------------*/
360
+ #ifdef NORMQTL_MAIN
361
+
362
+ int main (int argc, char *argv[])
363
+ { /* --- main function */
364
+ double mean = 0; /* mean value */
365
+ double var = 1; /* variance */
366
+ double prob; /* probability */
367
+
368
+ if ((argc < 2) || (argc > 4)){/* if wrong number of arguments */
369
+ printf("usage: %s prob [mean variance]\n", argv[0]);
370
+ printf("compute quantile of the normal distribution\n");
371
+ return 0; /* print a usage message */
372
+ } /* and abort the program */
373
+ prob = atof(argv[1]); /* get the probability */
374
+ if ((prob < 0) || (prob > 1)) {
375
+ printf("%s: invalid probability\n", argv[0]); return -1; }
376
+ if (argc > 2) mean = atof(argv[2]);
377
+ if (argc > 3) var = atof(argv[3]);
378
+ if (var < 0) { /* get the parameters */
379
+ printf("%s: invalid variance\n", argv[0]); return -1; }
380
+ printf("normal: F(% .16g; %.16g, %.16g) = %.16g\n",
381
+ normqtlP(prob, mean, var), mean, var, prob);
382
+ printf(" 1 - F(% .16g; %.16g, %.16g) = %.16g\n",
383
+ normqtlQ(prob, mean, var), mean, var, prob);
384
+ return 0; /* compute and print quantile */
385
+ } /* main() */
386
+
387
+ #endif