double* BetaGamma(int k, int noOfSamples, int noOfFeatures, double *featureMatrix, double *classColumn, double *outputFeatures, double betaParam, double gammaParam) { double **feature2D = (double **) checkedCalloc(noOfFeatures,sizeof(double *)); /*holds the class MI values*/ double *classMI = (double *)checkedCalloc(noOfFeatures,sizeof(double)); char *selectedFeatures = (char *)checkedCalloc(noOfFeatures,sizeof(char)); /*holds the intra feature MI values*/ int sizeOfMatrix = k*noOfFeatures; double *featureMIMatrix = (double *)checkedCalloc(sizeOfMatrix,sizeof(double)); double maxMI = 0.0; int maxMICounter = -1; double score, currentScore, totalFeatureMI; int currentHighestFeature, arrayPosition; int i,j,m; /*********************************************************** ** because the array is passed as ** s a m p l e s ** f ** e ** a ** t ** u ** r ** e ** s ** ** this pulls out a pointer to the first sample of ** each feature and stores it as a multidimensional array ** so it can be indexed nicely ***********************************************************/ for(j = 0; j < noOfFeatures; j++) { feature2D[j] = featureMatrix + (int)j*noOfSamples; } for (i = 0; i < sizeOfMatrix; i++) { featureMIMatrix[i] = -1; }/*for featureMIMatrix - blank to -1*/ /*********************************************************** ** SETUP COMPLETE ** Algorithm starts here ***********************************************************/ for (i = 0; i < noOfFeatures; i++) { classMI[i] = calculateMutualInformation(feature2D[i], classColumn, noOfSamples); if (classMI[i] > maxMI) { maxMI = classMI[i]; maxMICounter = i; }/*if bigger than current maximum*/ }/*for noOfFeatures - filling classMI*/ selectedFeatures[maxMICounter] = 1; outputFeatures[0] = maxMICounter; /************* ** Now we have populated the classMI array, and selected the highest ** MI feature as the first output feature ** Now we move into the JMI algorithm *************/ for (i = 1; i < k; i++) { /************************************************************ ** to ensure it selects some features ** if this is zero then it will not pick features where the ** redundancy is greater than the relevance ************************************************************/ score = -DBL_MAX; currentHighestFeature = 0; currentScore = 0.0; totalFeatureMI = 0.0; for (j = 0; j < noOfFeatures; j++) { /*if we haven't selected j*/ if (!selectedFeatures[j]) { currentScore = classMI[j]; totalFeatureMI = 0.0; for (m = 0; m < i; m++) { arrayPosition = m*noOfFeatures + j; if (featureMIMatrix[arrayPosition] == -1) { /*double calculateMutualInformation(double *firstVector, double *secondVector, int vectorLength);*/ featureMIMatrix[arrayPosition] = betaParam*calculateMutualInformation(feature2D[(int) outputFeatures[m]], feature2D[j], noOfSamples); /*double calculateConditionalMutualInformation(double *firstVector, double *targetVector, double* conditionVector, int vectorLength);*/ featureMIMatrix[arrayPosition] -= gammaParam*calculateConditionalMutualInformation(feature2D[(int) outputFeatures[m]], feature2D[j], classColumn, noOfSamples); }/*if not already known*/ totalFeatureMI += featureMIMatrix[arrayPosition]; }/*for the number of already selected features*/ currentScore -= (totalFeatureMI); if (currentScore > score) { score = currentScore; currentHighestFeature = j; } }/*if j is unselected*/ }/*for number of features*/ selectedFeatures[currentHighestFeature] = 1; outputFeatures[i] = currentHighestFeature; }/*for the number of features to select*/ FREE_FUNC(classMI); FREE_FUNC(feature2D); FREE_FUNC(featureMIMatrix); FREE_FUNC(selectedFeatures); classMI = NULL; feature2D = NULL; featureMIMatrix = NULL; selectedFeatures = NULL; return outputFeatures; }/*BetaGamma(int,int,int,double[][],double[],double[],double,double)*/
void ICAP(int k, int noOfSamples, int noOfFeatures, double *featureMatrix, double *classColumn, int *outputFeatures,int *noOfOutput) { /*holds the class MI values*/ double *classMI = (double *)checkedCalloc(noOfFeatures,sizeof(double)); char *selectedFeatures = (char *)checkedCalloc(noOfFeatures,sizeof(char)); /*holds the intra feature MI values*/ int sizeOfMatrix = k*noOfFeatures; double *featureMIMatrix = (double *)checkedCalloc(sizeOfMatrix,sizeof(double)); double *featureCMIMatrix = (double *)checkedCalloc(sizeOfMatrix,sizeof(double)); /*Changed to ensure it always picks a feature*/ double maxMI = -1.0; int maxMICounter = -1; double score, currentScore, totalFeatureInteraction, interactionInfo; int currentHighestFeature, arrayPosition; int i, j, m; /*holds the first element of each sample*/ double **feature2D = (double **) checkedCalloc(noOfFeatures,sizeof(double *)); firstElementOfEachSample(feature2D,featureMatrix,noOfFeatures,noOfSamples); for (i = 0; i < sizeOfMatrix; i++) { featureMIMatrix[i] = -1; featureCMIMatrix[i] = -1; }/*for featureMIMatrix and featureCMIMatrix - blank to -1*/ /*SETUP COMPLETE*/ /*Algorithm starts here*/ for (i = 0; i < noOfFeatures;i++) { classMI[i] = calculateMutualInformation(feature2D[i], classColumn, noOfSamples); if (classMI[i] > maxMI) { maxMI = classMI[i]; maxMICounter = i; }/*if bigger than current maximum*/ }/*for noOfFeatures - filling classMI*/ selectedFeatures[maxMICounter] = 1; outputFeatures[0] = maxMICounter; (*noOfOutput)++; /************* ** Now we have populated the classMI array, and selected the highest ** MI feature as the first output feature *************/ for (i = 1; i < k; i++) { /********************************************************************** ** to ensure it selects some features **if this is zero then it will not pick features where the redundancy is greater than the **relevance **********************************************************************/ score = -DBL_MAX; currentHighestFeature = 0; currentScore = 0.0; for (j = 0; j < noOfFeatures; j++) { /*if we haven't selected j*/ if (!selectedFeatures[j]) { currentScore = classMI[j]; totalFeatureInteraction = 0.0; for (m = 0; m < i; m++) { arrayPosition = m*noOfFeatures + j; if (featureMIMatrix[arrayPosition] == -1) { /*work out interaction*/ /*double calculateMutualInformation(double *firstVector, double *secondVector, int vectorLength);*/ featureMIMatrix[arrayPosition] = calculateMutualInformation(feature2D[(int) outputFeatures[m]], feature2D[j], noOfSamples); /*double calculateConditionalMutualInformation(double *firstVector, double *targetVector, double* conditionVector, int vectorLength);*/ featureCMIMatrix[arrayPosition] = calculateConditionalMutualInformation(feature2D[(int) outputFeatures[m]], feature2D[j], classColumn, noOfSamples); }/*if not already known*/ interactionInfo = featureCMIMatrix[arrayPosition] - featureMIMatrix[arrayPosition]; if (interactionInfo < 0) { totalFeatureInteraction += interactionInfo; } }/*for the number of already selected features*/ currentScore += totalFeatureInteraction; if (currentScore > score) { score = currentScore; currentHighestFeature = j; } }/*if j is unselected*/ }/*for number of features*/ selectedFeatures[currentHighestFeature] = 1; outputFeatures[i] = currentHighestFeature; (*noOfOutput)++; }/*for the number of features to select*/ FREE_FUNC(classMI); FREE_FUNC(feature2D); FREE_FUNC(featureMIMatrix); FREE_FUNC(featureCMIMatrix); FREE_FUNC(selectedFeatures); classMI = NULL; feature2D = NULL; featureMIMatrix = NULL; featureCMIMatrix = NULL; selectedFeatures = NULL; }/*ICAP(int,int,int,double[][],double[],double[])*/
void JMI(int k, int noOfSamples, int noOfFeatures, double *featureMatrix, double *classColumn, int *outputFeatures,int *noOfOutput) { /*holds the class MI values*/ double *classMI = (double *)checkedCalloc(noOfFeatures,sizeof(double)); char *selectedFeatures = (char *)checkedCalloc(noOfFeatures,sizeof(char)); /*holds the intra feature MI values*/ int sizeOfMatrix = k*noOfFeatures; double *featureMIMatrix = (double *)checkedCalloc(sizeOfMatrix,sizeof(double)); /*Changed to ensure it always picks a feature*/ double maxMI = -1.0; int maxMICounter = -1; double score, currentScore, totalFeatureMI; int currentHighestFeature; double *mergedVector = (double *) checkedCalloc(noOfSamples,sizeof(double)); int arrayPosition; double mi, tripEntropy; int i,j,x; /*holds the first element of each sample*/ double **feature2D = (double **) checkedCalloc(noOfFeatures,sizeof(double *)); firstElementOfEachSample(feature2D,featureMatrix,noOfFeatures,noOfSamples); for (i = 0; i < sizeOfMatrix;i++) { featureMIMatrix[i] = -1; }/*for featureMIMatrix - blank to -1*/ for (i = 0; i < noOfFeatures;i++) { /*calculate mutual info **double calculateMutualInformation(double *firstVector, double *secondVector, int vectorLength); */ classMI[i] = calculateMutualInformation(feature2D[i], classColumn, noOfSamples); if (classMI[i] > maxMI) { maxMI = classMI[i]; maxMICounter = i; }/*if bigger than current maximum*/ }/*for noOfFeatures - filling classMI*/ selectedFeatures[maxMICounter] = 1; outputFeatures[0] = maxMICounter; (*noOfOutput)++; /***************************************************************************** ** We have populated the classMI array, and selected the highest ** MI feature as the first output feature ** Now we move into the JMI algorithm *****************************************************************************/ for (i = 1; i < k; i++) { score = 0.0; currentHighestFeature = 0; currentScore = 0.0; totalFeatureMI = 0.0; for (j = 0; j < noOfFeatures; j++) { /*if we haven't selected j*/ if (selectedFeatures[j] == 0) { currentScore = 0.0; totalFeatureMI = 0.0; for (x = 0; x < i; x++) { arrayPosition = x*noOfFeatures + j; if (featureMIMatrix[arrayPosition] == -1) { mergeArrays(feature2D[(int) outputFeatures[x]], feature2D[j],mergedVector,noOfSamples); /*double calculateMutualInformation(double *firstVector, double *secondVector, int vectorLength);*/ mi = calculateMutualInformation(mergedVector, classColumn, noOfSamples); featureMIMatrix[arrayPosition] = mi; }/*if not already known*/ currentScore += featureMIMatrix[arrayPosition]; }/*for the number of already selected features*/ if (currentScore > score) { score = currentScore; currentHighestFeature = j; } }/*if j is unselected*/ }/*for number of features*/ selectedFeatures[currentHighestFeature] = 1; outputFeatures[i] = currentHighestFeature; (*noOfOutput)++; }/*for the number of features to select*/ FREE_FUNC(classMI); FREE_FUNC(feature2D); FREE_FUNC(featureMIMatrix); FREE_FUNC(mergedVector); FREE_FUNC(selectedFeatures); classMI = NULL; feature2D = NULL; featureMIMatrix = NULL; mergedVector = NULL; selectedFeatures = NULL; //outputFeatures = realloc(outputFeatures, sizeof(double)*(noOfFeatures-*noOfOutput)); }/*JMI(int,int,int,double[][],double[],double[])*/
double* CMIM(int k, int noOfSamples, int noOfFeatures, double *featureMatrix, double *classColumn, double *outputFeatures) { /*holds the class MI values **the class MI doubles as the partial score from the CMIM paper */ double *classMI = (double *)CALLOC_FUNC(noOfFeatures,sizeof(double)); /*in the CMIM paper, m = lastUsedFeature*/ int *lastUsedFeature = (int *)CALLOC_FUNC(noOfFeatures,sizeof(int)); double score, conditionalInfo; int iMinus, currentFeature; double maxMI = 0.0; int maxMICounter = -1; int j,i; double **feature2D = (double**) CALLOC_FUNC(noOfFeatures,sizeof(double*)); for(j = 0; j < noOfFeatures; j++) { feature2D[j] = featureMatrix + (int)j*noOfSamples; } for (i = 0; i < noOfFeatures;i++) { classMI[i] = calculateMutualInformation(feature2D[i], classColumn, noOfSamples); if (classMI[i] > maxMI) { maxMI = classMI[i]; maxMICounter = i; }/*if bigger than current maximum*/ }/*for noOfFeatures - filling classMI*/ outputFeatures[0] = maxMICounter; /***************************************************************************** ** We have populated the classMI array, and selected the highest ** MI feature as the first output feature ** Now we move into the CMIM algorithm *****************************************************************************/ for (i = 1; i < k; i++) { score = 0.0; iMinus = i-1; for (j = 0; j < noOfFeatures; j++) { while ((classMI[j] > score) && (lastUsedFeature[j] < i)) { /*double calculateConditionalMutualInformation(double *firstVector, double *targetVector, double *conditionVector, int vectorLength);*/ currentFeature = (int) outputFeatures[lastUsedFeature[j]]; conditionalInfo = calculateConditionalMutualInformation(feature2D[j],classColumn,feature2D[currentFeature],noOfSamples); if (classMI[j] > conditionalInfo) { classMI[j] = conditionalInfo; }/*reset classMI*/ /*moved due to C indexing from 0 rather than 1*/ lastUsedFeature[j] += 1; }/*while partial score greater than score & not reached last feature*/ if (classMI[j] > score) { score = classMI[j]; outputFeatures[i] = j; }/*if partial score still greater than score*/ }/*for number of features*/ }/*for the number of features to select*/ for (i = 0; i < k; i++) { outputFeatures[i] += 1; /*C indexes from 0 not 1*/ }/*for number of selected features*/ FREE_FUNC(classMI); FREE_FUNC(lastUsedFeature); FREE_FUNC(feature2D); classMI = NULL; lastUsedFeature = NULL; feature2D = NULL; return outputFeatures; }/*CMIM(int,int,int,double[][],double[],double[])*/
void mRMR_D(int k, int noOfSamples, int noOfFeatures, double *featureMatrix, double *classColumn, int *outputFeatures,int *noOfOutput) { /*holds the class MI values*/ double *classMI = (double *)checkedCalloc(noOfFeatures,sizeof(double)); int *selectedFeatures = (int *)checkedCalloc(noOfFeatures,sizeof(int)); /*holds the intra feature MI values*/ int sizeOfMatrix = k*noOfFeatures; double *featureMIMatrix = (double *)checkedCalloc(sizeOfMatrix,sizeof(double)); /*Changed to ensure it always picks a feature*/ double maxMI = -1.0; int maxMICounter = -1; /*init variables*/ double score, currentScore, totalFeatureMI; int currentHighestFeature; int arrayPosition, i, j, x; /*holds the first element of each sample*/ double **feature2D = (double **) checkedCalloc(noOfFeatures,sizeof(double *)); firstElementOfEachSample(feature2D,featureMatrix,noOfFeatures,noOfSamples); for (i = 0; i < sizeOfMatrix;i++) { featureMIMatrix[i] = -1; }/*for featureMIMatrix - blank to -1*/ for (i = 0; i < noOfFeatures;i++) { classMI[i] = calculateMutualInformation(feature2D[i], classColumn, noOfSamples); if (classMI[i] > maxMI) { maxMI = classMI[i]; maxMICounter = i; }/*if bigger than current maximum*/ }/*for noOfFeatures - filling classMI*/ selectedFeatures[maxMICounter] = 1; outputFeatures[0] = maxMICounter; (*noOfOutput)++; /************* ** Now we have populated the classMI array, and selected the highest ** MI feature as the first output feature ** Now we move into the mRMR-D algorithm *************/ for (i = 1; i < k; i++) { /**************************************************** ** to ensure it selects some features **if this is zero then it will not pick features where the redundancy is greater than the **relevance ****************************************************/ score = -DBL_MAX; currentHighestFeature = 0; currentScore = 0.0; totalFeatureMI = 0.0; for (j = 0; j < noOfFeatures; j++) { /*if we haven't selected j*/ if (selectedFeatures[j] == 0) { currentScore = classMI[j]; totalFeatureMI = 0.0; for (x = 0; x < i; x++) { arrayPosition = x*noOfFeatures + j; if (featureMIMatrix[arrayPosition] == -1) { /*work out intra MI*/ /*double calculateMutualInformation(double *firstVector, double *secondVector, int vectorLength);*/ featureMIMatrix[arrayPosition] = calculateMutualInformation(feature2D[(int) outputFeatures[x]], feature2D[j], noOfSamples); } totalFeatureMI += featureMIMatrix[arrayPosition]; }/*for the number of already selected features*/ currentScore -= (totalFeatureMI/i); if (currentScore > score) { score = currentScore; currentHighestFeature = j; } }/*if j is unselected*/ }/*for number of features*/ selectedFeatures[currentHighestFeature] = 1; outputFeatures[i] = currentHighestFeature; (*noOfOutput)++; }/*for the number of features to select*/ FREE_FUNC(classMI); FREE_FUNC(feature2D); FREE_FUNC(featureMIMatrix); FREE_FUNC(selectedFeatures); classMI = NULL; feature2D = NULL; featureMIMatrix = NULL; selectedFeatures = NULL; //outputFeatures = realloc(outputFeatures, sizeof(double)*(noOfFeatures-*noOfOutput)); }/*mRMR(int,int,int,double[][],double[],double[])*/
double* CondMI(int k, int noOfSamples, int noOfFeatures, double *featureMatrix, double *classColumn, double *outputFeatures) { /*holds the class MI values*/ double *classMI = (double *)CALLOC_FUNC(noOfFeatures,sizeof(double)); char *selectedFeatures = (char *)CALLOC_FUNC(noOfFeatures,sizeof(char)); /*holds the intra feature MI values*/ int sizeOfMatrix = k*noOfFeatures; double *featureMIMatrix = (double *)CALLOC_FUNC(sizeOfMatrix,sizeof(double)); double maxMI = 0.0; int maxMICounter = -1; double **feature2D = (double**) CALLOC_FUNC(noOfFeatures,sizeof(double*)); double score, currentScore, totalFeatureMI; int currentHighestFeature; double *conditionVector = (double *) CALLOC_FUNC(noOfSamples,sizeof(double)); int arrayPosition; double mi, tripEntropy; int i,j,x; for(j = 0; j < noOfFeatures; j++) { feature2D[j] = featureMatrix + (int)j*noOfSamples; } for (i = 0; i < sizeOfMatrix; i++) { featureMIMatrix[i] = -1; }/*for featureMIMatrix - blank to -1*/ for (i = 0; i < noOfFeatures; i++) { /*calculate mutual info **double calculateMutualInformation(double *firstVector, double *secondVector, int vectorLength); */ classMI[i] = calculateMutualInformation(feature2D[i], classColumn, noOfSamples); if (classMI[i] > maxMI) { maxMI = classMI[i]; maxMICounter = i; }/*if bigger than current maximum*/ }/*for noOfFeatures - filling classMI*/ selectedFeatures[maxMICounter] = 1; outputFeatures[0] = maxMICounter; memcpy(conditionVector,feature2D[maxMICounter],sizeof(double)*noOfSamples); /***************************************************************************** ** We have populated the classMI array, and selected the highest ** MI feature as the first output feature ** Now we move into the CondMI algorithm *****************************************************************************/ for (i = 1; i < k; i++) { score = 0.0; currentHighestFeature = -1; currentScore = 0.0; totalFeatureMI = 0.0; for (j = 0; j < noOfFeatures; j++) { /*if we haven't selected j*/ if (selectedFeatures[j] == 0) { currentScore = 0.0; totalFeatureMI = 0.0; /*double calculateConditionalMutualInformation(double *firstVector, double *targetVector, double *conditionVector, int vectorLength);*/ currentScore = calculateConditionalMutualInformation(feature2D[j],classColumn,conditionVector,noOfSamples); if (currentScore > score) { score = currentScore; currentHighestFeature = j; } }/*if j is unselected*/ }/*for number of features*/ outputFeatures[i] = currentHighestFeature; if (currentHighestFeature != -1) { selectedFeatures[currentHighestFeature] = 1; mergeArrays(feature2D[currentHighestFeature],conditionVector,conditionVector,noOfSamples); } }/*for the number of features to select*/ FREE_FUNC(classMI); FREE_FUNC(conditionVector); FREE_FUNC(feature2D); FREE_FUNC(featureMIMatrix); FREE_FUNC(selectedFeatures); classMI = NULL; conditionVector = NULL; feature2D = NULL; featureMIMatrix = NULL; selectedFeatures = NULL; return outputFeatures; }/*CondMI(int,int,int,double[][],double[],double[])*/
/******************************************************************************* **entry point for the mex call **nlhs - number of outputs **plhs - pointer to array of outputs **nrhs - number of inputs **prhs - pointer to array of inputs *******************************************************************************/ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /***************************************************************************** ** this function takes a flag and a variable number of arguments ** depending on the value of the flag and returns either a construct ** containing probability estimates, a merged vector or a double value ** representing an entropy or mutual information *****************************************************************************/ int flag, i, numberOfSamples, checkSamples, thirdCheckSamples, numberOfFeatures, checkFeatures, thirdCheckFeatures; int numArities, errorTest; double *dataVector, *condVector, *targetVector, *firstVector, *secondVector, *output, *numStates; double *matrix, *mergedVector, *arities; int *outputIntVector, *intArities; double *jointOutput, *numJointStates, *firstOutput, *numFirstStates, *secondOutput, *numSecondStates; ProbabilityState state; JointProbabilityState jointState; /*if (nlhs != 1) { printf("Incorrect number of output arguments\n"); }//if not 1 output */ switch (nrhs) { case 2: { /*printf("Must be H(X), calculateProbability(X), merge(X), normaliseArray(X)\n");*/ break; } case 3: { /*printf("Must be H(XY), H(X|Y), calculateJointProbability(XY), I(X;Y)\n");*/ break; } case 4: { /*printf("Must be I(X;Y|Z)\n");*/ break; } default: { printf("Incorrect number of arguments, format is MIToolbox(\"FLAG\",varargin)\n"); break; } } /* number to function map ** 1 = calculateProbability ** 2 = calculateJointProbability ** 3 = mergeArrays ** 4 = H(X) ** 5 = H(XY) ** 6 = H(X|Y) ** 7 = I(X;Y) ** 8 = I(X;Y|Z) ** 9 = normaliseArray */ flag = *mxGetPr(prhs[0]); switch (flag) { case 1: { /* **calculateProbability */ numberOfSamples = mxGetM(prhs[1]); dataVector = (double *) mxGetPr(prhs[1]); /*ProbabilityState calculateProbability(double *dataVector, int vectorLength);*/ state = calculateProbability(dataVector,numberOfSamples); plhs[0] = mxCreateDoubleMatrix(state.numStates,1,mxREAL); plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); output = (double *)mxGetPr(plhs[0]); numStates = (double *) mxGetPr(plhs[1]); *numStates = state.numStates; for (i = 0; i < state.numStates; i++) { output[i] = state.probabilityVector[i]; } break; }/*case 1 - calculateProbability*/ case 2: { /* **calculateJointProbability */ numberOfSamples = mxGetM(prhs[1]); firstVector = (double *) mxGetPr(prhs[1]); secondVector = (double *) mxGetPr(prhs[2]); /*JointProbabilityState calculateJointProbability(double *firstVector, double *secondVector int vectorLength);*/ jointState = calculateJointProbability(firstVector,secondVector,numberOfSamples); plhs[0] = mxCreateDoubleMatrix(jointState.numJointStates,1,mxREAL); plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); plhs[2] = mxCreateDoubleMatrix(jointState.numFirstStates,1,mxREAL); plhs[3] = mxCreateDoubleMatrix(1,1,mxREAL); plhs[4] = mxCreateDoubleMatrix(jointState.numSecondStates,1,mxREAL); plhs[5] = mxCreateDoubleMatrix(1,1,mxREAL); jointOutput = (double *)mxGetPr(plhs[0]); numJointStates = (double *) mxGetPr(plhs[1]); firstOutput = (double *)mxGetPr(plhs[2]); numFirstStates = (double *) mxGetPr(plhs[3]); secondOutput = (double *)mxGetPr(plhs[4]); numSecondStates = (double *) mxGetPr(plhs[5]); *numJointStates = jointState.numJointStates; *numFirstStates = jointState.numFirstStates; *numSecondStates = jointState.numSecondStates; for (i = 0; i < jointState.numJointStates; i++) { jointOutput[i] = jointState.jointProbabilityVector[i]; } for (i = 0; i < jointState.numFirstStates; i++) { firstOutput[i] = jointState.firstProbabilityVector[i]; } for (i = 0; i < jointState.numSecondStates; i++) { secondOutput[i] = jointState.secondProbabilityVector[i]; } break; }/*case 2 - calculateJointProbability */ case 3: { /* **mergeArrays */ numberOfSamples = mxGetM(prhs[1]); numberOfFeatures = mxGetN(prhs[1]); numArities = 0; if (nrhs > 2) { numArities = mxGetN(prhs[2]); /*printf("arities = %d, features = %d, samples = %d\n",numArities,numberOfFeatures,numberOfSamples);*/ } plhs[0] = mxCreateDoubleMatrix(0,0,mxREAL); if (numArities == 0) { /* **no arities therefore compress output */ if ((numberOfFeatures > 0) && (numberOfSamples > 0)) { matrix = (double *) mxGetPr(prhs[1]); mergedVector = (double *) mxCalloc(numberOfSamples,sizeof(double)); plhs[0] = mxCreateDoubleMatrix(numberOfSamples,1,mxREAL); output = (double *)mxGetPr(plhs[0]); /*int mergeMultipleArrays(double *inputMatrix, double *outputVector, int matrixWidth, int vectorLength)*/ mergeMultipleArrays(matrix, mergedVector, numberOfFeatures, numberOfSamples); for (i = 0; i < numberOfSamples; i++) { output[i] = mergedVector[i]; } mxFree(mergedVector); mergedVector = NULL; } } else if (numArities == numberOfFeatures) { if ((numberOfFeatures > 0) && (numberOfSamples > 0)) { matrix = (double *) mxGetPr(prhs[1]); mergedVector = (double *) mxCalloc(numberOfSamples,sizeof(double)); arities = (double *) mxGetPr(prhs[2]); intArities = (int *) mxCalloc(numberOfFeatures,sizeof(int)); for (i = 0; i < numArities; i++) { intArities[i] = (int) floor(arities[i]); } /*int mergeMultipleArrays(double *inputMatrix, double *outputVector, int matrixWidth, int *arities, int vectorLength);*/ errorTest = mergeMultipleArraysArities(matrix, mergedVector, numberOfFeatures, intArities, numberOfSamples); if (errorTest != -1) { plhs[0] = mxCreateDoubleMatrix(numberOfSamples,1,mxREAL); output = (double *)mxGetPr(plhs[0]); for (i = 0; i < numberOfSamples; i++) { output[i] = mergedVector[i]; } } else { printf("Incorrect arities supplied. More states in data than specified\n"); } mxFree(mergedVector); mergedVector = NULL; } } else { printf("Number of arities does not match number of features, arities should be a row vector\n"); } break; }/*case 3 - mergeArrays*/ case 4: { /* **H(X) */ numberOfSamples = mxGetM(prhs[1]); numberOfFeatures = mxGetN(prhs[1]); dataVector = (double *) mxGetPr(prhs[1]); plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); output = (double *)mxGetPr(plhs[0]); if (numberOfFeatures == 1) { /*double calculateEntropy(double *dataVector, int vectorLength);*/ *output = calculateEntropy(dataVector,numberOfSamples); } else { printf("No columns in input\n"); *output = -1.0; } break; }/*case 4 - H(X)*/ case 5: { /* **H(XY) */ numberOfSamples = mxGetM(prhs[1]); checkSamples = mxGetM(prhs[2]); numberOfFeatures = mxGetN(prhs[1]); checkFeatures = mxGetN(prhs[2]); firstVector = mxGetPr(prhs[1]); secondVector = mxGetPr(prhs[2]); plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); output = (double *)mxGetPr(plhs[0]); if ((numberOfFeatures == 1) && (checkFeatures == 1)) { if ((numberOfSamples == 0) && (checkSamples == 0)) { *output = 0.0; } else if (numberOfSamples == 0) { *output = calculateEntropy(secondVector,numberOfSamples); } else if (checkSamples == 0) { *output = calculateEntropy(firstVector,numberOfSamples); } else if (numberOfSamples == checkSamples) { /*double calculateJointEntropy(double *firstVector, double *secondVector, int vectorLength);*/ *output = calculateJointEntropy(firstVector,secondVector,numberOfSamples); } else { printf("Vector lengths do not match, they must be the same length\n"); *output = -1.0; } } else { printf("No columns in input\n"); *output = -1.0; } break; }/*case 5 - H(XY)*/ case 6: { /* **H(X|Y) */ numberOfSamples = mxGetM(prhs[1]); checkSamples = mxGetM(prhs[2]); numberOfFeatures = mxGetN(prhs[1]); checkFeatures = mxGetN(prhs[2]); dataVector = mxGetPr(prhs[1]); condVector = mxGetPr(prhs[2]); plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); output = (double *)mxGetPr(plhs[0]); if ((numberOfFeatures == 1) && (checkFeatures == 1)) { if (numberOfSamples == 0) { *output = 0.0; } else if (checkSamples == 0) { *output = calculateEntropy(dataVector,numberOfSamples); } else if (numberOfSamples == checkSamples) { /*double calculateConditionalEntropy(double *dataVector, double *condVector, int vectorLength);*/ *output = calculateConditionalEntropy(dataVector,condVector,numberOfSamples); } else { printf("Vector lengths do not match, they must be the same length\n"); *output = -1.0; } } else { printf("No columns in input\n"); *output = -1.0; } break; }/*case 6 - H(X|Y)*/ case 7: { /* **I(X;Y) */ numberOfSamples = mxGetM(prhs[1]); checkSamples = mxGetM(prhs[2]); numberOfFeatures = mxGetN(prhs[1]); checkFeatures = mxGetN(prhs[2]); firstVector = mxGetPr(prhs[1]); secondVector = mxGetPr(prhs[2]); plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); output = (double *)mxGetPr(plhs[0]); if ((numberOfFeatures == 1) && (checkFeatures == 1)) { if ((numberOfSamples == 0) || (checkSamples == 0)) { *output = 0.0; } else if (numberOfSamples == checkSamples) { /*double calculateMutualInformation(double *firstVector, double *secondVector, int vectorLength);*/ *output = calculateMutualInformation(firstVector,secondVector,numberOfSamples); } else { printf("Vector lengths do not match, they must be the same length\n"); *output = -1.0; } } else { printf("No columns in input\n"); *output = -1.0; } break; }/*case 7 - I(X;Y)*/ case 8: { /* **I(X;Y|Z) */ numberOfSamples = mxGetM(prhs[1]); checkSamples = mxGetM(prhs[2]); thirdCheckSamples = mxGetM(prhs[3]); numberOfFeatures = mxGetN(prhs[1]); checkFeatures = mxGetN(prhs[2]); thirdCheckFeatures = mxGetN(prhs[3]); firstVector = mxGetPr(prhs[1]); targetVector = mxGetPr(prhs[2]); condVector = mxGetPr(prhs[3]); plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); output = (double *)mxGetPr(plhs[0]); if ((numberOfFeatures == 1) && (checkFeatures == 1)) { if ((numberOfSamples == 0) || (checkSamples == 0)) { *output = 0.0; } else if ((thirdCheckSamples == 0) || (thirdCheckFeatures != 1)) { *output = calculateMutualInformation(firstVector,targetVector,numberOfSamples); } else if ((numberOfSamples == checkSamples) && (numberOfSamples == thirdCheckSamples)) { /*double calculateConditionalMutualInformation(double *firstVector, double *targetVector, double *condVector, int vectorLength);*/ *output = calculateConditionalMutualInformation(firstVector,targetVector,condVector,numberOfSamples); } else { printf("Vector lengths do not match, they must be the same length\n"); *output = -1.0; } } else { printf("No columns in input\n"); *output = -1.0; } break; }/*case 8 - I(X;Y|Z)*/ case 9: { /* **normaliseArray */ numberOfSamples = mxGetM(prhs[1]); dataVector = (double *) mxGetPr(prhs[1]); outputIntVector = (int *) mxCalloc(numberOfSamples,sizeof(int)); plhs[0] = mxCreateDoubleMatrix(numberOfSamples,1,mxREAL); plhs[1] = mxCreateDoubleMatrix(1,1,mxREAL); output = (double *)mxGetPr(plhs[0]); numStates = (double *) mxGetPr(plhs[1]); /*int normaliseArray(double *inputVector, int *outputVector, int vectorLength);*/ *numStates = normaliseArray(dataVector, outputIntVector, numberOfSamples); for (i = 0; i < numberOfSamples; i++) { output[i] = outputIntVector[i]; } break; }/*case 9 - normaliseArray*/ default: { printf("Unrecognised flag\n"); break; }/*default*/ }/*switch(flag)*/ return; }/*mexFunction()*/