File: probplot.c

package info (click to toggle)
plotmtv 1.4.1-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,024 kB
  • ctags: 5,006
  • sloc: ansic: 51,179; makefile: 1,976; fortran: 1,277; sh: 510; csh: 439
file content (220 lines) | stat: -rw-r--r-- 5,823 bytes parent folder | download | duplicates (6)
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
/*
 * probplot.c - routines used for probability plots
 *
 * Author:  Sean Casey
 *          Modified 6/93 by Kenny Toh
 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define ERRORRETURN -9.0e30
#define MAXITER  3000
#ifndef EXIT_FAILURE
#  define EXIT_FAILURE 1
#  define EXIT_SUCCESS 0
#endif

#ifndef FLT_EPSILON
#  define FLT_EPSILON 1e-7
#endif

/* Local procedure declarations */
double CNnorm_vert_distance();
#ifdef TEST
static void calc_prob_axes();
#endif


/*
 *
 * double CNnorm_vert_distance(p, rc)
 * Function to return the vertical distance on a probability plot given
 * a fraction between 0 and 1.
 * No allocated memory.
 *
 * This returns a value between -5 and +5.  If x=0.5 (50%), f(x)=0
 */

double CNnorm_vert_distance(p, rc)
double p;        /* Fraction between 0 and 1 */
int   *rc;      /* Return code, gets EXIT_SUCCESS for successful termination */
{
    /* Simple Newton's method plus bisection.  */
    /* Taylored specifically to this application. */
    double   fb = 0, fu, fv;
    double   b, u, v;
    double   sqrt2, derivative, step;
    int   count = 2, nbis = 25, debug = 0;

    *rc = EXIT_FAILURE;
    sqrt2 = sqrt(2.0);
#ifdef DEBUG
    debug = 1;
#endif

    u = -5.0;   /* Upper and lower limits on solution. */
    v =  5.0;   /* These will reach alomost to 1 */

    fu = .5 * ( erf( u / sqrt2 ) + 1) - p;
    fv = .5 * ( erf( v / sqrt2 ) + 1) - p;
 
    if (fu * fv > 0 ) {
        *rc = EXIT_FAILURE;
        return(ERRORRETURN);
    }
    step = v - u;

    for ( count = 0; count <= MAXITER; count++) {

        if ( count % nbis == 0 ) {
            b = u + (v - u) / 2;   /* Bisect the interval. */
            if (debug)
                (void) printf("Bisect\n");
        } else {
            derivative = ( 1 / ( sqrt2 * sqrt(M_PI) * exp(b * b / 2) ) );
            if (debug)
                (void) printf(
                       "Derivative f'(b) = f'(%-8f) = %-10f  step=%-10f  ",
                       b, derivative, fb / derivative);
            step = fb / derivative;
            b -= fb / derivative ;
            if (debug)
                (void) printf("new b=%-10f\n", b);
            if (b < u || b > v) {
                if (debug)
                    (void) printf("b = %-10f   Out of bounds. Bisect.\n", b);
                b = u + (v - u) / 2;   /* Bisect the interval. */
            }
        }

        fb = .5 * ( erf( b / sqrt2 ) + 1 ) - p;

        if (fb * fu > 0) {
            fu = fb;
            u = b;
        } else {
            fv = fb;
            v = b;
        }

        if (debug)
            (void) printf(
                   "count=%d  b=%f fb=%-10f  u=%-10f  v=%-10f  (u-v)=%-10f\n",
                    count, b, fb, u, v, u - v);

        if ( fabs(v - u) <= FLT_EPSILON  || fabs(step) <= FLT_EPSILON ) {
            *rc = EXIT_SUCCESS;
            return b;
        }
    }
    *rc = EXIT_FAILURE;
    return(ERRORRETURN);
}

#ifdef TEST

/*
 * Figure out the position of the axes and tickmarks
 */
static void calc_prob_axes()
{
    /*
     * Figure out the axis position (0-1) for x=0.0001, 0.001, 0.01, 0.1,
     * 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.99, 0.999, 0.9999
     *
     * x=0.0001 => 0
     * x=0.9999 => 1
     */
    
    double upper_lim, lower_lim;
    double x, y, Y, delta;
    int    i,j;
    int    error;

    /* 
     * lower_lim => 0    upper_lim => 1
     *   (y-0)/(1-0) = (Y-lower_lim)/(upper_lim-lower_lim)
     */
    lower_lim = CNnorm_vert_distance(0.0001, &error);
    upper_lim = CNnorm_vert_distance(0.9999, &error);
    
    /* Calculate axes from 0.0001 (0.01%) to 0.10 (10%) */
    delta = 0.0001;
    for (j=0; j<3; j++) {
        x = delta;
        for (i=0; i<10; i++) {
            Y = CNnorm_vert_distance(x, &error);
            y = 100*(Y - lower_lim)/(upper_lim - lower_lim);
            (void) printf("%s x=%10.5f  y=%10.5f\n",(i==0 ? "M" : " "),x,y);
            x += delta; 
        }
        delta = delta*10.0;
        (void) printf("\n");
    }

    /* Calculate axes from 0.1 (10%) to 0.9 (90%) */
    delta = 0.01;
    for (j=1; j<9; j++) {
        x = 0.1*j;
        for (i=0; i<10; i++) {
            Y = CNnorm_vert_distance(x, &error);
            y = 100*(Y - lower_lim)/(upper_lim - lower_lim);
            (void) printf("%s x=%10.5f  y=%10.5f\n",(i==0 ? "M" : " "),x,y);
            x += delta;
        }
        (void) printf("\n");
    }

    /* Calculate axes from 0.9 (90%) to 0.9999 */
    delta = 0.01;
    for (j=0; j<3; j++) {
        x = 1.0 - 10.0*delta;
        for (i=0; i<10; i++) {
            Y = CNnorm_vert_distance(x, &error);
            y = 100*(Y - lower_lim)/(upper_lim - lower_lim);
            (void) printf("%s x=%10.5f  y=%10.5f\n",(i==0 ? "M" : " "),x,y);
            x += delta;
        }
        delta = delta*0.10;
        (void) printf("\n");
   } 
}

main(argc, argv)
int   argc;
char  *argv[];
{
    int   rc, n, i;
    if (argc < 2) {
        (void) printf("To test, type a.out <fraction value>, e.g., a.out .6\n");
        (void) printf("To test, type a.out <fraction value> <integer value>, e.g., a.out .6 10 \n");
        exit(1);
    }

    (void) printf("Input=%f  verticalDistance=%g\n", atof(argv[1]), CNnorm_vert_distance( atof(argv[1]) , &rc ) );
    if (rc == 0)
        (void) printf("SUCCESS\n");
    else
        (void) printf("FAILURE\n");
    if (argc > 2) {
        n = atoi(argv[2]);
        for (i = 1; i <= n; i++) {
             (void) printf("%-10f  verticalDistance=%-10f\n", 
                           (2.0 * i - 1.0) / (2.0 * n), 
                           CNnorm_vert_distance( (2.0 * i -1.0) / (2.0 * n), &rc));
             if (rc != 0)
                 (void) printf("FAILURE\n");
        }
    }

    /* Test the axes routine */
    calc_prob_axes();
 
    exit(0);
}


#endif