public void TestOverlapPattern() { SetUp(); Classification <double> result = Compute <double>(_classifier, 0, new[] { 1, 5 }, 9, 9); result = Compute <double>(_classifier, 1, new[] { 1, 5 }, 9, 9); result = Compute <double>(_classifier, 1, new[] { 1, 5 }, 9, 9); result = Compute <double>(_classifier, 2, new[] { 3, 5 }, 2, 2); // Since overlap - should be previous with 100% CheckValue(result, 9, 9.0, 1.0); result = Compute <double>(_classifier, 3, new[] { 3, 5 }, 2, 2); // Second example: now new value should be more probable than old Assert.IsTrue(result.GetStat(1, 2) > result.GetStat(1, 9)); }
public void TestMultistepSimple() { _classifier = new CLAClassifier(new[] { 1, 2 }, 0.001, 0.3, 0); Classification <double> result = null; int recordNum = 0; for (int i = 0; i < 100; i++, recordNum++) { result = Compute <double>(_classifier, recordNum, new[] { i % 10 }, i % 10, (i % 10) * 10); } // Only should return one actual value bucket. Assert.IsTrue(Arrays.AreEqual(new double[] { 0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0 }, result.GetActualValues())); Assert.AreEqual(1.0, result.GetStat(1, 0), 0.1); for (int i = 1; i < 10; i++) { Assert.AreEqual(0.0, result.GetStat(1, i), 0.1); } Assert.AreEqual(1.0, result.GetStat(2, 1), 0.1); }
public void TestSingleValue0Steps() { var classifier = new SDRClassifier(new[] { 0 }, 1.0); // Enough times to perform Inference and learn associations Classification <double> retVal = null; for (int i = 0; i < 10; i++) { retVal = _compute(classifier, i, new[] { 1, 5 }, 0, 10); } Assert.AreEqual(retVal.GetActualValue(0), 10); Assert.IsTrue(retVal.GetStat(0, 0) > 0.9, "value of 1 must be greater then 0.9 and is " + retVal.GetStat(0, 0)); }
public void TestSingleValue0Steps() { _classifier = new CLAClassifier(new[] { 0 }, 0.001, 0.3, 0); // Enough times to perform Inference and learn associations Classification <double> retVal = null; for (int recordNum = 0; recordNum < 10; recordNum++) { retVal = Compute <double>(_classifier, recordNum, new[] { 1, 5 }, 0, 10); } Assert.AreEqual(10.0, retVal.GetActualValue(0), .00001); Assert.AreEqual(1.0, retVal.GetStat(0, 0), .00001); }
public void TestSerializeCLAClassifier() { CLAClassifier classifier = new CLAClassifier(new int[] { 1 }, 0.1, 0.1, 0); int recordNum = 0; Map <String, Object> classification = new Map <string, object>(); classification.Add("bucketIdx", 4); classification.Add("actValue", 34.7); Classification <double> result = classifier.Compute <double>(recordNum, classification, new int[] { 1, 5, 9 }, true, true); recordNum += 1; classification.Add("bucketIdx", 5); classification.Add("actValue", 41.7); result = classifier.Compute <double>(recordNum, classification, new int[] { 0, 6, 9, 11 }, true, true); recordNum += 1; classification.Add("bucketIdx", 5); classification.Add("actValue", 44.9); result = classifier.Compute <double>(recordNum, classification, new int[] { 6, 9 }, true, true); recordNum += 1; classification.Add("bucketIdx", 4); classification.Add("actValue", 42.9); result = classifier.Compute <double>(recordNum, classification, new int[] { 1, 5, 9 }, true, true); recordNum += 1; // Serialize the Metrics too just to be sure everything can be serialized SerialConfig config = new SerialConfig("testSerializeCLAClassifier", SerialConfig.SERIAL_TEST_DIR); IPersistenceAPI api = Persistence.Get(config); api.Write(classifier); // Deserialize the Metrics CLAClassifier serializedClassifier = api.Read <CLAClassifier>(); Assert.IsNotNull(serializedClassifier); //Using the deserialized classifier, continue test classification.Add("bucketIdx", 4); classification.Add("actValue", 34.7); result = serializedClassifier.Compute <double>(recordNum, classification, new int[] { 1, 5, 9 }, true, true); recordNum += 1; Assert.IsTrue(Arrays.AreEqual(new int[] { 1 }, result.StepSet())); Assert.AreEqual(35.520000457763672, result.GetActualValue(4), 0.00001); Assert.AreEqual(42.020000457763672, result.GetActualValue(5), 0.00001); Assert.AreEqual(6, result.GetStatCount(1)); Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 3), 0.00001); Assert.AreEqual(0.12300123, result.GetStat(1, 4), 0.00001); Assert.AreEqual(0.87699877, result.GetStat(1, 5), 0.00001); }
public void TestSerialization() { _classifier = new CLAClassifier(new[] { 1 }, 0.1, 0.1, 0); int recordNum = 0; Map <string, object> classification = new Map <string, object>(); classification.Add("bucketIdx", 4); classification.Add("actValue", 34.7); Classification <double> result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true); recordNum += 1; classification.Add("bucketIdx", 5); classification.Add("actValue", 41.7); result = _classifier.Compute <double>(recordNum, classification, new[] { 0, 6, 9, 11 }, true, true); recordNum += 1; classification.Add("bucketIdx", 5); classification.Add("actValue", 44.9); result = _classifier.Compute <double>(recordNum, classification, new[] { 6, 9 }, true, true); recordNum += 1; classification.Add("bucketIdx", 4); classification.Add("actValue", 42.9); result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true); recordNum += 1; // Configure serializer SerialConfig config = new SerialConfig("testSerializerClassifier", SerialConfig.SERIAL_TEST_DIR); IPersistenceAPI api = Persistence.Get(config); // 1. Serialize byte[] data = api.Write(_classifier, "testSerializeClassifier"); // 2. Deserialize CLAClassifier serialized = api.Read <CLAClassifier>(data); // Using the deserialized classifier, continue test classification.Add("bucketIdx", 4); classification.Add("actValue", 34.7); result = serialized.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true); recordNum += 1; Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, result.StepSet())); Assert.AreEqual(35.520000457763672, result.GetActualValue(4), 0.00001); Assert.AreEqual(42.020000457763672, result.GetActualValue(5), 0.00001); Assert.AreEqual(6, result.GetStatCount(1)); Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 3), 0.00001); Assert.AreEqual(0.12300123, result.GetStat(1, 4), 0.00001); Assert.AreEqual(0.87699877, result.GetStat(1, 5), 0.00001); }
public void TestComputeComplex() { _classifier = new CLAClassifier(new[] { 1 }, 0.1, 0.1, 0); int recordNum = 0; Map <string, object> classification = new Map <string, object>(); classification.Add("bucketIdx", 4); classification.Add("actValue", 34.7); Classification <double> result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true); recordNum += 1; classification.Add("bucketIdx", 5); classification.Add("actValue", 41.7); result = _classifier.Compute <double>(recordNum, classification, new[] { 0, 6, 9, 11 }, true, true); recordNum += 1; classification.Add("bucketIdx", 5); classification.Add("actValue", 44.9); result = _classifier.Compute <double>(recordNum, classification, new[] { 6, 9 }, true, true); recordNum += 1; classification.Add("bucketIdx", 4); classification.Add("actValue", 42.9); result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true); recordNum += 1; classification.Add("bucketIdx", 4); classification.Add("actValue", 34.7); result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 5, 9 }, true, true); recordNum += 1; Assert.IsTrue(Arrays.AreEqual(new[] { 1 }, result.StepSet())); Assert.AreEqual(35.520000457763672, result.GetActualValue(4), 0.00001); Assert.AreEqual(42.020000457763672, result.GetActualValue(5), 0.00001); Assert.AreEqual(6, result.GetStatCount(1)); Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 3), 0.00001); Assert.AreEqual(0.12300123, result.GetStat(1, 4), 0.00001); Assert.AreEqual(0.87699877, result.GetStat(1, 5), 0.00001); }
public void CheckValue <T>(Classification <T> retVal, int index, object value, double probability) { Assert.AreEqual(retVal.GetActualValue(index), value); Assert.AreEqual(probability, retVal.GetStat(1, index), 0.01); }
public void TestMissingRecords() { _classifier = new CLAClassifier(new[] { 1 }, 0.1, 0.1, 0); int recordNum = 0; Map <string, object> classification = new Map <string, object>(); classification.Add("bucketIdx", 0); classification.Add("actValue", 0); _classifier.Compute <double>(recordNum, classification, new[] { 1, 3, 5 }, true, true); recordNum += 1; classification.Add("bucketIdx", 1); classification.Add("actValue", 1); _classifier.Compute <double>(recordNum, classification, new[] { 2, 4, 6 }, true, true); recordNum += 1; classification.Add("bucketIdx", 2); classification.Add("actValue", 2); _classifier.Compute <double>(recordNum, classification, new[] { 1, 3, 5 }, true, true); recordNum += 1; classification.Add("bucketIdx", 1); classification.Add("actValue", 1); _classifier.Compute <double>(recordNum, classification, new[] { 2, 4, 6 }, true, true); recordNum += 1; // ---------------------------------------------------------------------------------- // At this point, we should have learned [1, 3, 5] => bucket 1 // [2, 4, 6] => bucket 2 classification.Add("bucketIdx", 2); classification.Add("actValue", 2); Classification <double> result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 3, 5 }, true, true); recordNum += 1; Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(1.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001); classification.Add("bucketIdx", 1); classification.Add("actValue", 1); result = _classifier.Compute <double>(recordNum, classification, new[] { 2, 4, 6 }, true, true); recordNum += 1; Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(1.0, result.GetStat(1, 2), 0.00001); // ---------------------------------------------------------------------------------- // Feed in records that skip and make sure they don't mess up what we learned // // If we skip a record, the CLA should NOT learn that [2,4,6] from // the previous learning associates with bucket 0 recordNum += 1; // <----- Does the skip classification.Add("bucketIdx", 0); classification.Add("actValue", 0); result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 3, 5 }, true, true); recordNum += 1; Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(1.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001); // If we skip a record, the CLA should NOT learn that [1,3,5] from // the previous learning associates with bucket 0 recordNum += 1; // <----- Does the skip classification.Add("bucketIdx", 0); classification.Add("actValue", 0); result = _classifier.Compute <double>(recordNum, classification, new[] { 2, 4, 6 }, true, true); recordNum += 1; Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(1.0, result.GetStat(1, 2), 0.00001); // If we skip a record, the CLA should NOT learn that [2,4,6] from // the previous learning associates with bucket 0 recordNum += 1; // <----- Does the skip classification.Add("bucketIdx", 0); classification.Add("actValue", 0); result = _classifier.Compute <double>(recordNum, classification, new[] { 1, 3, 5 }, true, true); recordNum += 1; Assert.AreEqual(0.0, result.GetStat(1, 0), 0.00001); Assert.AreEqual(1.0, result.GetStat(1, 1), 0.00001); Assert.AreEqual(0.0, result.GetStat(1, 2), 0.00001); }