summaryrefslogtreecommitdiff
path: root/info/maad/pgm4.c
blob: 883cb6a2f8c43cf55867218fefc47628d175c2af (plain)
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
#include <stdio.h>        /* Header for input/output subroutines. */
#include <math.h>         /* Header for math subroutines. */
#include <float.h>        /* Header for floating point subroutines. */

#define pi 3.141592653589793238462643383279     /* Accurate value for pi. */

/* Simpson's rule for approximating integrals.
    a:              left endpoint
    b:              right endpoint
    fc:             pointer to function to integrate
    n:              number of subintervals
*/
double fc (double x);

main()

double a,b,h,sum,x,y,p,r,psum;   /* In 'C' all variables must be assigned */
double p1, p2, p3;
int i, n, o;
printf("\007");           /* Sound bell in computer. */
a = (double) 0.0;         /* The normalizing integral is */
b = (double) 1.0;         /* from 0 to 1.  */
n = 256;  /* This has been tested and seen to give 5 decimal accuracy. */
printf("\n Enter number of points ");
scanf("%d",&o);
if (o < 5){
    printf("Too few points"); return(o);

p = (double) o;
h = (double) (b-a)/n;
  for (i=1, sum=0.0; i<=n; i = i+ 2)
    p1 = fc((double) a+(i-1)*h, p);
    p2 = fc((double) a+i*h, p);
    p3 = fc((double) a+(i+1)*h, p);
    sum += p1 + 4.0 * p2 + p3;


psum = (double) h*sum/3.0; /* This normalizes the integral */
/* For a given value of N, P\_N[|r|>=0] (must)) = 1.0 */
/* We assure this (and eliminate the need to compute some */
/* Gamma function) in computing the value in "psum." */

printf("\n Enter correlation coefficient r = ");
scanf("%lf", &r); a = fabs((double) r);
if (a > 1) a = (double) 1.0;
    /* r\_0 must be between -1 and 1.  Force a to assume a value */
    /* between 0 and 1. */
h = (double) (b-a)/n;
  for (i=1, sum=0.0; i<=n; i = i+ 2)
    p1 = fc((double) a+(i-1)*h, p);
    p2 = fc((double) a+i*h, p);
    p3 = fc((double) a+(i+1)*h, p);
    sum += p1 + 4.0 * p2 + p3;


y = (double) h*sum/3.0;
printf("\n Percentage probability that N measurements");
printf("\n of two uncorrelated variables would give an r");
printf("\n as large as r\_0, %.2lf, that is ", a);
printf("\n P\_N[|r|>=r\_0] = %.2lf (per cent).", (double) 100.*y/psum);
printf("\n If the number %.2lf is less than 5 (per cent),", 100.*y/psum);
printf("\n then the correlation is called significant.");
printf("\n If the number %.2lf is less than 1 (per cent),", 100.*y/psum);
printf("\n then the correlation is call highly significant.");
printf("\n Given this simple program and its speedy calculation");
printf("\n of the significance of a correlation in absolute terms,");
printf("\n we only have to plug in the number of data points and,");
printf("\n the value gotten from Pearson's correlation coefficient, r.");
printf("\n");
return(0);


double fc (double x, double p)
  
    double y;
    y = (double) pow((double)(1.0-x*x),(double) (p-4.0)/2.0);
    return (y);


/* End of file */