orange / source / orange / logreg.cpp

  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
/*

This file contains code from various sources.
Its origins are described at each function separately.

*/


#include "logreg.hpp"

#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))

const double EPSILON = .1192e-06;
const double INF = 1e32;

double lngamma(double z) {
//  Uses Lanczos-type approximation to ln(gamma) for z > 0.
//  Reference:
//       Lanczos, C. 'A precision approximation of the gamma
//               function', J. SIAM Numer. Anal., B, 1, 86-96, 1964.
//  Accuracy: About 14 significant digits except for small regions
//            in the vicinity of 1 and 2.

	double a[9] = {0.9999999999995183, 676.5203681218835,
                      -1259.139216722289, 771.3234287757674, 
                      -176.6150291498386, 12.50734324009056, 
                      -0.1385710331296526, 0.9934937113930748e-05,
					  0.1659470187408462e-06};
	double lnsqrt2pi = 0.9189385332046727;
	double lanczos,tmp;

	int j;

	if (z <= EPSILON)
		return 0;

	lanczos = 0;
	tmp = z + 7;
	for (j = 8; j >= 1; --j) {
	  lanczos = lanczos + a[j]/tmp;
	  tmp = tmp - 1;
	}
	lanczos = lanczos + a[0];
	lanczos = log(lanczos) + lnsqrt2pi - (z + 6.5) + (z - 0.5)*log(z + 6.5);

	return lanczos;
}



double alnorm(double x, bool upper) {
	double norm_prob, con=1.28, z,y, ltone = 7.0, utzero = 18.66;
	double p, r, a2, b1, c1, c3, c5, d1, d3, d5;
	double q, a1, a3, b2, c2, c4, c6, d2, d4;
	bool up;

//  Algorithm AS66 Applied Statistics (1973) vol.22, no.3

// Evaluates the tail area of the standardised normal curve
// from x to infinity if upper is .true. or
// from minus infinity to x if upper is .false.

	
	p = 0.398942280444;
	q = 0.39990348504;
	r = 0.398942280385; 
	a1 = 5.75885480458;
	a2 = 2.62433121679; 
	a3 = 5.92885724438; 
	b1 = -29.8213557807;
	b2 = 48.6959930692;
    c1 = -3.8052e-8;
	c2 = 3.98064794e-4;       
    c3 = -0.151679116635;
	c4 = 4.8385912808;
	c5 = 0.742380924027;
	c6 = 3.99019417011; 
    d1 = 1.00000615302; 
	d2 = 1.98615381364;  
	d3 = 5.29330324926;
	d4 = -15.1508972451;
    d5 = 30.789933034;

	up = upper;
	z = x;
	if(z < 0.0) {
		up = !up;
		z = -z;
	}
	if(z <= ltone || up && z <= utzero) {
		y = 0.5*z*z;
	} else {
		norm_prob = 0.0;
	}
	if(z > con) {
		norm_prob = r*exp(-y)/(z+c1+d1/(z+c2+d2/(z+c3+d3/(z+c4+d4/(z+c5+d5/(z+c6))))));
	} else {
		norm_prob = 0.5 - z*(p-q*y/(y+a1+b1/(y+a2+b2/(y+a3))));
	}
	if(!up) 
		norm_prob = 1.0 - norm_prob;

	return norm_prob;
}


