1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
|
/* -*- mode: C; mode: fold; -*- */
/*
Copyright (c) 2007 Massachusetts Institute of Technology
This software was developed by the MIT Center for Space Research
under contract SV1-61010 from the Smithsonian Institution.
Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in
the supporting documentation, and that the name of the Massachusetts
Institute of Technology not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission. The Massachusetts Institute of Technology makes
no representations about the suitability of this software for any
purpose. It is provided "as is" without express or implied warranty.
THE MASSACHUSETTS INSTITUTE OF TECHNOLOGY DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE MASSACHUSETTS
INSTITUTE OF TECHNOLOGY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Author: John E. Davis <davis@space.mit.edu> */
#include "config.h"
#include <stdio.h>
#include <slang.h>
#include <math.h>
#ifndef PI
#define PI 3.14159265358979323846264338327950288
#endif
/* <MODULE_INCLUDES> */
#ifdef __cplusplus
extern "C"
{
#endif
SLANG_MODULE(stats);
#ifdef __cplusplus
}
#endif
#define MODULE_VERSION_STRING "1.0.2"
#define MODULE_VERSION_NUMBER 10002
#if 0
double anderson_darling_statistic (double *f, unsigned int num)
{
double s;
double n;
n = (double)num;
for (i = 0; i < n; i++)
{
((2*i+1)/n)* (log(f[i]) + log(f[n-i]));
}
}
#endif
/* Computes (n k) = n!/(k! (n-k)!) */
static double compute_binomial_coeff (unsigned int n, unsigned int k) /*{{{*/
{
double f;
unsigned int i;
if (k > n)
return 0.0;
if ((k == 0) || (k == n))
return 1.0;
if (n-k < k)
k = n-k;
f = n;
for (i = 2; i <= k; i++)
{
n--;
f = (f/i)*n;
}
return f;
}
/*}}}*/
/* This function implements the _asymptotic_ form of the kolmogorov distribution
* for large N. Asymptotically, this is given by the series expansion:
*
* S(x) = 1 + 2\sum_{j=1}^{\infty} (-1)^j \exp(-2j^2 x^2)
*
* This is equivalent to
*
* S(x) = \sqrt{2\pi}/x \sum_{j=1}^\infty \exp[-(2j-1)^2\pi^2/(8x^2)]
*
* See eq. 1.4 of W. Feller, "On the Kolmogorov-Smirnov limit theorems for
* empirical distributions", Annals of Math. Stat, Vol 19 (1948), pp. 177-190.
*
* S(x) represents the prob of obtaining a (normalized) statistic <= x.
*
* I chose the value of 1.09 was chosen based upon some emperical testing. Of
* course this value will most likely vary with compiler.
*/
static double smirnov_cdf (double x) /*{{{*/
{
int j;
unsigned int max_loops;
double a, b, sum;
max_loops = 5000;
if (x <= 0.15)
{
if (x < 0.0)
{
SLang_set_error (SL_INVALID_PARM);
return -1.0;
}
return 0.0;
}
if (x > 1.09)
{
int j3;
if (x > 19.4)
return 1.0;
x = 2*x*x;
sum = 0.0;
j = 1;
j3 = 3;
while (max_loops)
{
double dsum = exp(-j*j*x) * (1.0 - exp (-j3*x));
sum += dsum;
if (dsum == 0)
return 1-2*sum;
j += 2;
j3 += 4;
max_loops--;
}
return 1.0;
}
/* Use sqrt(X) = 0.5*log(X) */
b = log(sqrt(2*PI)/x);
a = (PI*PI/(8.0*x*x));
sum = 0.0;
j = 1;
while (max_loops)
{
double dsum = exp (-a*j*j + b);
sum += dsum;
if (dsum == 0.0)
return sum;
j += 2;
max_loops--;
}
return 0.0;
}
/*}}}*/
/* This gives the probability that an observed value of D_mn <= c/(mn).
*/
static double kim_jennrich_cdf (unsigned int m, unsigned int n, unsigned int c) /*{{{*/
{
unsigned int i, j;
double p;
double *u;
if (m > n)
{
unsigned int tmp = m;
m = n;
n = tmp;
}
u = (double *)SLmalloc (sizeof (double)*(n+1));
if (u == NULL)
return -1.0;
u[0] = 1.0;
for (j = 1; j <= n; j++)
{
u[j] = 1.0;
if (m*j > c)
u[j] = 0.0;
}
for (i = 1; i <= m; i++)
{
double w = i/(i + (double)n);
unsigned int ni = n*i;
u[0] = w*u[0];
if (ni > c) u[0] = 0;
for (j = 1; j <= n; j++)
{
unsigned int mj = m*j;
unsigned int diff;
diff = (ni >= mj) ? (ni-mj) : (mj-ni);
u[j] = (diff > c) ? 0.0 : u[j-1]+u[j]*w;
}
}
p = u[n];
if (p > 1.0) p = 1.0;
else if (p < 0.0) p = 0.0;
SLfree ((char *)u);
return p;
}
/*}}}*/
/* P(X<=s) */
static double mann_whitney_cdf (unsigned int m, unsigned int n, unsigned int s) /*{{{*/
{
unsigned int M, p;
unsigned int m_plus_n, t, u;
double *f, c, cf;
/* min allowed s: 1+2+...m = m(m+1)/2 = min
* max allowed s: (n+1)+(n+2)+ .. (n+m) = n*m + min
* avg = (min+max)/2 = nm/2 + m(m+1)/2 = (m/2)[n+ m+1]
*/
M = m*(m+1)/2;
if (s < M)
return 0.0;
M += m*n;
if (M <= s)
return 1.0;
M = m*n/2;
f = (double *)SLmalloc ((M+1)*sizeof(double));
if (f == NULL)
return -1.0;
f[0] = 1.0;
for (p = 1; p <= M; p++)
f[p] = 0.0;
m_plus_n = m + n;
if (n + 1 < M)
{
p = m_plus_n;
if (p > M) p = M;
for (t = n + 1; t <= p; t++)
{
for (u = M; u >= t; u--)
f[u] -= f[u-t];
}
}
p = m;
if (M < p) p = M;
for (t = 1; t <= p; t++)
{
for (u = t; u <= M; u++)
f[u] += f[u-t];
}
c = compute_binomial_coeff (m+n, m);
cf = 0.0;
for (t = 0; t <= M; t++)
{
cf += f[t]/c;
f[t] = cf;
}
s -= m*(m+1)/2;
if (s > M)
{
s = m*n - s;
c = 1.0 - f[s];
}
else c = f[s];
SLfree ((char *) f);
return c;
}
/*}}}*/
/*{{{ Gamma Function */
#ifdef HAVE_LGAMMA
# define JDMlog_gamma lgamma
#else
/* The following code is adapted from my JDMath library */
/* This implementation is derived from
* Spouge JL. Computation of the gamma, digamma, and trigamma
* functions. SIAM J Numerical Anal 1994;31:931-44.
* as pointed out by Glynne Casteel <glynnec@ix.netcom.com> in the
* sci.math.num-analysis article <6potip$l56@sjx-ixn4.ix.netcom.com>.
*/
#define NUM_COEFFS 18 /* make this even */
static double Param = NUM_COEFFS + 1; /* 0.840 */
/* determined empirically using the driver in main at end of the file. */
static double Coeffs[NUM_COEFFS+1];
static int Coeffs_Initialized = 0;
static void init_coefficients (void)
{
int i;
double e = 2.718281828459045235360287471352;
double f = 2.506628274631000502415765284809; /* sqrt(2pi) */
Coeffs[0] = exp (-Param)*f;
Coeffs[1] = sqrt (Param-1)/e;
for (i = 1; i < NUM_COEFFS; i++)
{
register double x = Param - i;
Coeffs[i+1] = Coeffs[i]
* ((x - 1)*pow (1-1/x,i-0.5)/(i*e));
}
Coeffs_Initialized = 1;
}
static double JDMlog_gamma (double x)
{
register double sum;
unsigned int i;
if (Coeffs_Initialized == 0)
init_coefficients ();
x -= 1.0;
sum = Coeffs[0];
i = 1;
while (i < NUM_COEFFS)
{
register double dsum;
dsum = Coeffs[i]/(x+i);
i++;
sum += (dsum - Coeffs[i]/(x+i));
i++;
}
return log(sum) + ((x+0.5)*log(x+Param) - x);
}
#endif /* HAVE_LGAMMA */
/* See A&S 6.5.4 for a definition. Here, the series expansion given by
* A&S 6.5.29 is used:
*
* gamma_star(a,x) = exp(-x) \sum_n x^n/Gamma(a+n+1)
*
* Here, Gamma(a+1)=a*Gamma(a), Gamma(a+2)=(a+1)*a*Gamma(a), ....
* Thus,
*
* gamma_star(a,x) = exp(-x)/Gamma(a) \sum_n x^n c_n
*
* where c_n = 1/(a(a+1)...(a+n)). For a > 0, c_n --> 0, hence based upon
* this c_n, the sum will terminate. However, x^n may overflow before this
* happens. However, as long as |x| < a, this should not happen. So avoid
* this function if |x| > a.
*/
static double JDMlog_gamma_star (double a, double x)
{
double sum;
int n;
double cnxn;
double eps = 2.220446049250313e-16;
if (a == 0.0)
return 0.0;
if (a < 0.0)
{
/* FIXME!!! Handle a < 0 */
}
cnxn = 1.0 / a;
sum = cnxn;
n = 0;
while (n < 5000)
{
n++;
cnxn *= x/(a+n);
if (cnxn < sum*eps)
break;
sum += cnxn;
}
return log (sum) - x - JDMlog_gamma (a);
}
/* This incomplete gamma function has a continued fraction approximation
* given by A&S 6.5.31. This expansion may be used to compute the incomplete
* Gamma function (A&S 6.5.1) via A&S 6.5.2,3:
* Gamma(a,x) = Gamma(a) - P(a,x) Gamma(a)
* = Gamma(a) - gamma(a,x)
* or
* P(a,x) = 1 - Gamma(a,x)/Gamma(a).
*
* The continued fraction expansion (A&S 6.5.31) is given by:
*
* Gamma(a,x) = exp(-x)x^a (1/x+ (1-a)/1+ 1/x+ (2-a)/1+ 2/x+ ...)
*
* Use the recursion relations in theorem 2 of A&S 3.10.1 to evaluate this.
* That is, let f_n be defined as
*
* f_n = b_0 + a_1/b_1+ a_2/b_2+...a_n/b_n := A_n/B_n
*
* Then:
*
* A_n = b_n A_{n-1} + a_n A_{n-2}
* B_n = b_n B_{n-1} + a_n B_{n-2}
*
* where A_{-1}=1, A_0=b_0, B_{-1}=0, B_0=1
*/
static double JDMlog_CapGamma (double a, double x)
{
double a1, a0, b0, b1;
double f;
double renorm_factor;
double eps = 2.220446049250313e-16;
int n;
if (x <= 0.0)
return log(x); /* NaN/-Inf */
/* Set up the recursion at the initial 1/x+ piece */
a1 = 1;
b1 = x;
a0 = 0;
b0 = 1;
renorm_factor = 1.0/b1;
f = a1 * renorm_factor;
n = 1;
if (renorm_factor != 0.0) while (n < 5000)
{
double f1, aa;
/* Note that the renormalization factor is 1/b1.
* So, replace renorm_factor*b1 combinations by 1 */
aa = n - a;
/* Now the (n-a)/(1+??) piece */
a0 = (a1 + aa * a0) * renorm_factor;
b0 = (b1 + aa * b0) * renorm_factor;
/* Since a0,b0 now have the renorm_factor applied, omit it below */
/* Handle {1,2,3...}/(x+??) piece */
a1 = x * a0 + n * a1 * renorm_factor;
/* b1 = x * b0 + n * b1 * renorm_factor;*/
b1 = x * b0 + n;
n++;
if (b1 == 0.0)
continue;
renorm_factor = 1.0 / b1;
f1 = a1 * renorm_factor;
if (fabs (f - f1) < eps * fabs(f1))
{
f = f1;
break;
}
f = f1;
}
return a * log(x) - x + log (f);
}
/* See A&S 6.5.1
* This is P(a,x) = gamma(a,x)/Gamma(a)
*/
static double JDMincomplete_gamma (double a, double x)
{
if (a <= 0.0)
return log(a);
if (x >= a)
return 1.0 - exp (JDMlog_CapGamma (a,x) - JDMlog_gamma (a));
return exp (a * log(x) + JDMlog_gamma_star (a, x));
}
/*}}}*/
/*{{{ Incomplete Beta */
static int incbeta_cfe (double x, double a, double b, double *result)
{
unsigned int m, maxm = 1024;
double c = a+b;
double alpha_neg2, alpha_neg1, beta_neg2, beta_neg1;
double eps = 1e-14;
double last_f;
double factor;
factor = a*log(x)+b*log(1.0-x);
factor = a*log(x)+b*log1p(-x);
factor += JDMlog_gamma(a+b)-JDMlog_gamma(a) - JDMlog_gamma(b);
factor = exp (factor)/a;
/* F = 1/(1+(d1/(1+(d2/(1+....)))))
* F0 = 1/1
* F1 = 1/1+ d1
* F2 = 1/1+ d1/1+ d2
* F_n = alpha_n/beta_n
* alpha_n = alpha_{n-1} + d_n*alpha_{n-2}
* beta_n = beta_{n-1} + d_n*beta_{n-2}
* (d0 = 1)
* alpha_{-2} = 1, alpha{-1} = 0, alpha_{0} = 1, alpha_1 = 1,
* beta_{-2} = 0, beta_{-1} = 1, beta_{0} = 1, beta_1 = (1+d1),
* alpha_2 = 1+d2
* beta_2 = (1+d1+d2)
* alpha_2/beta_2 = 1/(1+d1/(1+d2)) ok
*/
/* This loop computes results for d2, d3, ... So initialize for d1 term */
alpha_neg2 = 1.0; alpha_neg1 = 1.0;
beta_neg2 = 1.0; beta_neg1 = 1.0-c/(a+1)*x;
last_f = alpha_neg1/beta_neg1;
m = 1;
while (m < maxm)
{
double d,g;
unsigned int m2 = 2*m;
g = a+m2;
/* even d_2m */
d = (m*(b-m)/((g-1.0)*g))*x;
alpha_neg2 = alpha_neg1 + d*alpha_neg2;
beta_neg2 = beta_neg1 + d*beta_neg2;
/* odd d_{2m+1} */
d = -(((a+m)*(c+m))/(g*(g+1.0)))*x;
alpha_neg1 = alpha_neg2 + d*alpha_neg1;
beta_neg1 = beta_neg2 + d*beta_neg1;
/* Renormalize to prevent large alpha/beta */
alpha_neg1 /= beta_neg1;
alpha_neg2 /= beta_neg1;
beta_neg2 /= beta_neg1;
beta_neg1 = 1.0;
if (fabs(alpha_neg1 - last_f) < eps * fabs(alpha_neg1))
{
*result = alpha_neg1*factor;
return 0;
}
last_f = alpha_neg1;
m++;
}
*result = last_f*factor;
return -1;
}
static int incbeta (double x, double a, double b, double *result)
{
double cfe;
int status;
if ((x < 0.0) || (x > 1.0))
{
SLang_verror (SL_INVALID_PARM, "Domain error for x in incbeta");
*result = -1;
return -1;
}
if ((x == 0.0) || (x == 1.0))
{
*result = x;
return 0;
}
/* Use 26.5.8 of A&S --- Note that this condition is different from that
* of A&S --- GSL uses this one and it seems to work better.
*/
if (x * (a+b+2.0) < (a+1.0))
{
status = incbeta_cfe (x, a, b, &cfe);
}
else
{
/* Use I_x(a,b) = 1.0 - I_{1-x}(b,a) */
status = incbeta_cfe (1.0-x, b, a, &cfe);
cfe = 1.0 - cfe;
}
*result = cfe;
return status;
}
/*}}}*/
/*{{{ Intrinsics */
/* See A&S 6.5.13
*
* This computes: \sum_{0<=k<=n} lambda^k/k! exp(-lambda)
* From A&S 6.5.13, this is just 1-P(n+1,lambda)
*/
static double poisson_cdf_intrin (double *lambdap, int *np)
{
int n = *np;
double lambda = *lambdap;
if (n < 0)
return 0.0;
n = n+1;
if (lambda > 1000.0)
{
double dn = sqrt(n);
if (fabs(lambda - n) < dn)
{
/* Wilson and Hilferty, 1951 */
double c = pow (lambda/n, 1.0/3.0);
double mu = 1.0-1.0/(9.0*n);
double s = 1.0/(3*dn);
return 0.5 * (1 - erf((c-mu)/s/sqrt(2.0)));
}
/* drop */
}
return 1.0 - JDMincomplete_gamma ((double)n, lambda);
}
/* P(X^2 < chisqr)
* A&S 26.4.19
*/
static double chisqr_cdf_intrin (int *dofp, double *chisqrp)
{
int dof = *dofp;
double chisqr = *chisqrp;
if (dof <= 0)
{
SLang_verror (SL_INVALID_PARM, "The number of degrees of freedom should be positive");
return -1.0;
}
if (chisqr < 0)
{
SLang_verror (SL_INVALID_PARM, "Expecting a non-negative value for the chi-square statistic");
return -1.0;
}
return JDMincomplete_gamma (0.5*dof, 0.5*chisqr);
}
static double smirnov_cdf_intrin (double *x)
{
return smirnov_cdf (*x);
}
static double kim_jennrich_cdf_intrin (unsigned int *m, unsigned int *n, unsigned int *c)
{
return kim_jennrich_cdf (*m, *n, *c);
}
static double mann_whitney_cdf_intrin (unsigned int *m, unsigned int *n, double *s)
{
unsigned int sn = (unsigned int) (*s + 0.5);
return mann_whitney_cdf (*m, *n, sn);
}
/* df needs to be real valued for the welch test */
static double student_t_cdf_intrin (double *t, double *dfp)
{
double x = *t;
double n = *dfp;
double cdf;
(void) incbeta (n/(n+x*x), 0.5*n, 0.5, &cdf);
cdf *= 0.5;
if (x > 0)
cdf = 1.0 - cdf;
return cdf;
}
static double f_cdf_intrin (double *t, double *nu1p, double *nu2p)
{
double x = *t;
double cdf;
double nu1 = *nu1p, nu2 = *nu2p;
if (x < 0.0)
return 0.0;
(void) incbeta (nu2/(nu2+nu1*x), 0.5*nu2, 0.5*nu1, &cdf);
return 1.0-cdf;
}
/*}}}*/
/* Mean, Stdev, etc... */
#define GENERIC_TYPE double
#define MEAN_FUNCTION mean_doubles
#define MEAN_RESULT_TYPE double
#define STDDEV_FUNCTION stddev_doubles
#define STDDEV_RESULT_TYPE double
#define MEDIAN_FUNCTION median_doubles
#define NC_MEDIAN_FUNCTION nc_median_doubles
#include "stats-module.inc"
#define GENERIC_TYPE float
#define MEAN_FUNCTION mean_floats
#define MEAN_RESULT_TYPE float
#define STDDEV_FUNCTION stddev_floats
#define STDDEV_RESULT_TYPE float
#define MEDIAN_FUNCTION median_floats
#define NC_MEDIAN_FUNCTION nc_median_floats
#include "stats-module.inc"
#define GENERIC_TYPE int
#define MEAN_FUNCTION mean_ints
#define MEAN_RESULT_TYPE double
#define STDDEV_FUNCTION stddev_ints
#define STDDEV_RESULT_TYPE double
#define MEDIAN_FUNCTION median_ints
#define NC_MEDIAN_FUNCTION nc_median_ints
#include "stats-module.inc"
#define GENERIC_TYPE unsigned int
#define MEAN_FUNCTION mean_uints
#define MEAN_RESULT_TYPE double
#define STDDEV_FUNCTION stddev_uints
#define STDDEV_RESULT_TYPE double
#define MEDIAN_FUNCTION median_uints
#define NC_MEDIAN_FUNCTION nc_median_uints
#include "stats-module.inc"
#if SIZEOF_LONG != SIZEOF_INT
# define GENERIC_TYPE long
# define MEAN_FUNCTION mean_longs
# define MEAN_RESULT_TYPE double
# define STDDEV_FUNCTION stddev_longs
# define STDDEV_RESULT_TYPE double
# define MEDIAN_FUNCTION median_longs
# define NC_MEDIAN_FUNCTION nc_median_longs
# include "stats-module.inc"
# define GENERIC_TYPE unsigned long
# define MEAN_FUNCTION mean_ulongs
# define MEAN_RESULT_TYPE double
# define STDDEV_FUNCTION stddev_ulongs
# define STDDEV_RESULT_TYPE double
# define MEDIAN_FUNCTION median_ulongs
# define NC_MEDIAN_FUNCTION nc_median_ulongs
# include "stats-module.inc"
#else
# define mean_longs mean_ints
# define stddev_longs stddev_ints
# define median_longs median_ints
# define nc_median_longs nc_median_ints
# define mean_ulongs mean_uints
# define stddev_ulongs stddev_uints
# define median_ulongs median_uints
# define nc_median_ulongs nc_median_uints
#endif
#if SIZEOF_SHORT != SIZEOF_INT
# define GENERIC_TYPE short
# define MEAN_FUNCTION mean_shorts
# define MEAN_RESULT_TYPE float
# define STDDEV_FUNCTION stddev_shorts
# define STDDEV_RESULT_TYPE float
# define MEDIAN_FUNCTION median_shorts
# define NC_MEDIAN_FUNCTION nc_median_shorts
# include "stats-module.inc"
# define GENERIC_TYPE unsigned short
# define MEAN_FUNCTION mean_ushorts
# define MEAN_RESULT_TYPE float
# define STDDEV_FUNCTION stddev_ushorts
# define STDDEV_RESULT_TYPE float
# define MEDIAN_FUNCTION median_ushorts
# define NC_MEDIAN_FUNCTION nc_median_ushorts
# include "stats-module.inc"
#else
# define mean_shorts mean_ints
# define stddev_shorts stddev_ints
# define median_shorts median_ints
# define nc_median_shorts nc_median_ints
# define mean_ushorts mean_uints
# define stddev_ushorts stddev_uints
# define median_ushorts median_uints
# define nc_median_ushorts nc_median_uints
#endif
#define GENERIC_TYPE char
#define MEAN_FUNCTION mean_chars
#define MEAN_RESULT_TYPE float
#define STDDEV_FUNCTION stddev_chars
#define STDDEV_RESULT_TYPE float
#define MEDIAN_FUNCTION median_chars
#define NC_MEDIAN_FUNCTION nc_median_chars
#include "stats-module.inc"
#define GENERIC_TYPE unsigned char
#define MEAN_FUNCTION mean_uchars
#define MEAN_RESULT_TYPE float
#define STDDEV_FUNCTION stddev_uchars
#define STDDEV_RESULT_TYPE float
#define MEDIAN_FUNCTION median_uchars
#define NC_MEDIAN_FUNCTION nc_median_uchars
#include "stats-module.inc"
static SLarray_Contract_Type Stddev_Functions [] =
{
{SLANG_CHAR_TYPE, SLANG_CHAR_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) stddev_chars},
{SLANG_UCHAR_TYPE,SLANG_UCHAR_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) stddev_uchars},
{SLANG_SHORT_TYPE,SLANG_SHORT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) stddev_shorts},
{SLANG_USHORT_TYPE, SLANG_USHORT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) stddev_ushorts},
{SLANG_INT_TYPE, SLANG_INT_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) stddev_ints},
{SLANG_UINT_TYPE, SLANG_UINT_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) stddev_uints},
{SLANG_LONG_TYPE, SLANG_LONG_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) stddev_longs},
{SLANG_ULONG_TYPE, SLANG_ULONG_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) stddev_ulongs},
{SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) stddev_floats},
{SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) stddev_doubles},
{0, 0, 0, NULL}
};
static void stddev_intrin (void)
{
if (SLang_Num_Function_Args == 0)
{
SLang_verror (SL_Usage_Error, "x = stddev(X [,dim])");
return;
}
(void) SLarray_contract_array (Stddev_Functions);
}
static SLCONST SLarray_Contract_Type Mean_Functions [] =
{
{SLANG_CHAR_TYPE, SLANG_CHAR_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) mean_chars},
{SLANG_UCHAR_TYPE, SLANG_UCHAR_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) mean_uchars},
{SLANG_SHORT_TYPE, SLANG_SHORT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) mean_shorts},
{SLANG_USHORT_TYPE, SLANG_USHORT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) mean_ushorts},
{SLANG_UINT_TYPE, SLANG_UINT_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) mean_uints},
{SLANG_INT_TYPE, SLANG_INT_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) mean_ints},
{SLANG_LONG_TYPE, SLANG_LONG_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) mean_longs},
{SLANG_ULONG_TYPE, SLANG_ULONG_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) mean_ulongs},
{SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) mean_floats},
{SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) mean_doubles},
{0, 0, 0, NULL}
};
static void mean_intrin (void)
{
if (SLang_Num_Function_Args == 0)
{
SLang_verror (SL_Usage_Error, "x = mean(X [,dim])");
return;
}
(void) SLarray_contract_array (Mean_Functions);
}
static SLarray_Contract_Type Median_Functions [] =
{
{SLANG_CHAR_TYPE, SLANG_CHAR_TYPE, SLANG_CHAR_TYPE, (SLarray_Contract_Fun_Type *) median_chars},
{SLANG_UCHAR_TYPE,SLANG_UCHAR_TYPE, SLANG_UCHAR_TYPE, (SLarray_Contract_Fun_Type *) median_uchars},
{SLANG_SHORT_TYPE,SLANG_SHORT_TYPE, SLANG_SHORT_TYPE, (SLarray_Contract_Fun_Type *) median_shorts},
{SLANG_USHORT_TYPE, SLANG_USHORT_TYPE, SLANG_USHORT_TYPE, (SLarray_Contract_Fun_Type *) median_ushorts},
{SLANG_INT_TYPE, SLANG_INT_TYPE, SLANG_INT_TYPE, (SLarray_Contract_Fun_Type *) median_ints},
{SLANG_UINT_TYPE, SLANG_UINT_TYPE, SLANG_UINT_TYPE, (SLarray_Contract_Fun_Type *) median_uints},
{SLANG_LONG_TYPE, SLANG_LONG_TYPE, SLANG_LONG_TYPE, (SLarray_Contract_Fun_Type *) median_longs},
{SLANG_ULONG_TYPE, SLANG_ULONG_TYPE, SLANG_ULONG_TYPE, (SLarray_Contract_Fun_Type *) median_ulongs},
{SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) median_floats},
{SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) median_doubles},
{0, 0, 0, NULL}
};
static SLarray_Contract_Type NC_Median_Functions [] =
{
{SLANG_CHAR_TYPE, SLANG_CHAR_TYPE, SLANG_CHAR_TYPE, (SLarray_Contract_Fun_Type *) nc_median_chars},
{SLANG_UCHAR_TYPE,SLANG_UCHAR_TYPE, SLANG_UCHAR_TYPE, (SLarray_Contract_Fun_Type *) nc_median_uchars},
{SLANG_SHORT_TYPE,SLANG_SHORT_TYPE, SLANG_SHORT_TYPE, (SLarray_Contract_Fun_Type *) nc_median_shorts},
{SLANG_USHORT_TYPE, SLANG_USHORT_TYPE, SLANG_USHORT_TYPE, (SLarray_Contract_Fun_Type *) nc_median_ushorts},
{SLANG_INT_TYPE, SLANG_INT_TYPE, SLANG_INT_TYPE, (SLarray_Contract_Fun_Type *) nc_median_ints},
{SLANG_UINT_TYPE, SLANG_UINT_TYPE, SLANG_UINT_TYPE, (SLarray_Contract_Fun_Type *) nc_median_uints},
{SLANG_LONG_TYPE, SLANG_LONG_TYPE, SLANG_LONG_TYPE, (SLarray_Contract_Fun_Type *) nc_median_longs},
{SLANG_ULONG_TYPE, SLANG_ULONG_TYPE, SLANG_ULONG_TYPE, (SLarray_Contract_Fun_Type *) nc_median_ulongs},
{SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, SLANG_FLOAT_TYPE, (SLarray_Contract_Fun_Type *) nc_median_floats},
{SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, (SLarray_Contract_Fun_Type *) nc_median_doubles},
{0, 0, 0, NULL}
};
static void median_nc_intrin (void)
{
if (SLang_Num_Function_Args == 0)
{
SLang_verror (SL_Usage_Error, "x = median_nc (X [,dim])");
return;
}
(void) SLarray_contract_array (NC_Median_Functions);
}
static void median_intrin (void)
{
if (SLang_Num_Function_Args == 0)
{
SLang_verror (SL_Usage_Error, "x = median (X [,dim])");
return;
}
(void) SLarray_contract_array (Median_Functions);
}
static void binomial_intrin (void)
{
unsigned int n, k, i;
double f;
SLindex_Type dims;
SLang_Array_Type *at;
double *data;
if (SLang_Num_Function_Args == 2)
{
if ((-1 == SLang_pop_uint (&k)) || (-1 == (SLang_pop_uint (&n))))
return;
(void) SLang_push_double (compute_binomial_coeff (n, k));
return;
}
if (-1 == SLang_pop_uint (&n))
return;
dims = (SLindex_Type) (n+1);
if (NULL == (at = SLang_create_array (SLANG_DOUBLE_TYPE, 0, NULL, &dims, 1)))
return;
data = (double *)at->data;
k = n;
data[0] = 1.0;
data[k] = 1.0;
f = 1.0;
for (i = 1; i <= k; i++)
{
f = (f/i)*k;
k--;
data[i] = data[k] = f;
}
(void) SLang_push_array (at, 1);
}
#if 0
static void erf_intrin (void)
{
int is_float;
SLang_Array_Type *at;
is_float == (SLang_peek_at_stack1 () == SLANG_FLOAT_TYPE);
if (SLang_peek_at_stack () != SLANG_ARRAY_TYPE)
{
double x;
if (is_float)
{
float xf;
if (-1 == SLang_pop_float (&xf))
return;
(void) SLang_push_float ((float) erf((double) xf));
}
if (0 == POP_DOUBLE (&x))
(void) SLang_push_double (x);
return;
}
if (is_float)
{
float *f,
if (-1 == SLang_pop_array_of_type (&at, SLANG_FLOAT_TYPE))
return;
}
xxxxxxxxxx
}
#endif
static double normal_cdf_intrin (double *xp)
{
double x = *xp;
return 0.5 * (1.0 + erf (x/sqrt(2.0)));
}
static SLang_Intrin_Fun_Type Module_Intrinsics [] =
{
MAKE_INTRINSIC_1("smirnov_cdf", smirnov_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE),
MAKE_INTRINSIC_2("chisqr_cdf", chisqr_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_INT_TYPE, SLANG_DOUBLE_TYPE),
MAKE_INTRINSIC_1("_normal_cdf", normal_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE),
MAKE_INTRINSIC_2("student_t_cdf", student_t_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE),
MAKE_INTRINSIC_2("_poisson_cdf", poisson_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_INT_TYPE),
MAKE_INTRINSIC_3("kim_jennrich_cdf", kim_jennrich_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_UINT_TYPE, SLANG_UINT_TYPE, SLANG_UINT_TYPE),
MAKE_INTRINSIC_3("mann_whitney_cdf", mann_whitney_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_UINT_TYPE, SLANG_UINT_TYPE, SLANG_DOUBLE_TYPE),
MAKE_INTRINSIC_3("f_cdf", f_cdf_intrin, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE, SLANG_DOUBLE_TYPE),
MAKE_INTRINSIC_0("mean", mean_intrin, SLANG_VOID_TYPE),
MAKE_INTRINSIC_0("stddev", stddev_intrin, SLANG_VOID_TYPE),
MAKE_INTRINSIC_0("median", median_intrin, SLANG_VOID_TYPE),
MAKE_INTRINSIC_0("median_nc", median_nc_intrin, SLANG_VOID_TYPE),
MAKE_INTRINSIC_0("binomial", binomial_intrin, SLANG_VOID_TYPE),
SLANG_END_INTRIN_FUN_TABLE
};
static SLFUTURE_CONST char *Module_Version_String = MODULE_VERSION_STRING;
static SLang_Intrin_Var_Type Module_Variables [] =
{
MAKE_VARIABLE("_stats_module_version_string", &Module_Version_String, SLANG_STRING_TYPE, 1),
SLANG_END_INTRIN_VAR_TABLE
};
static SLang_IConstant_Type Module_IConstants [] =
{
MAKE_ICONSTANT("_stats_module_version", MODULE_VERSION_NUMBER),
SLANG_END_ICONST_TABLE
};
static SLang_DConstant_Type Module_DConstants [] =
{
SLANG_END_DCONST_TABLE
};
int init_stats_module_ns (char *ns_name)
{
SLang_NameSpace_Type *ns = SLns_create_namespace (ns_name);
if (ns == NULL)
return -1;
if (
(-1 == SLns_add_intrin_var_table (ns, Module_Variables, NULL))
|| (-1 == SLns_add_intrin_fun_table (ns, Module_Intrinsics, NULL))
|| (-1 == SLns_add_iconstant_table (ns, Module_IConstants, NULL))
|| (-1 == SLns_add_dconstant_table (ns, Module_DConstants, NULL))
)
return -1;
return 0;
}
/* This function is optional */
void deinit_stats_module (void)
{
}
|