Exemplo n.º 1
0
TableOfReal CCA_and_TableOfReal_scores (CCA me, TableOfReal thee, long numberOfFactors) {
	try {
		long n = thy numberOfRows;
		long nx = my x -> dimension, ny = my y -> dimension;

		if (ny + nx != thy numberOfColumns) {
			Melder_throw (U"The number of columns in the table (", thy numberOfColumns,
				U") does not agree with the dimensions of the CCA object (ny + nx = ", ny, U" + ", nx, U").");
		}
		if (numberOfFactors == 0) {
			numberOfFactors = my numberOfCoefficients;
		}
		if (numberOfFactors < 1 || numberOfFactors > my numberOfCoefficients) {
			Melder_throw (U"The number of factors must be in interval [1, ", my numberOfCoefficients, U"].");
		}
		autoTableOfReal him = TableOfReal_create (n, 2 * numberOfFactors);
		TableOfReal phim = him.peek();
		NUMstrings_copyElements (thy rowLabels, his rowLabels, 1, thy numberOfRows);
		Eigen_and_TableOfReal_project_into (my y, thee, 1, ny, &phim, 1, numberOfFactors);
		Eigen_and_TableOfReal_project_into (my x, thee, ny + 1, thy numberOfColumns, &phim, numberOfFactors + 1, his numberOfColumns);
		TableOfReal_setSequentialColumnLabels (him.peek(), 1, numberOfFactors, U"y_", 1, 1);
		TableOfReal_setSequentialColumnLabels (him.peek(), numberOfFactors + 1, his numberOfColumns, U"x_", 1, 1);
		return him.transfer();
	} catch (MelderError) {
		Melder_throw (me, U": no TableOfReal with scores created.");
	}
}
Exemplo n.º 2
0
Arquivo: CCA.c Projeto: alekstorm/tala
TableOfReal CCA_and_TableOfReal_scores (CCA me, TableOfReal thee, long numberOfFactors)
{
	TableOfReal him = NULL;
	long n = thy numberOfRows;
	long nx = my x -> dimension, ny = my y -> dimension;

	if (ny + nx != thy numberOfColumns) return Melder_errorp7 (L"The number "
		"of columns in the table (", Melder_integer (thy numberOfColumns), L") does not agree with "
		"the dimensions of the CCA object (ny + nx = ", Melder_integer (ny), L" + ", Melder_integer (nx), L").");

	if (numberOfFactors == 0) numberOfFactors = my numberOfCoefficients;
	if (numberOfFactors < 1 || numberOfFactors > my numberOfCoefficients)
		return Melder_errorp3 (L"The number of factors must be in interval "
			"[1, ", Melder_integer (my numberOfCoefficients), L"].");
	
	him = TableOfReal_create (n, 2 * numberOfFactors);
	if (him == NULL) return NULL;

	if (! NUMstrings_copyElements (thy rowLabels, his rowLabels, 1, thy numberOfRows) ||
		! Eigen_and_TableOfReal_project_into (my y, thee, 1, ny, &him, 1, numberOfFactors) ||
		! Eigen_and_TableOfReal_project_into (my x, thee, ny + 1,
			thy numberOfColumns, &him, numberOfFactors + 1, his numberOfColumns) ||
		! TableOfReal_setSequentialColumnLabels (him, 1, numberOfFactors, L"y_", 1, 1) ||
		! TableOfReal_setSequentialColumnLabels (him, numberOfFactors + 1,
			his numberOfColumns, L"x_", 1, 1)) forget (him);

	return him;
}
Exemplo n.º 3
0
class_methods_end

