Example #1
0
File: dist.c Project: cran/mvpart
int distinit(int n, double *y[], int maxcat, char **error, 
          double *parm, int *size, int who, double *wt)
    {
    if (who==1 && maxcat >0) {
    graycode_init0(maxcat);        
    tsplit= (int *) ALLOC(maxcat*2, sizeof(int));   
    count = (int *)ALLOC(maxcat, sizeof(int)); 
    countn = (int *)ALLOC(maxcat*maxcat, sizeof(int)); 
    dsts  = (double *)ALLOC(maxcat*maxcat, sizeof(double));  
        if (countn==0 || count==0 || dsts== 0) {
        *error = "Could not allocate memory in distinit";
        return(1);
        }
    }
    *size = 1;
    return(0);
    }
Example #2
0
int
tstatsinit(int n, double *y[], int maxcat, char **error,
        int *size, int who, double *wt, double *treatment, int bucketnum, 
        int bucektMax, double *train_to_est_ratio)
{
    if (who == 1 && maxcat > 0) {
        graycode_init0(maxcat);
        countn = (int *) ALLOC(2 * maxcat, sizeof(int));
        tsplit = countn + maxcat;
        mean = (double *) ALLOC(6 * maxcat, sizeof(double));
        wts = mean + maxcat;
        trs = wts + maxcat;
        sums = trs + maxcat;
        wtsums = sums + maxcat;
        trsums = wtsums + maxcat;
    }
    *size = 1;
    *train_to_est_ratio = n * 1.0 / ct.NumHonest;
    return 0;
}
Example #3
0
int
CTDinit(int n, double *y[], int maxcat, char **error,
		int *size, int who, double *wt, double *treatment, 
		int bucketnum, int bucketMax, double *train_to_est_ratio)
{
	if (who == 1 && maxcat > 0) {
		graycode_init0(maxcat);
		countn = (int *) ALLOC(2 * maxcat, sizeof(int));
		tsplit = countn + maxcat;
		treatment_effect = (double *) ALLOC(8 * maxcat, sizeof(double));
		wts = treatment_effect + maxcat;
		trs = wts + maxcat;
		sums = trs + maxcat;
		wtsums = sums + maxcat;
		trsums = wtsums + maxcat;
		wtsqrsums = trsums + maxcat;
		wttrsqrsums = wtsqrsums + maxcat;
	}
	*size = 1;
	*train_to_est_ratio = n * 1.0 / ct.NumHonest;
	if (bucketnum == 0) 
		Rprintf("ERROR for buket!\n");
	return 0;
}
Example #4
0
File: gini.c Project: csilles/cxxr
int
giniinit(int n, double **y, int maxcat, char **error,
	 double *parm, int *size, int who, double *wt)
{
    int i, j, k;
    double temp;

   /* allocate memory  and setup losses */
    if (who == 1) {
	numclass = 0;           /* number of classes */
	for (i = 0; i < n; i++)
	    if (*y[i] > numclass)
		numclass = (int) *y[i];

	if (parm[numclass + numclass * numclass] == 2)
	    impurity = gini_impure2;
	else
	    impurity = gini_impure1;

	left = (double *) ALLOC(numclass * 2, sizeof(double));
	right = left + numclass;

	tsplit = (int *) ALLOC(maxcat * 2, sizeof(int));
	countn = tsplit + maxcat;

	awt = (double *) ALLOC(maxcat * 2, sizeof(double));
	rate = awt + maxcat;

	if (maxcat > 0) {
	    graycode_init0(maxcat);
	    ccnt = (double **) ALLOC(numclass, sizeof(double *));
	    ccnt[0] = (double *) ALLOC(numclass * maxcat, sizeof(double));
	    for (i = 1; i < numclass; i++)
		ccnt[i] = ccnt[i - 1] + maxcat;
	}
	i = 3 * numclass + numclass * numclass;
	prior = (double *) ALLOC(i, sizeof(double));
	aprior = prior + numclass;
	freq = aprior + numclass;
	loss = freq + numclass;

	for (i = 0; i < numclass; i++)
	    freq[i] = 0;
	temp = 0;
	for (i = 0; i < n; i++) {
	    j = (int) *y[i] - 1;
	    freq[j] += wt[i];
	    temp += wt[i];      /* sum total of weights */
	}
	for (i = 0; i < numclass; i++)
	    freq[i] /= temp;    /* relative frequency */

	temp = 0;
	for (i = 0; i < numclass; i++) {
	    prior[i] = parm[i];
	    aprior[i] = 0;
	    for (j = 0; j < numclass; j++) {
		k = numclass * j + i;
		loss[k] = parm[numclass + k];
		temp += loss[k] * prior[i];
		aprior[i] += loss[k] * prior[i];
	    }
	}
	for (i = 0; i < numclass; i++) {
	    if (freq[i] > 0) {  /* watch out for a missing class */
		prior[i] /= freq[i];
		aprior[i] /= (temp * freq[i]);  /* pi_i / n_i */
	    }
	}
    }
    *size = 2 + numclass;
    return 0;
}