├── COPYING ├── MarkovChain.cpp ├── MarkovChain.h ├── README ├── examples └── examples.ino └── tests └── tests.ino /COPYING: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 16 | 17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 18 | 19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 20 | 21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 22 | 23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 24 | 25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 26 | 27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 28 | 29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 30 | 31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 36 | 37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 38 | 39 | You must cause any modified files to carry prominent notices stating that You changed the files; and 40 | 41 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 42 | 43 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 44 | 45 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 46 | 47 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 48 | 49 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 50 | 51 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 52 | 53 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 54 | 55 | 56 | -------------------------------------------------------------------------------- /MarkovChain.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | MarkovChain.cpp - Library for using Markov Chains in Arduino. 3 | */ 4 | 5 | #include "Arduino.h" 6 | #include "MarkovChain.h" 7 | 8 | int* MarkovChain::getFirstStates(char* elements, int numOfElements, char** sequences, int rows){ 9 | int* firstStates = (int *)malloc(numOfElements*sizeof(int)); 10 | 11 | 12 | for(int iElement = 0; iElement < numOfElements; iElement++){ 13 | int count = 0; 14 | char element = elements[iElement]; 15 | for(int iSequence = 0; iSequence < rows; iSequence++){ 16 | char firstElement = sequences[iSequence][0]; 17 | if (firstElement == element){ 18 | count++; 19 | } 20 | } 21 | firstStates[iElement] = count; 22 | } 23 | return firstStates; 24 | } 25 | 26 | int MarkovChain::counterTransitionsInSequence(char expectedFrom, char expectedTo, char sequence [], int numOfElements){ 27 | int count = 0; 28 | for (int i = 0; i < numOfElements; i++) { 29 | char actualFrom = sequence[i]; 30 | char actualTo = sequence[i + 1]; 31 | if((actualFrom == expectedFrom) && (actualTo == expectedTo)) 32 | count++; 33 | } 34 | return count; 35 | } 36 | 37 | int MarkovChain::countElementsInSequence(char * sequence){ 38 | int count = -1; 39 | char current = '1'; 40 | while(current != '\0'){ 41 | count++; 42 | current = sequence[count]; 43 | } 44 | return count; 45 | } 46 | 47 | 48 | int** MarkovChain::createTransitionMatrix(char elements [], int numOfElements, char ** sequences, int numSequences){ 49 | //The number of rows and columns of the transition matrix always equals 50 | //to the number of distinc elements 51 | int** matrix; 52 | matrix = (int **) malloc(numOfElements*sizeof(int *)); 53 | for(int i = 0; i < numOfElements; i++) 54 | matrix[i] = (int *) malloc(numOfElements*sizeof(int)); 55 | 56 | int row = 0; 57 | int col = 0; 58 | int count = 0; 59 | for(int iFrom = 0; iFrom < numOfElements; iFrom++){ 60 | for (int iTo = 0; iTo < numOfElements; iTo++){ 61 | for(int iSequences = 0; iSequences < numSequences; iSequences++){ 62 | char from = elements[iFrom]; 63 | char to = elements[iTo]; 64 | char* sequence = sequences[iSequences]; 65 | int num = countElementsInSequence(sequence); 66 | count = count + counterTransitionsInSequence(from, to, sequence,num); 67 | } 68 | matrix[row][col] = count; 69 | count = 0; 70 | col++; 71 | } 72 | row++; 73 | col = 0; 74 | } 75 | return matrix; 76 | } 77 | 78 | int* MarkovChain::countRowsTotals(int** transitionMatrix, int numOfElements){ 79 | int* totals = (int *)malloc(numOfElements*sizeof(int)); 80 | for(int i = 0; i < numOfElements; ++i){ 81 | totals[i] = 0; 82 | } 83 | 84 | for(int i = 0; i < numOfElements; ++i){ 85 | for (int j = 0; j < numOfElements; ++j){ 86 | totals[i] = totals[i] + transitionMatrix[i][j]; 87 | } 88 | } 89 | return totals; 90 | } 91 | 92 | double** MarkovChain::createTransitionProbabilityMatrix(int* rowsTotals, int** transitionMatrix, int numOfElements){ 93 | double** probMatrix; 94 | probMatrix = (double **) malloc(numOfElements*sizeof(double *)); 95 | for(int i = 0; i < numOfElements; i++) 96 | probMatrix[i] = (double *) malloc(numOfElements*sizeof(double)); 97 | 98 | for (int i = 0; i < numOfElements; i++) { 99 | for (int j = 0; j < numOfElements; j++) { 100 | probMatrix[i][j] = (double)transitionMatrix[i][j] / (double)rowsTotals[i]; 101 | } 102 | } 103 | 104 | return probMatrix; 105 | } 106 | 107 | //the returning array has the state probabilities in the same order as the "elements" array 108 | double * MarkovChain::calculateFirstStatesProbabilities (char* elements, int numOfElements, char** sequences, int numOfSecuences){ 109 | double * firstStateProbs = (double *)malloc(numOfElements*sizeof(double)); 110 | int* firstStates = getFirstStates(elements, numOfElements, sequences, numOfSecuences); 111 | int total = 0; 112 | for(int i = 0; i < numOfElements; i++){ 113 | total = total + firstStates[i]; 114 | } 115 | 116 | for(int i = 0; i < numOfElements; i++){ 117 | firstStateProbs[i] = (double)firstStates[i] / (double)total; 118 | } 119 | 120 | return firstStateProbs; 121 | } 122 | 123 | int getElementPosition(char element, char* elements, int numOfElements){ 124 | for (int i = 0; i < numOfElements; i++){ 125 | if (element == elements[i]) 126 | return i; 127 | } 128 | } 129 | 130 | //****************************************************************************************** 131 | //******* Only the following functions are needed to be called to use Markov Chains ******** 132 | //****************************************************************************************** 133 | 134 | /* 135 | * Returns the probabilities for the next states. The probabilities appear in the same order 136 | * that appear in "elements" 137 | */ 138 | 139 | double* MarkovChain::getNextTransitions(char element, char* elements, int numOfElements, char ** sequences, int numSequences ){ 140 | double* probabilities = (double *)malloc(numOfElements*sizeof(double)); 141 | int row = -1; 142 | for (int i = 0; i < numOfElements; i++){ 143 | if (elements[i] == element && row == -1){ 144 | row = i; 145 | } 146 | } 147 | 148 | if (row == -1){ 149 | return NULL; 150 | } 151 | 152 | int ** transitionMatrix = createTransitionMatrix(elements, numOfElements, sequences, numSequences);; 153 | int * rowsTotals = countRowsTotals(transitionMatrix, numOfElements); 154 | double ** transitionProbabilityMatrix = createTransitionProbabilityMatrix(rowsTotals, transitionMatrix, numOfElements); 155 | 156 | for (int i = 0; i < numOfElements; i++) { 157 | double prob = transitionProbabilityMatrix[row][i]; 158 | probabilities[i] = prob; 159 | } 160 | 161 | for(int i = 0; i < numOfElements; i++) 162 | free(transitionMatrix[i]); 163 | free(transitionMatrix); 164 | 165 | free(rowsTotals); 166 | 167 | for(int i = 0; i < numOfElements; i++) 168 | free(transitionProbabilityMatrix[i]); 169 | free(transitionProbabilityMatrix); 170 | 171 | return probabilities; 172 | } 173 | 174 | /* 175 | * Returns the ocurrence probability of a given sequence 176 | */ 177 | 178 | double MarkovChain::getSequenceProbability(char* sequence, int seqElementsNum, char* elements, int numOfElements, char ** sequences, int numSequences){ 179 | double probability = 1.0; 180 | 181 | int ** transitionMatrix = createTransitionMatrix(elements, numOfElements, sequences, numSequences);; 182 | int * rowsTotals = countRowsTotals(transitionMatrix, numOfElements); 183 | double **transitionProbabilityMatrix = createTransitionProbabilityMatrix(rowsTotals, transitionMatrix, numOfElements); 184 | double * firstStateProbabilities = calculateFirstStatesProbabilities (elements, numOfElements, sequences, numSequences); 185 | 186 | for (int i = 0; i < seqElementsNum-1; i++) { 187 | 188 | int row = getElementPosition(sequence[i], elements, numOfElements); 189 | int col = getElementPosition(sequence[i+1], elements, numOfElements); 190 | 191 | probability = probability * transitionProbabilityMatrix[row][col]; 192 | } 193 | int firstElementPos = getElementPosition(sequence[0], elements, numOfElements); 194 | probability = firstStateProbabilities[firstElementPos] * probability; 195 | 196 | for(int i = 0; i < numOfElements; i++) 197 | free(transitionMatrix[i]); 198 | free(transitionMatrix); 199 | 200 | free(rowsTotals); 201 | 202 | for(int i = 0; i < numOfElements; i++) 203 | free(transitionProbabilityMatrix[i]); 204 | free(transitionProbabilityMatrix); 205 | 206 | free (firstStateProbabilities); 207 | 208 | return probability; 209 | } 210 | 211 | -------------------------------------------------------------------------------- /MarkovChain.h: -------------------------------------------------------------------------------- 1 | /* 2 | MarkovChain.h - Library for using Markov Chains in Arduino 3 | */ 4 | #ifndef MarkovChain_h 5 | #define MarkovChain_h 6 | 7 | #include "Arduino.h" 8 | 9 | class MarkovChain 10 | { 11 | public: 12 | int* getFirstStates(char* elements, int numOfElements, char** sequences, int rows); 13 | int counterTransitionsInSequence(char expectedFrom, char expectedTo, char sequence [], int numOfElements); 14 | int countElementsInSequence(char * sequence); 15 | int** createTransitionMatrix(char elements [], int numOfElements, char ** sequences, int numSequences); 16 | int* countRowsTotals(int** transitionMatrix, int numOfElements); 17 | double** createTransitionProbabilityMatrix(int* rowsTotals, int** transitionMatrix, int numOfElements); 18 | double * calculateFirstStatesProbabilities (char* elements, int numOfElements, char** sequences, int numOfSecuences); 19 | 20 | //****************************************************************************************** 21 | //******* Only the following functions are needed to be called to use Markov Chains ******** 22 | //****************************************************************************************** 23 | double* getNextTransitions(char element, char* elements, int numOfElements, char ** sequences, int numSequences ); 24 | double getSequenceProbability(char* sequence, int seqElementsNum, char* elements, int numOfElements, char ** sequences, int numSequences); 25 | }; 26 | 27 | #endif -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Library for Markov Chains in Arduino. 2 | 3 | The examples directory contains a straightforward example of the library usage. 4 | The tests directory contains some adhoc unit tests for the library. -------------------------------------------------------------------------------- /examples/examples.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MarkovChain chain; 4 | 5 | void usageExample(){ 6 | 7 | //Training set to create the transition matrix. It has 3 different sequences. 8 | char ** trainingSet; 9 | trainingSet = (char **) malloc(3*sizeof(char *)); 10 | for(int i = 0; i < 3; i++) 11 | trainingSet[i] = (char *) malloc(5*sizeof(char)); 12 | 13 | //Sequence 0 14 | trainingSet[0][0] = 'a'; 15 | trainingSet[0][1] = 'b'; 16 | trainingSet[0][2] = 'c'; 17 | trainingSet[0][3] = 'c'; 18 | trainingSet[0][4] = '\0'; 19 | //Sequence 1 20 | trainingSet[1][0] = 'a'; 21 | trainingSet[1][1] = 'a'; 22 | trainingSet[1][2] = 'a'; 23 | trainingSet[1][3] = 'a'; 24 | trainingSet[1][4] = '\0'; 25 | //Sequence 2 26 | trainingSet[2][0] = 'b'; 27 | trainingSet[2][1] = 'a'; 28 | trainingSet[2][2] = 'b'; 29 | trainingSet[2][3] = 'c'; 30 | trainingSet[2][4] = '\0'; 31 | 32 | // The sequences are composed by three elements: a, b and c 33 | char elements [] = {'a', 'b', 'c'}; 34 | 35 | //We calculate the probability of a element apearing after 'a' 36 | double* probs = chain.getNextTransitions('a', elements, 3, trainingSet, 3); 37 | 38 | Serial.println("Probability of appearing after element /'a/'"); 39 | for (int i = 0; i < 3; i++){ 40 | Serial.print(elements[i] + ": "); 41 | Serial.print(probs[i]); 42 | Serial.println(); 43 | } 44 | 45 | //We can also calculate the ocurrence probability of a given sequence using the transition matrix 46 | //and first element probabilities 47 | char sequence [] = {'a','b','a'}; 48 | double probability = chain.getSequenceProbability(sequence, 3, elements, 3, trainingSet, 3); 49 | Serial.print("Ocurrence probability of the sequence a-b-a: "); 50 | Serial.print(probability); 51 | Serial.println(); 52 | } 53 | 54 | void setup() { 55 | // put your setup code here, to run once: 56 | Serial.begin(9600); 57 | usageExample(); 58 | } 59 | 60 | void loop() { 61 | } 62 | -------------------------------------------------------------------------------- /tests/tests.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MarkovChain chain; 4 | 5 | void testGetFirstStates(){ 6 | Serial.println(); 7 | Serial.println("*** TEST: testGetFirstStates ***"); 8 | char ** sequences; 9 | sequences = (char **) malloc(3*sizeof(char *)); 10 | for(int i = 0; i < 3; i++) 11 | sequences[i] = (char *) malloc(3*sizeof(char)); 12 | 13 | sequences[0][0] = 'a'; 14 | sequences[0][1] = 'b'; 15 | sequences[0][2] = 'c'; 16 | sequences[1][0] = 'a'; 17 | sequences[1][1] = 'b'; 18 | sequences[1][2] = 'c'; 19 | sequences[2][0] = 'b'; 20 | sequences[2][1] = 'b'; 21 | sequences[2][2] = 'c'; 22 | 23 | char elements [] = {'a', 'b', 'c'}; 24 | int* firstStates = chain.getFirstStates(elements, 3, sequences, 3); 25 | 26 | Serial.print("** R1 expected value: 2 actual value: "); 27 | Serial.print(firstStates[0]); 28 | Serial.println(); 29 | Serial.print("** R2 expected value: 1 actual value: "); 30 | Serial.print(firstStates[1]); 31 | Serial.println(); 32 | Serial.print("** R3 expected value: 0 actual value: "); 33 | Serial.print(firstStates[2]); 34 | Serial.println(); 35 | 36 | for(int i = 0; i < 3; i++) 37 | free(sequences[i]); 38 | free(sequences); 39 | free(firstStates); 40 | } 41 | 42 | void testCounterTransitionsInSequence(){ 43 | Serial.println(); 44 | Serial.println("*** TEST: testCounterTransitionsInSequence ***"); 45 | char sequence [] = {'a','b','b','c','a','b','\0'}; 46 | int actual = chain.counterTransitionsInSequence('a', 'b', sequence, 6); 47 | Serial.print("** Expected value: 2, actual: "); 48 | Serial.print(actual); 49 | Serial.println(); 50 | char sequence2 [] = {'a','b','b','c','a','b','\0'}; 51 | int actual2 = chain.counterTransitionsInSequence('b', 'b', sequence2, 6); 52 | Serial.print("** Expected value: 1, actual: "); 53 | Serial.print(actual2); 54 | Serial.println(); 55 | 56 | } 57 | 58 | void testCountElementsInSequence(){ 59 | Serial.println(); 60 | Serial.println("*** TEST: testCountElementsInSequence ***"); 61 | char sequence [] = {'a','b','b','c','a','b','\0'}; 62 | int actual = chain.countElementsInSequence(sequence); 63 | Serial.print("** Expected value: 6, actual: "); 64 | Serial.print(actual); 65 | Serial.println(); 66 | } 67 | 68 | void testCreateTransitionMatrix(){ 69 | Serial.println(); 70 | Serial.println("*** TEST: testCreateTransitionMatrix ***"); 71 | 72 | char ** sequences; 73 | sequences = (char **) malloc(3*sizeof(char *)); 74 | for(int i = 0; i < 3; i++) 75 | sequences[i] = (char *) malloc(5*sizeof(char)); 76 | 77 | //Sequence 0 78 | sequences[0][0] = 'a'; 79 | sequences[0][1] = 'b'; 80 | sequences[0][2] = 'b'; 81 | sequences[0][3] = 'b'; 82 | sequences[0][4] = '\0'; 83 | //Sequence 1 84 | sequences[1][0] = 'b'; 85 | sequences[1][1] = 'a'; 86 | sequences[1][2] = 'b'; 87 | sequences[1][3] = 'a'; 88 | sequences[1][4] = '\0'; 89 | //Sequence 2 90 | sequences[2][0] = 'a'; 91 | sequences[2][1] = 'a'; 92 | sequences[2][2] = 'b'; 93 | sequences[2][3] = 'a'; 94 | sequences[2][4] = '\0'; 95 | 96 | char elements [] = {'a', 'b'}; 97 | int** matrix = chain.createTransitionMatrix(elements, 2, sequences, 3); 98 | 99 | Serial.print("** Expected value: 1, actual: "); 100 | Serial.print(matrix[0][0]); 101 | Serial.println(); 102 | Serial.print("** Expected value: 3, actual: "); 103 | Serial.print(matrix[0][1]); 104 | Serial.println(); 105 | Serial.print("** Expected value: 3, actual: "); 106 | Serial.print(matrix[1][0]); 107 | Serial.println(); 108 | Serial.print("** Expected value: 2, actual: "); 109 | Serial.print(matrix[1][1]); 110 | Serial.println(); 111 | 112 | for(int i = 0; i < 3; i++) 113 | free(sequences[i]); 114 | free(sequences); 115 | 116 | for(int i = 0; i < 2; i++) 117 | free(matrix[i]); 118 | free(matrix); 119 | } 120 | 121 | void testCountRowsTotals(){ 122 | Serial.println(); 123 | Serial.println("*** TEST: testCountRowsTotals ***"); 124 | 125 | char ** sequences; 126 | sequences = (char **) malloc(3*sizeof(char *)); 127 | for(int i = 0; i < 3; i++) 128 | sequences[i] = (char *) malloc(5*sizeof(char)); 129 | 130 | //Sequence 0 131 | sequences[0][0] = 'a'; 132 | sequences[0][1] = 'b'; 133 | sequences[0][2] = 'b'; 134 | sequences[0][3] = 'b'; 135 | sequences[0][4] = '\0'; 136 | //Sequence 1 137 | sequences[1][0] = 'b'; 138 | sequences[1][1] = 'a'; 139 | sequences[1][2] = 'b'; 140 | sequences[1][3] = 'a'; 141 | sequences[1][4] = '\0'; 142 | //Sequence 2 143 | sequences[2][0] = 'a'; 144 | sequences[2][1] = 'a'; 145 | sequences[2][2] = 'b'; 146 | sequences[2][3] = 'a'; 147 | sequences[2][4] = '\0'; 148 | 149 | char elements [] = {'a', 'b'}; 150 | int** matrix = chain.createTransitionMatrix(elements, 2, sequences, 3); 151 | int* rowsTotals = chain.countRowsTotals(matrix, 2); 152 | 153 | Serial.print("** Expected value: 4, actual: "); 154 | Serial.print(rowsTotals[0]); 155 | Serial.println(); 156 | Serial.print("** Expected value: 5, actual: "); 157 | Serial.print(rowsTotals[1]); 158 | Serial.println(); 159 | 160 | 161 | for(int i = 0; i < 3; i++) 162 | free(sequences[i]); 163 | free(sequences); 164 | 165 | for(int i = 0; i < 2; i++) 166 | free(matrix[i]); 167 | free(matrix); 168 | 169 | free(rowsTotals); 170 | } 171 | 172 | void testCreateTransitionProbabilityMatrix(){ 173 | Serial.println(); 174 | Serial.println("*** TEST: testCreateTransitionProbabilityMatrix ***"); 175 | 176 | char ** sequences; 177 | sequences = (char **) malloc(3*sizeof(char *)); 178 | for(int i = 0; i < 3; i++) 179 | sequences[i] = (char *) malloc(5*sizeof(char)); 180 | 181 | //Sequence 0 182 | sequences[0][0] = 'a'; 183 | sequences[0][1] = 'b'; 184 | sequences[0][2] = 'b'; 185 | sequences[0][3] = 'b'; 186 | sequences[0][4] = '\0'; 187 | //Sequence 1 188 | sequences[1][0] = 'b'; 189 | sequences[1][1] = 'a'; 190 | sequences[1][2] = 'b'; 191 | sequences[1][3] = 'a'; 192 | sequences[1][4] = '\0'; 193 | //Sequence 2 194 | sequences[2][0] = 'a'; 195 | sequences[2][1] = 'a'; 196 | sequences[2][2] = 'b'; 197 | sequences[2][3] = 'a'; 198 | sequences[2][4] = '\0'; 199 | 200 | char elements [] = {'a', 'b'}; 201 | int** matrix = chain.createTransitionMatrix(elements, 2, sequences, 3); 202 | int* rowsTotals = chain.countRowsTotals(matrix, 2); 203 | double** probMatrix = chain.createTransitionProbabilityMatrix(rowsTotals, matrix, 2); 204 | 205 | Serial.print("** Expected value: "); 206 | Serial.print(1.0/4.0); 207 | Serial.print(" actual: "); 208 | Serial.print(probMatrix[0][0]); 209 | Serial.println(); 210 | Serial.print("** Expected value: "); 211 | Serial.print(3.0/4.0); 212 | Serial.print(" actual: "); 213 | Serial.print(probMatrix[0][1]); 214 | Serial.println(); 215 | Serial.print("** Expected value: "); 216 | Serial.print(3.0/5.0); 217 | Serial.print(" actual: "); 218 | Serial.print(probMatrix[1][0]); 219 | Serial.println(); 220 | Serial.print("** Expected value: "); 221 | Serial.print(2.0/5.0); 222 | Serial.print(" actual: "); 223 | Serial.print(probMatrix[1][1]); 224 | Serial.println(); 225 | 226 | for(int i = 0; i < 3; i++) 227 | free(sequences[i]); 228 | free(sequences); 229 | 230 | for(int i = 0; i < 2; i++) 231 | free(matrix[i]); 232 | free(matrix); 233 | 234 | for(int i = 0; i < 2; i++) 235 | free(probMatrix[i]); 236 | free(probMatrix); 237 | 238 | free(rowsTotals); 239 | } 240 | 241 | void testCalculateFirstStatesProbabilities(){ 242 | Serial.println(); 243 | Serial.println("*** TEST: testCalculateFirstStatesProbabilities ***"); 244 | 245 | char ** sequences; 246 | sequences = (char **) malloc(3*sizeof(char *)); 247 | for(int i = 0; i < 3; i++) 248 | sequences[i] = (char *) malloc(5*sizeof(char)); 249 | 250 | //Sequence 0 251 | sequences[0][0] = 'a'; 252 | sequences[0][1] = 'b'; 253 | sequences[0][2] = 'b'; 254 | sequences[0][3] = 'b'; 255 | sequences[0][4] = '\0'; 256 | //Sequence 1 257 | sequences[1][0] = 'b'; 258 | sequences[1][1] = 'a'; 259 | sequences[1][2] = 'b'; 260 | sequences[1][3] = 'a'; 261 | sequences[1][4] = '\0'; 262 | //Sequence 2 263 | sequences[2][0] = 'a'; 264 | sequences[2][1] = 'a'; 265 | sequences[2][2] = 'b'; 266 | sequences[2][3] = 'a'; 267 | sequences[2][4] = '\0'; 268 | 269 | char elements [] = {'a', 'b'}; 270 | double * firstStatesProbs = chain.calculateFirstStatesProbabilities (elements, 2, sequences, 3); 271 | 272 | Serial.print("** Expected value: "); 273 | Serial.print(2.0/3.0); 274 | Serial.print(" actual: "); 275 | Serial.print(firstStatesProbs[0]); 276 | Serial.println(); 277 | Serial.print("** Expected value: "); 278 | Serial.print(1.0/3.0); 279 | Serial.print(" actual: "); 280 | Serial.print(firstStatesProbs[1]); 281 | Serial.println(); 282 | 283 | for(int i = 0; i < 3; i++) 284 | free(sequences[i]); 285 | free(sequences); 286 | 287 | free(firstStatesProbs); 288 | } 289 | 290 | void testGetNextTransitions(){ 291 | Serial.println(); 292 | Serial.println("*** TEST: testGetNextTransitions ***"); 293 | 294 | char ** sequences; 295 | sequences = (char **) malloc(3*sizeof(char *)); 296 | for(int i = 0; i < 3; i++) 297 | sequences[i] = (char *) malloc(5*sizeof(char)); 298 | 299 | //Sequence 0 300 | sequences[0][0] = 'a'; 301 | sequences[0][1] = 'b'; 302 | sequences[0][2] = 'b'; 303 | sequences[0][3] = 'b'; 304 | sequences[0][4] = '\0'; 305 | //Sequence 1 306 | sequences[1][0] = 'b'; 307 | sequences[1][1] = 'a'; 308 | sequences[1][2] = 'b'; 309 | sequences[1][3] = 'a'; 310 | sequences[1][4] = '\0'; 311 | //Sequence 2 312 | sequences[2][0] = 'a'; 313 | sequences[2][1] = 'a'; 314 | sequences[2][2] = 'b'; 315 | sequences[2][3] = 'a'; 316 | sequences[2][4] = '\0'; 317 | 318 | char elements [] = {'a', 'b'}; 319 | 320 | double* probsA = chain.getNextTransitions('a', elements, 2, sequences, 3); 321 | if(probsA == NULL) 322 | Serial.println("TEST FAILED: element 'a' does no exits"); 323 | 324 | Serial.print("** Expected value: "); 325 | Serial.print(1.0/4.0); 326 | Serial.print(" actual: "); 327 | Serial.print(probsA[0]); 328 | Serial.println(); 329 | Serial.print("** Expected value: "); 330 | Serial.print(3.0/4.0); 331 | Serial.print(" actual: "); 332 | Serial.print(probsA[1]); 333 | Serial.println(); 334 | 335 | for(int i = 0; i < 3; i++) 336 | free(sequences[i]); 337 | free(sequences); 338 | 339 | free(probsA); 340 | } 341 | 342 | void testGetSequenceProbability(){ 343 | Serial.println(); 344 | Serial.println("*** TEST: testGetSequenceProbability ***"); 345 | 346 | char ** sequences; 347 | sequences = (char **) malloc(3*sizeof(char *)); 348 | for(int i = 0; i < 3; i++) 349 | sequences[i] = (char *) malloc(5*sizeof(char)); 350 | 351 | //Sequence 0 352 | sequences[0][0] = 'a'; 353 | sequences[0][1] = 'b'; 354 | sequences[0][2] = 'b'; 355 | sequences[0][3] = 'b'; 356 | sequences[0][4] = '\0'; 357 | //Sequence 1 358 | sequences[1][0] = 'b'; 359 | sequences[1][1] = 'a'; 360 | sequences[1][2] = 'b'; 361 | sequences[1][3] = 'a'; 362 | sequences[1][4] = '\0'; 363 | //Sequence 2 364 | sequences[2][0] = 'a'; 365 | sequences[2][1] = 'a'; 366 | sequences[2][2] = 'b'; 367 | sequences[2][3] = 'a'; 368 | sequences[2][4] = '\0'; 369 | 370 | char elements [] = {'a', 'b'}; 371 | 372 | char sequence [] = {'a','b','a'}; 373 | 374 | double prob = chain.getSequenceProbability(sequence, 3, elements, 2, sequences, 3); 375 | 376 | Serial.print("** Expected value: "); 377 | Serial.print((2.0/3.0) * (3.0/4.0) * (3.0/5.0)); 378 | Serial.print(" actual: "); 379 | Serial.print(prob); 380 | Serial.println(); 381 | 382 | for(int i = 0; i < 3; i++) 383 | free(sequences[i]); 384 | free(sequences); 385 | 386 | } 387 | 388 | 389 | void doTests(){ 390 | testGetFirstStates(); 391 | testCounterTransitionsInSequence(); 392 | testCountElementsInSequence(); 393 | testCreateTransitionMatrix(); 394 | testCountRowsTotals(); 395 | testCreateTransitionProbabilityMatrix(); 396 | testCalculateFirstStatesProbabilities(); 397 | testGetNextTransitions(); 398 | testGetSequenceProbability(); 399 | } 400 | 401 | void setup() { 402 | // put your setup code here, to run once: 403 | Serial.begin(9600); 404 | doTests(); 405 | } 406 | 407 | void loop() { 408 | } 409 | --------------------------------------------------------------------------------