public void MDSReturnCorrectFourProfilesTimesFourProfilesMatrixItShould() { double[,] mdsInput = { { 0, 0.5946, 0.6571, 0.5517 }, { 0.5946, 0, 0.6486, 0.7222 }, { 0.6571, 0.6486, 0, 0.6452 }, { 0.5517, 0.7222, 0.6452, 0 } }; Matrix mdsInputMatrix = new Matrix(mdsInput); double[,] expectedRes = { { -0.084, 0.36, 0.073, -0.349 }, { -0.227, -0.148, 0.389, -0.016 } }; Matrix expectedResMatrix = new Matrix(expectedRes); MultiDimensionalScaling mdsResult = new MultiDimensionalScaling(mdsInputMatrix); Matrix givenMDSResult = mdsResult.Calculate(); double difference; for (int row = 0; row < expectedRes.GetLength(0); row++) { for (int col = 0; col < expectedRes.GetLength(1); col++) { difference = expectedRes[row, col] - givenMDSResult[row, col]; if (!(difference < 0.01 && difference > -0.01)) { Assert.Fail("{0}, row = {1}, col = {2}", difference, row, col); } } } }
public void MDSReturnCorrectFourTimesFourMatrixItShould() { double[,] mdsInput = { { 0, 93, 82, 133 }, { 93, 0, 52, 60 }, { 82, 52, 0, 111 }, { 133, 60, 111, 0 } }; Matrix mdsInputMatrix = new Matrix(mdsInput); double[,] expectedRes = { { -62.815, 18.44, -24.948, 69.422 }, { -32.947, 12.032, 39.693, -18.778 } }; Matrix expectedResMatrix = new Matrix(expectedRes); MultiDimensionalScaling mdsResult = new MultiDimensionalScaling(mdsInputMatrix); Matrix givenMDSResult = mdsResult.Calculate(); double difference; for (int row = 0; row < expectedRes.GetLength(0); row++) { for (int col = 0; col < expectedRes.GetLength(1); col++) { difference = expectedRes[row, col] - givenMDSResult[row, col]; if (!(difference < 0.1 && difference > -0.1)) { Assert.Fail("{0}, row = {1}, col = {2}", difference, row, col); } } } }
public void MDSReturnCorrectFiveTimesFiveMatrixItShould() { double[,] mdsInput = { { 0, 87, 284, 259, 259 }, { 87, 0, 195, 183, 222 }, { 284, 195, 0, 123, 260 }, { 259, 183, 123, 0, 140 }, { 259, 222, 260, 140, 0 } }; Matrix mdsInputMatrix = new Matrix(mdsInput); double[,] expectedRes = { { 163.278, 82.341, -95.441, -95.675, -54.504 }, { 1.044, -34.181, -115.548, 7.617, 141.184 } }; Matrix expectedResMatrix = new Matrix(expectedRes); MultiDimensionalScaling mdsResult = new MultiDimensionalScaling(mdsInputMatrix); Matrix givenMDSResult = mdsResult.Calculate(); double difference; for (int row = 0; row < expectedRes.GetLength(0); row++) { for (int col = 0; col < expectedRes.GetLength(1); col++) { difference = expectedRes[row, col] - givenMDSResult[row, col]; if (!(difference < 0.1 && difference > -0.1)) { Assert.Fail("{0}, row = {1}, col = {2}", difference, row, col); } } } }
static void TestMDS() { double[,] distanceTestOne = { { 0, 87.0, 284.0, 259, 259 }, { 87.0, 0, 195, 183, 222 }, { 284, 195, 0, 123, 260 }, { 259, 183, 123, 0, 140 }, { 259, 222, 260, 140, 0 } }; Matrix distanceTestOneMatrix = new Matrix(distanceTestOne); MultiDimensionalScaling a = new MultiDimensionalScaling(distanceTestOneMatrix); a.Calculate(); double[,] distanceTestTwo = { { 0, 93.0, 82.0, 133 }, { 93.0, 0, 52, 60 }, { 82, 52, 0, 111 }, { 133, 60, 111, 0 } }; Matrix distanceTestTwoMatrix = new Matrix(distanceTestTwo); MultiDimensionalScaling b = new MultiDimensionalScaling(distanceTestTwoMatrix); b.Calculate(); }
static void MultiDimensionalScaling(StreamWriter writer, IdentifiableDataPointCollection dataCollection, IDistanceMetric distanceMetric) { writer.WriteLine("MDS coordinates"); DistanceMatrix distanceMatrix = new DistanceMatrix(dataCollection, distanceMetric); var mds = new MultiDimensionalScaling(distanceMatrix); Matrix resultMatrix = mds.Calculate(); //a shitty name int limiter = 20; if (limiter > resultMatrix.Columns) { limiter = resultMatrix.Columns; } char letter = 'A'; //Print file index writer.Write(" "); for (int columnIndex = 0; columnIndex < limiter; columnIndex++, letter++) { if (letter >= 'z') { letter = 'A'; } writer.Write(" {0} |", letter); } writer.WriteLine(); //Print the coordinates letter = 'X'; for (int rowIndex = 0; rowIndex < 2; rowIndex++) { writer.Write(" {0} |", letter++); for (int columnIndex = 0; columnIndex < limiter; columnIndex++) { writer.Write("{0,5:N2}|", resultMatrix[rowIndex, columnIndex]); } writer.Write("\r\n"); } writer.WriteLine("\r\n"); }