Ejemplo n.º 1
0
	void Statistics::calculate(ssi_stream_t &stream_in)
	{
		ssi_real_t * in_ptr = ssi_pcast(ssi_real_t, stream_in.ptr);

		for (ssi_size_t d_i = 0; d_i < _dim; d_i++)
		{
			for (ssi_size_t num_i = 0; num_i < stream_in.num; num_i++){
				_tmp_arr[d_i][num_i] = in_ptr[d_i*stream_in.num + num_i];
			}
		}

		calculate_running_stats(stream_in.num);

		std::vector<stat_fn> stat_fns = _options.getSelection();

		for (ssi_size_t d_i = 0; d_i < _dim; d_i++)
		{
			for (ssi_size_t f_i = 0; f_i < stat_fns.size(); f_i++){

				ssi_size_t r_idx = d_i * ssi_size_t (stat_fns.size()) + f_i;
				switch (stat_fns[f_i])
				{
				case STAT_KURTOSIS:
					_res[r_idx] = calculateKurtosis(d_i);
					break;
				case STAT_SKEWNESS:
					_res[r_idx] = calculateSkewness(d_i);
					break;
				case STAT_MEAN:
					_res[r_idx] = calculateMean(d_i);
					break;
				case STAT_STDDEV:
					_res[r_idx] = calculateStandardDeviation(d_i);
					break;
				case STAT_VARIANCE:
					_res[r_idx] = calculateVariance(d_i);
					break;
				case STAT_NUMBER_VALS:
					_res[r_idx] = calulateNumberVals(d_i);
					break;
				default:
					ssi_err("stat_fn not implemented");
				}
			}
		}
	}