int Proximity_init (I, long numberOfPoints)
{
	iam (Proximity);
		
    return TableOfReal_init (me, numberOfPoints, numberOfPoints) &&
		TableOfReal_setSequentialRowLabels (me, 0, 0, NULL, 1, 1) &&
		TableOfReal_setSequentialColumnLabels (me, 0, 0, NULL, 1, 1); 
}
Exemplo n.º 4
0
autoConfiguration Discriminant_and_TableOfReal_to_Configuration (Discriminant me, TableOfReal thee, long numberOfDimensions) {
    try {
        if (numberOfDimensions == 0) {
            numberOfDimensions = Discriminant_getNumberOfFunctions (me);
        }
        autoConfiguration him = Configuration_create (thy numberOfRows, numberOfDimensions);
        Eigen_and_TableOfReal_into_TableOfReal_projectRows (my eigen.get(), thee, 1, him.get(), 1, numberOfDimensions);
        TableOfReal_copyLabels (thee, him.get(), 1, 0);
        TableOfReal_setSequentialColumnLabels (him.get(), 0, 0, U"Eigenvector ", 1, 1);
        return him;
    } catch (MelderError) {
        Melder_throw (U"Configuration not created.");
    }
}
Exemplo n.º 5
0
autoConfiguration PCA_and_TableOfReal_to_Configuration (PCA me, TableOfReal thee, long numberOfDimensionsToKeep) {
	try {
		if (numberOfDimensionsToKeep == 0 || numberOfDimensionsToKeep > my numberOfEigenvalues) {
			numberOfDimensionsToKeep = my numberOfEigenvalues;
		}
		autoConfiguration him = Configuration_create (thy numberOfRows, numberOfDimensionsToKeep);
		Eigen_and_TableOfReal_into_TableOfReal_projectRows (me, thee, 1, him.get(), 1, numberOfDimensionsToKeep);
		NUMstrings_copyElements (thy rowLabels, his rowLabels, 1, thy numberOfRows);
		TableOfReal_setSequentialColumnLabels (him.get(), 0, 0, U"pc", 1, 1);
		return him;
	} catch (MelderError) {
		Melder_throw (U"Configuration not created from PCA & TableOfReal.");
	}
}
Exemplo n.º 6
0
autoConfiguration Configuration_create (long numberOfPoints, long numberOfDimensions) {
	try {
		autoConfiguration me = Thing_new (Configuration);
		TableOfReal_init (me.peek(), numberOfPoints, numberOfDimensions);
		my w = NUMvector<double> (1, numberOfDimensions);
		TableOfReal_setSequentialRowLabels (me.peek(), 0, 0, nullptr, 1, 1);
		TableOfReal_setSequentialColumnLabels (me.peek(), 0, 0, U"dimension ", 1, 1);

		my metric = 2;
		Configuration_setDefaultWeights (me.peek());
		Configuration_randomize (me.peek());
		return me;
	} catch (MelderError) {
		Melder_throw (U"Configuration not created.");
	}
}
Exemplo n.º 7
0
autoTableOfReal Matrix_and_Categories_to_TableOfReal (Matrix me, Categories thee) {
	try {
		if (thy size != my ny) {
			Melder_throw (U"Number of rows and number of categories must be equal.");
		}

		autoTableOfReal him = TableOfReal_create (my ny, my nx);
		TableOfReal_setSequentialColumnLabels (him.peek(), 0, 0, nullptr, 1, 1);

		for (long i = 1; i <= my ny; i ++) {
			his rowLabels [i] = Melder_dup (OrderedOfString_itemAtIndex_c (thee, i));
		}
		for (long i = 1; i <= my ny; i ++) {
			for (long j = 1; j <= my nx; j ++) {
				his data [i] [j] = my z [i] [j];
			}
		}
		return him;
	} catch (MelderError) {
		Melder_throw (U"TableOfReal not created from Matrix & Categories.");
	}
}
Exemplo n.º 8
0
autoTableOfReal PCA_and_TableOfReal_to_TableOfReal_zscores (PCA me, TableOfReal thee, long numberOfDimensions) {
	try {
		if (numberOfDimensions == 0 || numberOfDimensions > my numberOfEigenvalues) {
			numberOfDimensions = my numberOfEigenvalues;
		}
		autoTableOfReal him = TableOfReal_create (thy numberOfRows, numberOfDimensions);
		for (long i = 1; i <= thy numberOfRows; i++) { /* row */
			for (long j = 1; j <= numberOfDimensions; j++) {
				double r = 0, sigma = sqrt (my eigenvalues[j]);
				for (long k = 1; k <= my dimension; k++) {
					// eigenvector in row, data in row
					r += my eigenvectors[j][k] * (thy data[i][k] - my centroid[k]) / sigma;
				}
				his data[i][j] = r;
			}
		}
		NUMstrings_copyElements (thy rowLabels, his rowLabels, 1, thy numberOfRows);
		TableOfReal_setSequentialColumnLabels (him.get(), 0, 0, U"pc", 1, 1);
		return him;
	} catch (MelderError) {
		Melder_throw (U"TableOfReal (zscores) not created from PCA & TableOfReal.");
	}
}
Exemplo n.º 9
0
TableOfReal Matrix_and_Categories_to_TableOfReal (I, Categories thee)
{
	iam (Matrix); TableOfReal him; long i, j;
	
	if (thy size != my ny) return Melder_errorp1 (L"Matrix_and_Categories_to_TableOfReal: "
		"number of rows and number of categories must be equal.");
		
	if (! (him = TableOfReal_create (my ny, my nx)) ||
		! TableOfReal_setSequentialColumnLabels (him, 0, 0, NULL, 1, 1)) goto end;
	
	for (i = 1; i <= my ny; i++)
	{
		if (! (his rowLabels[i] = Melder_wcsdup_e (OrderedOfString_itemAtIndex_c (thee, i)))) goto end;
	}
		
	for (i = 1; i <= my ny; i++)
	{
		for (j = 1; j <= my nx; j++) his data[i][j] = my z[i][j];
	}
	
end:
	if (Melder_hasError()) forget (him);
	return him;
}
Exemplo n.º 10
0
autoDiscriminant TableOfReal_to_Discriminant (TableOfReal me) {
    try {
        autoDiscriminant thee = Thing_new (Discriminant);
        long dimension = my numberOfColumns;

        if (! NUMdmatrix_hasFiniteElements(my data, 1, my numberOfRows, 1, my numberOfColumns)) {
            Melder_throw (U"At least one of the table's elements is not finite or undefined.");
        }

        if (! TableOfReal_hasRowLabels (me)) {
            Melder_throw (U"At least one of the rows has no label.");
        }

        autoTableOfReal mew = TableOfReal_sortOnlyByRowLabels (me);
        if (! TableOfReal_hasColumnLabels (mew.get())) {
            TableOfReal_setSequentialColumnLabels (mew.get(), 0, 0, U"c", 1, 1);
        }

        thy groups = TableOfReal_to_SSCPList_byLabel (mew.get());
        thy total = TableOfReal_to_SSCP (mew.get(), 0, 0, 0, 0);

        if ((thy numberOfGroups = thy groups -> size) < 2) {
            Melder_throw (U"Number of groups must be greater than one.");
        }

        TableOfReal_centreColumns_byRowLabel (mew.get());

        // Overall centroid and apriori probabilities and costs.

        autoNUMvector<double> centroid (1, dimension);
        autoNUMmatrix<double> between (1, thy numberOfGroups, 1, dimension);
        thy aprioriProbabilities = NUMvector<double> (1, thy numberOfGroups);
        thy costs = NUMmatrix<double> (1, thy numberOfGroups, 1, thy numberOfGroups);

        double sum = 0, scale;
        for (long k = 1; k <= thy numberOfGroups; k ++) {
            SSCP m = thy groups->at [k];
            sum += scale = SSCP_getNumberOfObservations (m);
            for (long j = 1; j <= dimension; j ++) {
                centroid [j] += scale * m -> centroid [j];
            }
        }

        for	(long j = 1; j <= dimension; j ++) {
            centroid [j] /= sum;
        }

        for (long k = 1; k <= thy numberOfGroups; k ++) {
            SSCP m = thy groups->at [k];
            scale = SSCP_getNumberOfObservations (m);
            thy aprioriProbabilities[k] = scale / my numberOfRows;
            for (long j = 1; j <= dimension; j ++) {
                between [k] [j] = sqrt (scale) * (m -> centroid [j] - centroid [j]);
            }
        }

        // We need to solve B'B.x = lambda W'W.x, where B'B and W'W are the between and within covariance matrices.
        // We do not calculate these covariance matrices directly from the data but instead use the GSVD to solve for
        // the eigenvalues and eigenvectors of the equation.

        thy eigen = Thing_new (Eigen);
        Eigen_initFromSquareRootPair (thy eigen.get(), between.peek(), thy numberOfGroups, dimension, mew -> data, my numberOfRows);

        // Default priors and costs

        for (long igroup = 1; igroup <= thy numberOfGroups; igroup ++) {
            for (long jgroup = igroup + 1; jgroup <= thy numberOfGroups; jgroup ++) {
                thy costs [igroup] [jgroup] = thy costs [jgroup] [igroup] = 1.0;
            }
        }
        return thee;
    } catch (MelderError) {
        Melder_throw (me, U": Discriminant not created.");
    }
}