double gammad(double x, double p) {
	//!  ALGORITHM AS239  APPL. STATIST. (1988) VOL. 37, NO. 3
	//!  Computation of the Incomplete Gamma Integral
	//!  Auxiliary functions required: ALNORM = algorithm AS66 (included) & LNGAMMA
	//!  Converted to be compatible with ELF90 by Alan Miller
	//!  N.B. The return parameter IFAULT has been removed as ELF90 allows only
	//!  one output parameter from functions.   An error message is issued instead.
	
	double gamma_prob;
	double pn1, pn2, pn3, pn4, pn5, pn6, tol = 1.e-14, oflo = 1.e+37;
	double xbig = 1.e+8, arg, c, rn, a, b, one = 1.0, zero = 0.0, an;
	double two = 2.0, elimit = -88.0, plimit = 1000.0, three = 3.0;
    double nine = 9;
	
	gamma_prob = zero;
	
	if	(p <= zero || x < EPSILON) {
		return 0.0;
	}
	
	//      Use a normal approximation if P > PLIMIT
	if (p > plimit) {
		pn1 = three * sqrt(p) * (pow(x/p,one/three) + one / (nine * p) - one);
		return alnorm(pn1, false);
	}
	
	//      If X is extremely large compared to P then set gamma_prob = 1
	if (x > xbig) {
		return one;
	}
	
	if (x <= one || x < p) {
		//!      Use Pearson's series expansion.
		//!      (Note that P is not large enough to force overflow in LNGAMMA)
		
		arg = p * log(x) - x - lngamma(p + one);
		c = one;
		gamma_prob = one;
		a = p;
		do {
			a = a + one;
			c = c * x / a;
			gamma_prob = gamma_prob + c;
		} while (c >= tol);
		
		arg = arg + log(gamma_prob);
		gamma_prob = zero;
		if (arg >= elimit) {
			gamma_prob = exp(arg);
		} 
	} else {
		//!      Use a continued fraction expansion
		
		arg = p * log(x) - x - lngamma(p);
		a = one - p;
		b = a + x + one;
		c = zero;
		pn1 = one;
		pn2 = x;
		pn3 = x + one;
		pn4 = x * b;
		gamma_prob = pn3 / pn4;
		do {
			a = a + one;
			b = b + two;
			c = c + one;
			an = a * c;
			pn5 = b * pn3 - an * pn1;
			pn6 = b * pn4 - an * pn2;
			if (fabs(pn6) > zero) {
				rn = pn5 / pn6;
				if(fabs(gamma_prob - rn) <= MIN(tol, tol * rn))
					break;
				gamma_prob = rn;
			}
			
			pn1 = pn3;
			pn2 = pn4;
			pn3 = pn5;
			pn4 = pn6;
			if (fabs(pn5) >= oflo) {
				//  !      Re-scale terms in continued fraction if terms are large
				
				pn1 = pn1 / oflo;
				pn2 = pn2 / oflo;
				pn3 = pn3 / oflo;
				pn4 = pn4 / oflo;
			}
		} while (true);
		arg = arg + log(gamma_prob);
		gamma_prob = one;
		if (arg >= elimit) {
			gamma_prob = one - exp(arg);
		}
	}
	return gamma_prob;
}



double chi_squared(int ndf, double chi2) {
// Calculate the chi-squared distribution function
// ndf  = number of degrees of freedom
// chi2 = chi-squared value
// prob = probability of a chi-squared value <= chi2 (i.e. the left-hand
//        tail area)
	return gammad(0.5*chi2, 0.5*ndf);
}

void disaster() {
}