Ejemplo n.º 2
0
int main (void)
{
	FILE *inputStream = NULL,			/* File pointer for input file */
		 *outputStream = NULL;			/* File pointer for output file */

	int studentId1 = 0,
		studentId2 = 0,
		studentId3 = 0,
		studentId4 = 0,
		studentId5 = 0;					/* 5 student IDs */

	int studentClassStanding1 = 0,
		studentClassStanding2 = 0,
		studentClassStanding3 = 0,
		studentClassStanding4 = 0,
		studentClassStanding5 = 0;		/* 5 student class standings */

	double studentGpa1 = 0.0,
		   studentGpa2 = 0.0,
		   studentGpa3 = 0.0,
		   studentGpa4 = 0.0,
		   studentGpa5 = 0.0;			/* 5 student GPAs */

	double studentAge1 = 0.0,
		   studentAge2 = 0.0,
		   studentAge3 = 0.0,
		   studentAge4 = 0.0,
		   studentAge5 = 0.0;			/* 5 student ages */

	double sumGpa = 0.0,				/* sum of the 5 student's GPA */
		   sumClassStanding = 0.0,		/* sum of the 5 student's class standing */
		   sumAge = 0.0;				/* sum of the 5 student's ages */

	double meanGpa = 0.0,				/* mean of the 5 student's GPA */
		   meanClassStanding = 0.0,		/* mean of the 5 student's class standing */
		   meanAge = 0.0;				/* mean of the 5 student's ages */

	double deviationGpa1 = 0.0,		
		   deviationGpa2 = 0.0,
		   deviationGpa3 = 0.0,
		   deviationGpa4 = 0.0,
		   deviationGpa5 = 0.0;			/* deviation of each student's GPA */

	double varianceGpa = 0.0,			/* variance of the 5 student's GPA */
		   standardDeviationGpa = 0.0,	/* standard variance of the 5 student's GPA */
		   maxStudentGpa = 0.0,			/* maximum number out of the 5 student's GPA */
		   minStudentGpa = 0.0;			/* minimum number out of the 5 student's GPA */


	/* Opens an input file "input.dat" for reading; */
	inputStream = openFileStream (INPUT_FILE, INPUT_FILE_MODE);

    /* Opens an output file "output.dat" for writing; */
	outputStream = openFileStream (OUTPUT_FILE, OUTPUT_FILE_MODE);

	/* Checks the condition if files were successfully opened.
	 * If either file didn't open, then an error message is prompted,
	 * otherwise program will continue to execute other operations. */
	if (inputStream == NULL || outputStream == NULL)
	{
		/* Files were not successfully opened and prompts user of the error.
		 * Program will not continue to execute the rest of the program. */
		printf ("Error: Not able to open files!\n");
	}
	else
	{
		/* Reads five records from the input file (input.dat); */
	
		/* Student 1 */
		studentId1 = readInteger (inputStream);
		studentGpa1 = readDouble (inputStream);
		studentClassStanding1 = readInteger (inputStream);
		studentAge1 = readDouble (inputStream);

		/* Student 2 */
		studentId2 = readInteger (inputStream);
		studentGpa2 = readDouble (inputStream);
		studentClassStanding2 = readInteger (inputStream);
		studentAge2 = readDouble (inputStream);

		/* Student 3 */
		studentId3 = readInteger (inputStream);
		studentGpa3 = readDouble (inputStream);
		studentClassStanding3 = readInteger (inputStream);
		studentAge3 = readDouble (inputStream);

		/* Student 4 */
		studentId4 = readInteger (inputStream);
		studentGpa4 = readDouble (inputStream);
		studentClassStanding4 = readInteger (inputStream);
		studentAge4 = readDouble (inputStream);

		/* Student 5 */
		studentId5 = readInteger (inputStream);
		studentGpa5 = readDouble (inputStream);
		studentClassStanding5 = readInteger (inputStream);
		studentAge5 = readDouble (inputStream);

		/* Calculates the sum of the GPAs; */
		sumGpa = calculateSum (studentGpa1, studentGpa2, 
							   studentGpa3, studentGpa4, 
							   studentGpa5);

		/* Calculates the sum of the class standings; */
		sumClassStanding = calculateSum (studentClassStanding1, studentClassStanding2, 
										 studentClassStanding3, studentClassStanding4, 
										 studentClassStanding5);

		/* Calculates the sum of the ages; */
		sumAge = calculateSum (studentAge1, studentAge2,
								studentAge3, studentAge4,
								studentAge5);

		/* Calculates the mean of the GPAs; */
		meanGpa = calculateMean (sumGpa, NUMBER_OF_STUDENTS);

		/* Calculates the mean of the class standings; */
		meanClassStanding = calculateMean (sumClassStanding, NUMBER_OF_STUDENTS);

		/* Calculates the mean of the ages; */
		meanAge = calculateMean (sumAge, NUMBER_OF_STUDENTS);
 
		/* Calculates the deviation of each GPA from the mean */
		deviationGpa1 = calculateDeviation (studentGpa1, meanGpa);
		deviationGpa2 = calculateDeviation (studentGpa2, meanGpa);
		deviationGpa3 = calculateDeviation (studentGpa3, meanGpa);
		deviationGpa4 = calculateDeviation (studentGpa4, meanGpa);
		deviationGpa5 = calculateDeviation (studentGpa5, meanGpa);

		/* Calculates the variance of the GPAs */
		varianceGpa = calculateVariance (deviationGpa1, deviationGpa2,
										 deviationGpa3, deviationGpa4,
										 deviationGpa5, NUMBER_OF_STUDENTS);

		/* Calculates the standard deviation of the GPAs; */
		standardDeviationGpa = calculateStandardDeviation (varianceGpa);
	
		/* Determines the min of the GPAs; */
		minStudentGpa = findMin (studentGpa1, studentGpa2, 
								 studentGpa3, studentGpa4, 
								 studentGpa5);	

		/* Determines the max of the GPAs; */
		maxStudentGpa = findMax (studentGpa1, studentGpa2, 
								 studentGpa3, studentGpa4, 
								 studentGpa5);

		/* Writing the result to the output file (output.dat) */
		printDouble (outputStream, meanGpa);
		printDouble (outputStream, meanClassStanding);
		printDouble (outputStream, meanAge);
		printDouble (outputStream, standardDeviationGpa);
		printDouble (outputStream, minStudentGpa);
		printDouble (outputStream, maxStudentGpa);

		/* Closes the input and output files (i.e. input.dat and output.dat) */
		closeFileStream (inputStream);
		closeFileStream (outputStream);
	}

	return 0;
}
Ejemplo n.º 3
0
void
subspaceTrain (Subspace *s, Matrix images, ImageList *srt, int numSubjects, int dropNVectors, CutOffMode cutOffMode, double cutOff, int useLDA, int writeTextInterm
               /*START Changed by Zeeshan: For LPP*/
               ,int useLPP, int neighbourCount, int useAdaptiveK, int lppKeepNVectors, char* lppDistance
               /*END Changed by Zeeshan: For LPP*/
               /*START 	Changed by Zeeshan: For ICA*/
               ,int useICA, int arch, double learningRate, int blockSize, int iterations
               /*END 	Changed by Zeeshan: For ICA*/
               )
{
    int i;
    Matrix m;
    int n = 0;    /* The number of eigen vectors to keep */
    double total_energy, energy;
    Matrix tmp;

    /* Initialize structure */

    s->useLDA         = useLDA;
    s->cutOffMode     = cutOffMode;
    s->cutOff         = cutOff;
    s->dropNVectors   = dropNVectors;

    s->numSubjects    = numSubjects;
    s->numPixels      = images->row_dim;

    /*START Changed by Zeeshan: For LPP*/
    s->useLPP 	    = useLPP;
    s->neighbourCount = neighbourCount;
    s->lppDistance    = strdup(lppDistance);
    s->lppKeepNVectors= lppKeepNVectors;
    s->useAdaptiveK   = useAdaptiveK;
    /*END Changed by Zeeshan: For LPP*/

    /*START Changed by Zeeshan: For ICA*/
    s->useICA 	    = useICA;
    s->arch	    = arch;
    s->ica2Basis 	    = NULL;
    s->learningRate   = learningRate;
    s->blockSize 	    = blockSize;
    s->iterations     = iterations;
    /*END Changed by Zeeshan: For ICA*/

    /*START Changed by Zeeshan: For ICA & LPP*/
    /*********************************************************************
    * STEP ZERO: Make sure LDA and LPP are executed exclusively
    ********************************************************************/
    DEBUG_CHECK (!(s->useLDA && s->useLPP) && !(s->useLDA && s->useICA) && !(s->useICA && s->useLPP), 
        "Either LDA, LPP or ICA should be executed.");
    /*END Changed by Zeeshan: For ICA & LPP*/


    /*********************************************************************
    * STEP ONE: Calculate the eigenbasis
    ********************************************************************/

    /* Compute the Eigenvalues and Eigenvectors for the covariance matrix
    derived from the images data matrix. The image data is "centered", meaning
    the mean image is subtracted from all images before PCA is performed.
    This centering is done in place, so after this call images are centered. */

    MESSAGE("Computing the PCA eigenspace.");

    eigentrain (&s->mean, &s->values, &s->basis, images);

    MESSAGE("Finished computing eigenspace.");

    /* Numerical roundoff errors may lead to small negative values.
    Strip those before saving the matrix. */

    m = s->values;
    for (i = 0; i < m->row_dim; i++)
    {
        if (ME(m,i,0) < 0) {
            if (ME(m,i,0) < -1e-10)
                printf("WARNING: Large negative eigenvalue found %f. Truncating to zero.\n", ME(m,i,0));
            ME(m,i,0) = 0;
        }
    }

    /*********************************************************************
    * STEP TWO: Drop eigenvectors from the front or truncate them from
    * the back
    ********************************************************************/

    /* 
    The following is used to filter the vectors that are retained after PCA
    training.  The function first optionally removes the vectors from the matrix
    based on the argument -dropNVectors. This argument is always intrepreted in
    terms of absolute numbers, i.e. a value of 1 means drop the first vector, a
    value of 3 means drop the first three. The function then drops vectors from the
    tail based on -cutOffMode and -cutOff arguments. Here, the mode controls how the
    cutoff is performed. The possible modes are:

    NONE:    Keep all remaining eigenvectors.
    SIMPLE:  Keep a percentage where the percentage is specified by cutOff.
    ENERGY:  Keep the fewest vectors are such that the sum of energy for them just
    exceeds cutOff times the total energy. 
    STRETCH: Keep all eigenvectors that have eigenvalues greater than a percentage 
    of the largest, where the percentage is specied by cutOff. 
    CLASSES: Keep as many eigenvectors as there are LDA classes.

    For both Energy and Stretch, if eigen values/vectors are dropped from the front, 
    these are not included in the determination of the total energy or the max
    eigen value. 
    */

    /* Drop the first vectors  */

    DEBUG_CHECK (s->dropNVectors < (s->basis)->col_dim, "Number of vectors to drop must be less than the number of the eigen vectors");

    /* transpose eigenValues for use in this function */

    tmp = transposeMatrix (s->values);
    freeMatrix (s->values);
    s->values = tmp;

    if (s->dropNVectors && (s->dropNVectors < (s->values)->col_dim))
    {
        tmp = matrixCols (s->basis, s->dropNVectors, (s->basis)->col_dim-1);
        freeMatrix (s->basis);
        s->basis = tmp;

        tmp = matrixCols (s->values, s->dropNVectors, (s->values)->col_dim-1);
        freeMatrix (s->values);
        s->values = tmp;
    }

    /* transpose the eigenValues back to the original order. */

    tmp = transposeMatrix (s->values);
    freeMatrix (s->values);
    s->values = tmp;

    DEBUG_CHECK((s->values)->row_dim - s->dropNVectors > 0, "Too many eigen vectors droped from front. Can not proceed.");

    switch (s->cutOffMode)
    {
    case CUTOFF_NONE:
        n = (s->basis)->col_dim;
        break;

    case CUTOFF_SIMPLE:
        n = (int)((s->basis)->col_dim * s->cutOff / 100.0);
        break;

    case CUTOFF_ENERGY:
        /* compute total energy - this will not include vectors/values dropped from front. */
        total_energy = 0;
        for (i = 0; i < (s->values)->row_dim; i++) {
            total_energy += ME(s->values, i, 0);
        }

        /* compute cutoff point */
        i = 0;
        energy = 0;
        while ((i < (s->values)->row_dim) && (energy < total_energy * s->cutOff / 100.0)) {
            energy += ME(s->values, i, 0);
            i++;
        }
        n = i;
        break;

    case CUTOFF_STRETCH:
        i = 1;
        while ((i < (s->values)->row_dim) &&
            (100.0*(ME(s->values, i, 0) / ME(s->values, s->dropNVectors, 0)) > cutOff )) {
                i++;
        }
        n = i;
        break;

    case CUTOFF_CLASSES:
        n = s->numSubjects;
        break;

    case CUTOFF_DROPVEC:
        n = (int)((s->basis)->col_dim - s->cutOff);
        break;

    default:
        n = 0;
        DEBUG_CHECK (0, "ERROR: Unkown cutoff type");
        break;
    };

    /* Never set the dimensionality of the PCA subspace below the number of
    LDA classes when LDA is being used. Doing so creates a horrible problem
    for LDA: too fee dimensions */

    if (s->useLDA && (n < s->numSubjects))
        n = s->numSubjects;

    DEBUG_CHECK (n <= (s->basis)->col_dim, "Tried to expand, not contract, PCA space.");

    MESSAGE1ARG ("Retaining %d eigen vectors.",n);

    tmp = matrixCols ( s->basis, 0 , n-1);
    freeMatrix (s->basis);
    s->basis = tmp;

    DEBUG_INT (1, "Number of eigen vectors kept.", n);

    DEBUG_CHECK ((s->basis)->col_dim > 0, "All basis vectors deleted after cutoff "
        "and vector drop was processed.");

    MESSAGE2ARG("Truncating PCA Space. Subspace projection expressed "
        "as %d by %d matrix.", s->basis->row_dim, s->basis->col_dim);

    /*********************************************************************
    * STEP THREE: Do the LDA if specified
    ********************************************************************/

    if (s->useLDA)
    {
        /* Need to project original images into PCA space */

        Matrix fisherBasis, fisherValues, combinedBasis;
        Matrix imspca = transposeMultiplyMatrixL (s->basis, images);

        MESSAGE("Computing Fisher Linear Discriminants for "
            "training images projected into PCA subspace.");

        fisherTrain (imspca, srt, &fisherBasis, &fisherValues, writeTextInterm);

        combinedBasis = multiplyMatrix (s->basis, fisherBasis);
        basis_normalize (combinedBasis);

        MESSAGE2ARG ("PCA and LDA Combined. Combined projection expressed as %d by "
            "%d matrix.", combinedBasis->row_dim, combinedBasis->col_dim);

        s->values = fisherValues;
        s->basis  = combinedBasis;
    }

    /*START Changed by Zeeshan: For LPP*/
    /*********************************************************************
    * STEP FOUR: Do the LPP if specified
    ********************************************************************/
    if (s->useLPP)
    {
        /* Need to project original images into PCA space */

        Matrix laplacianBasis, laplacianValues, combinedBasis;
        Matrix imspca = transposeMultiplyMatrixL (s->basis, images);           

        MESSAGE("Computing Locality Preservation Projections for "
            "training images projected into PCA subspace.");

        laplacianTrain (imspca, srt, &laplacianBasis, &laplacianValues, neighbourCount, 
            useAdaptiveK, lppKeepNVectors, &s->values, lppDistance, writeTextInterm);

        combinedBasis = multiplyMatrix (s->basis, laplacianBasis);
        basis_normalize (combinedBasis);

        MESSAGE2ARG ("PCA and LPP Combined. Combined projection expressed as %d by "
            "%d matrix.", combinedBasis->row_dim, combinedBasis->col_dim);

        s->values = calculateStandardDeviation (laplacianBasis);//OPTION4
        //s->values = laplacianValues;//OPTION2
        s->basis  = combinedBasis;

        for (i = 0; i < s->values->row_dim; i++)
        {
            if (ME(s->values, i, 0) <= 0.001)
            {
                if (ME(s->values, i, 0) < -1e-10)
                    printf("WARNING: Large negative value found %f. Truncating to zero.\n", ME(s->values, i, 0));
                ME(s->values, i, 0) = 0.001;
            }
        }


    }
    /*END Changed by Zeeshan: For LPP*/


    /*START Changed by Zeeshan: For ICA*/
    /*********************************************************************
    * STEP FIVE: Do the ICA if specified
    ********************************************************************/
    if (s->useICA)
    {
        /* Need to project original images into PCA space */
        Matrix independentBasis;
        Matrix imspca = transposeMultiplyMatrixL (s->basis, images);           

        MESSAGE("Computing independent components from the principle components.");

        independentTrain(s->basis, imspca, &independentBasis, s->arch, s->blockSize, s->learningRate, s->iterations);

        if (s->arch == 1)
        {
            Matrix combinedBasis;
            combinedBasis = multiplyMatrix (s->basis, independentBasis);
            s->basis  = combinedBasis;
            s->ica2Basis = NULL;

            MESSAGE2ARG ("PCA and ICA Combined. Combined projection expressed as %d by "
                "%d matrix.", combinedBasis->row_dim, combinedBasis->col_dim);	
        }
        else if (s->arch == 2)
        {
            s->ica2Basis = independentBasis;
            MESSAGE2ARG ("PCA and ICA kept separate. ICA projection expressed as %d by "
                "%d matrix.", independentBasis->row_dim, independentBasis->col_dim);	
        }
    }
    /*END Changed by Zeeshan: For ICA*/
}