/*
The below function is from Alan Miller's package which has been made available
freely, and may be freely distributed.

The author is:
     Author: Alan Miller
             CSIRO Division of Mathematics & Statistics
             Private Bag 10, Rosebank MDC
             Clayton 3169, Victoria, Australia
     Phone: (+61) 3 9545-8036      Fax: (+61) 3 9545-8080
     e-mail: Alan.Miller @ mel.dms.csiro.au

The original code was translated from Fortran by Aleks Jakulin.     
*/
void logistic(// input
 			   int& ier,
			   int ngroups,			// # of examples
			   double **x,			// examples (real-valued)
			   int k,				// # attributes
			   double *s, double *n,// s-successes of n-trials
			   
			   // output
			   double& chisq,			// chi-squared
			   double& devnce,			// deviance
			   int& ndf,				// degrees of freedom
			   double *beta,			// fitted beta coefficients
			   double *se_beta,			// beta std.devs
			   double *fit,				// fitted probabilities for groups
			   double **cov_beta,		// approx covariance matrix
			   double *stdres,		 	// residuals
			   int *dependent
			   )			
{
	int i, iter, j, ncov, pos;
	double *propn, *p, *wt, *xrow, 
		*db, *bnew, dev_new, xb, *pnew, *covmat,
		*wnew, *range, var, *e, hii;
	bool *lindep;
	lsq fitter;
	
	ier = 0;
	ndf = ngroups - k - 1;
	if (ngroups < 2 || ndf < 0) {
		ier = 1; // not enough examples
		return;
	}
	for(i = 1; i <= ngroups; ++i) {
		if (s[i] < 0) {
			ier = 2;
			return;
		}
		if (n[i] < 0) {
			ier = 3;
			return;
		}
		if (n[i] < s[i]) {
			ier = 4;
			return;
		}
	}
	range = (double *)malloc(sizeof(double)*(k+1));
	for (i = 1; i <= k; ++i) {
		double min = INF;
		double max = -INF;
		
		for (j = 1; j <= ngroups; ++j) {
			if (x[j][i] < min)
				min = x[j][i];
			if (x[j][i] > max)
				max= x[j][i];
		}
		range[i] = max-min;
		if (range[i] < EPSILON*(fabs(min)+fabs(max))) {
			free(range);
			ier = 5; // constant variable
			//printf("variable %d is constant\n",i);
			dependent[i] = 1;
			return;
		}
	}
	
	++ngroups; ++k;
	propn = (double *)malloc(sizeof(double)*ngroups);
	p = (double *)malloc(sizeof(double)*ngroups);
	pnew = (double *)malloc(sizeof(double)*ngroups);
	wnew = (double *)malloc(sizeof(double)*ngroups);
	e = (double *)malloc(sizeof(double)*ngroups);
	wt = (double *)malloc(sizeof(double)*ngroups);
	
	xrow = (double *)malloc(sizeof(double)*k);
	db = (double *)malloc(sizeof(double)*k);	
	bnew = (double *)malloc(sizeof(double)*k);
	lindep = (bool *)malloc(sizeof(bool)*k);
	--k; --ngroups;
	
	for(i = 1; i <= ngroups; ++i) {
		/*
		printf("\n%2.2f %2.2f: ",s[i],n[i]);
		for (j = 1; j <= k; ++j) {
			printf("%5.2f ",x[i][j]);
		}*/
		propn[i] = s[i]/n[i];
		wt[i] = 1.0;
		p[i] = 0.5;
	}
	
	for(i = 0; i <= k; ++i) {
		beta[i] = 0.0;
	}
	
	iter = 1;
	
	do {
		fitter.startup(k,true);
		for (i = 1; i <= ngroups; ++i) {
			if (iter == 0) {
				xrow[0] = 0.25;
				for (j = 1; j <= k; ++j) {
					xrow[j] = 0.25*x[i][j];
				}
			} else {
				xrow[0] = p[i]*(1.0-p[i]);
				for (j = 1; j <= k; ++j) {
					xrow[j] = p[i]*(1.0-p[i])*x[i][j];
				}
			}
			fitter.includ(wt[i], xrow-1, propn[i]-p[i]);
		}
		
		//! Test for a singularity
		fitter.sing(lindep-1, ier);
		if (ier != 0) {
			for( i = 1; i<= k; ++i) {
				if (lindep[i]) {
					dependent[i] = 1;
					//printf("Variable number %d is linearly dependent upon earlier variables\n",i);
				}
			}
			ier = 6;
			return;
		}
		fitter.regcf(db-1, k+1, ier); // corrected
l10: 
		for (i = 0; i <= k; ++i) {
			bnew[i] = beta[i]+db[i];
		}
		
		//! Calculate new p(i)'s, weights & deviance
		
/*		double beta_sum=0.0;
		for (i=0; i<=k; i++) {
			beta_sum+=beta[i]*beta[i]*0.5;
		}
		dev_new = beta_sum; */

		dev_new = 0.0;
		for (i = 1; i <= ngroups; ++i) {
			xb = bnew[0];
			for (j = 1; j <= k; ++j) {
				xb += x[i][j] * bnew[j];
			}
			xb = exp(xb);
			pnew[i] = xb / (1.0 + xb);
			wnew[i] = (n[i])/(pnew[i]*(1.0 - pnew[i]));
			if (iter == 1) 
				wnew[i] = sqrt(wnew[i]);
			if (s[i] > 0) 
				dev_new = dev_new + s[i]*log(propn[i]/pnew[i]);
			if (s[i] < n[i]) 
				dev_new += (n[i]-s[i])*log((1.0-propn[i])/(1.0-pnew[i]));
		}
		dev_new = 2 * dev_new;
		//! If deviance has increased, reduce the step size.
		
		if (iter > 2 && dev_new > devnce*1.0001) {
			for (i = 0; i <= k; ++i)
				db[i] = 0.5 * db[i];
			goto l10;
		}
		//! Replace betas, weights & p's with new values
		
		for (i = 0; i <= k; ++i) {
			beta[i] = bnew[i];
		}
		for (i = 1; i <= ngroups; ++i) {
			wt[i] = wnew[i];
			p[i] = pnew[i];
		}
		//! Test for convergence
		
//		printf("iter. %d, dev: %f\n",iter,devnce-dev_new);
		if (iter > 2 && devnce - dev_new < 0.0001)
			break;
		devnce = dev_new;
		iter = iter + 1;
		if (iter > 200) {
			ier = 8;
			return;
		}
				
		//! Test for a very large beta
		
		for( i = 1; i<= k; ++i) {
			if(fabs(beta[i])*range[i] > 30.0) {
			//if(fabs(beta[i])*range[i] > 1e20) {	
				dependent[i] = 1;
//				printf("Coefficient for variable no. %d tending to infinity",i);
				
				ier = 7;
				return;
			}
		}
	} while(true);

	chisq = 0.0;
	for (i = 1; i <= ngroups; ++i) {
		double tt;

		e[i] = n[i]*p[i];
		tt = s[i]-e[i];
		chisq += tt*tt/e[i];
	}
	devnce = dev_new;

//! Calculate the approximate covariance matrix for the beta's, if ndf > 0.

	covmat = NULL;
	if (ndf > 0) {
		ncov = (k+1)*(k+2)/2;
		covmat = (double *)malloc(sizeof(double)*(ncov+1));
		fitter.cov(k+1, var, covmat, ncov, se_beta-1, ier);
		if (var < 1.0) {
			for (i = 1; i <= ncov; ++i) {
				covmat[i] /= var;
			}
			for (i = 0; i <= k; ++i) {
			    se_beta[i] /= sqrt(var);
			}
		}
		if (cov_beta != NULL) {
			pos = 1;
			for(i = 0;i<= k; ++i) {
				cov_beta[i][i] = covmat[pos];
				pos = pos + 1;
				for(j = i+1; j<= k; ++j) {
					cov_beta[i][j] = covmat[pos];
					cov_beta[j][i] = covmat[pos];
					pos = pos + 1;
				}
			}
		}
	}
	
	if (fit != NULL) {
		for (i = 1; i <= ngroups; ++i) {
			fit[i] = p[i];
		}
	}

	if (stdres != NULL) {
		for (i = 1; i <= ngroups; ++i) {
			xrow[0] = p[i]*(1.0 - p[i]);
			for (j = 1; j <= k; ++j) {
				xrow[j] = p[i]*(1.0 - p[i])*x[i][j];
			}
			fitter.hdiag(xrow-1, k+1, hii, ier);
			stdres[i] = (s[i]-n[i]*p[i]) / sqrt(n[i]*p[i]*(1.0-p[i])*(1.0-hii));
		}
	}

	if (covmat != NULL)
		free(covmat);
	free(propn); 
	free(p); 
	free(wnew); 
	free(pnew); 
	free(e); 
	free(wt);	
	free(xrow); 
	free(db); 
	free(bnew); 
	free(range); 
	free(lindep);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.