Exemple #1
0
        public void CombineTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var result = ica.Result;

            var expected = Accord.Statistics.Tools.ZScores(X);
            var actual   = Accord.Statistics.Tools.ZScores(ica.Combine(result));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4));
        }
Exemple #2
0
        public void CombineTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);

            ica.Compute(2);

            double[,] result = ica.Result;


            float[][] expected = ica.Combine(result).ToSingle().ToArray(true);
            float[][] actual   = ica.Combine(result.ToSingle().ToArray(true));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4f));
        }
Exemple #3
0
        public void CombineTest2()
        {
            Accord.Math.Random.Generator.Seed = 0;

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = Matrix.Multiply(S, A);

            var ica = new IndependentComponentAnalysis(X);

            ica.Compute(2);

            double[,] result = ica.Result;


            float[][] expected = ica.Combine(result).ToSingle().ToJagged(true);
            float[][] actual   = ica.Combine(result.ToSingle().ToJagged(true));

            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-4f));
        }
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Let's create a random dataset containing
            // 5000 samples of two dimensional samples.
            //
            double[,] source = Matrix.Random(5000, 2);

            // Now, we will mix the samples the dimensions of the samples.
            // A small amount of the second column will be applied to the
            // first, and vice-versa. 
            //
            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            // mix the source data
            double[,] input = source.Multiply(mix);

            // Now, we can use ICA to identify any linear mixing between the variables, such
            // as the matrix multiplication we did above. After it has identified it, we will
            // be able to revert the process, retrieving our original samples again
            
            // Create a new Independent Component Analysis
            var ica = new IndependentComponentAnalysis(input);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            // Compute it 
            ica.Compute();

            // Now, we can retrieve the mixing and demixing matrices that were 
            // used to alter the data. Note that the analysis was able to detect
            // this information automatically:

            double[,] mixingMatrix = ica.MixingMatrix; // same as the 'mix' matrix
            double[,] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },        
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
Exemple #5
0
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Let's create a random dataset containing
            // 5000 samples of two dimensional samples.
            //
            double[,] source = Matrix.Random(5000, 2);

            // Now, we will mix the samples the dimensions of the samples.
            // A small amount of the second column will be applied to the
            // first, and vice-versa.
            //
            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            // mix the source data
            double[,] input = source.Multiply(mix);

            // Now, we can use ICA to identify any linear mixing between the variables, such
            // as the matrix multiplication we did above. After it has identified it, we will
            // be able to revert the process, retrieving our original samples again

            // Create a new Independent Component Analysis
            var ica = new IndependentComponentAnalysis(input);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            // Compute it
            ica.Compute();

            // Now, we can retrieve the mixing and demixing matrices that were
            // used to alter the data. Note that the analysis was able to detect
            // this information automatically:

            double[,] mixingMatrix = ica.MixingMatrix;   // same as the 'mix' matrix
            double[,] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
Exemple #6
0
        public void SerializeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] source = Matrix.Random(5000, 2);

            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] input = source.Dot(mix);

            var ica = new IndependentComponentAnalysis(input);

            ica.Compute();

            MemoryStream stream = new MemoryStream();

            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(stream, ica);
            }

            stream.Seek(0, SeekOrigin.Begin);

            {
                BinaryFormatter b = new BinaryFormatter();
                ica = (IndependentComponentAnalysis)b.Deserialize(stream);
            }


            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            double[][] mixingMatrix = ica.MixingMatrix;   // same as the 'mix' matrix
            double[][] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, atol: 0.008));

            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, atol: 0.008));
        }
Exemple #7
0
        public void ComputeTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  1, 1 },
                { -1, 3 },
            };

            A = A.Divide(Norm.Norm1(A));

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Deflation);

            Assert.AreEqual(IndependentComponentAlgorithm.Deflation, ica.Algorithm);

            ica.Compute(2);

            var result       = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(Norm.Norm1(mixingMatrix));
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.05));


            // Verify demixing matrix
            double[,] expected =
            {
                { 3, -1 },
                { 1,  1 },
            };

            expected = expected.Divide(Norm.Norm1(expected));

            revertMatrix = revertMatrix.Divide(Norm.Norm1(revertMatrix));
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.05));



            var reverted = Accord.Statistics.Tools.ZScores(result).Abs();
            var original = Accord.Statistics.Tools.ZScores(S).Abs();

            Assert.IsTrue(reverted.IsEqual(original, 0.1));
        }
Exemple #8
0
        public void ConvergenceTest()
        {
            IndependentComponentAnalysis ica;

            // https://github.com/accord-net/framework/issues/225
            var mixedData = LoadData();

            ica            = new IndependentComponentAnalysis(mixedData, AnalysisMethod.Standardize);
            ica.Overwrite  = false;
            ica.Iterations = 1000;
            ica.Algorithm  = IndependentComponentAlgorithm.Parallel;
            ica.Contrast   = new Kurtosis();

            ica.Compute();

            Assert.AreEqual(3.2178976535060348, ica.WhiteningMatrix.Sum());
            Assert.AreEqual(1, ica.MixingMatrix.Sum(), 1e-7);
        }
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Parallel);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            ica.Compute(2);

            var result = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },        
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
        public void ComputeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Parallel);

            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            ica.Compute(2);

            var result       = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.008));


            // Verify demixing matrix
            double[,] expected =
            {
                { 0.75, -0.25 },
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));
        }
Exemple #11
0
        public void SeparateTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = Matrix.Multiply(S, A);

            var ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var expected = ica.Result;
            var actual   = ica.Separate(X);

            Assert.IsTrue(expected.IsEqual(actual, 1e-4));
        }
Exemple #12
0
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            // Retrieve the input data as a double[,] matrix
            double[,] data = input.ToMatrix(true).ToDouble();


            // Create a new Independent Component Analysis
            ica = new IndependentComponentAnalysis(data,
                                                   AnalysisMethod.Standardize)
            {
                Overwrite = true
            };

            // Compute the analysis
            ica.Compute();


            // Separate the input signals
            demix = ica.Separate(input);


            btnSource1.Enabled = true;
            btnSource2.Enabled = true;
        }
Exemple #13
0
        public void SeparateTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },
            };

            double[,] X = S.Dot(A);

            var ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var expected = ica.Result.ToSingle().ToJagged(true);
            var actual   = ica.Separate(X.ToSingle().ToJagged(true));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4f));
        }
        public void CombineTest2()
        {
            Accord.Math.Random.Generator.Seed = 0;

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = Matrix.Multiply(S, A);

            var ica = new IndependentComponentAnalysis(X);

            ica.Compute(2);

            double[,] result = ica.Result;


            float[][] expected = ica.Combine(result).ToSingle().ToJagged(true);
            float[][] actual = ica.Combine(result.ToSingle().ToJagged(true));

            Assert.IsTrue(expected.IsEqual(actual, atol: 1e-4f));
        }
        public void SerializeTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] source = Matrix.Random(5000, 2);

            double[,] mix =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] input = source.Multiply(mix);

            var ica = new IndependentComponentAnalysis(input);

            ica.Compute();

            MemoryStream stream = new MemoryStream();

            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(stream, ica);
            }

            stream.Seek(0, SeekOrigin.Begin);

            {
                BinaryFormatter b = new BinaryFormatter();
                ica = (IndependentComponentAnalysis)b.Deserialize(stream);
            }


            Assert.AreEqual(IndependentComponentAlgorithm.Parallel, ica.Algorithm);

            double[,] mixingMatrix = ica.MixingMatrix; // same as the 'mix' matrix
            double[,] revertMatrix = ica.DemixingMatrix; // inverse of the 'mix' matrix

            double[,] result = ica.Result;

            mixingMatrix = mixingMatrix.Divide(mixingMatrix.Sum().Sum());
            Assert.IsTrue(mix.IsEqual(mixingMatrix, 0.008));

            double[,] expected =
            {
                { 0.75, -0.25 },        
                { 0.25,  0.25 },
            };

            revertMatrix = revertMatrix.Divide(revertMatrix.Sum().Sum());
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.008));


        }
        public void CombineTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var result = ica.Result;

            var expected = Accord.Statistics.Tools.ZScores(X);
            var actual = Accord.Statistics.Tools.ZScores(ica.Combine(result));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4));
        }
        public void SeparateTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  0.25, 0.25 },
                { -0.25, 0.75 },    
            };

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X);


            ica.Compute(2);

            var expected = ica.Result.ToSingle().ToArray(true);
            var actual = ica.Separate(X.ToSingle().ToArray(true));

            Assert.IsTrue(expected.IsEqual(actual, 1e-4f));
        }
        public void ComputeTest2()
        {
            Accord.Math.Tools.SetupGenerator(0);

            double[,] S = Matrix.Random(5000, 2);

            double[,] A =
            {
                {  1, 1 },
                { -1, 3 },    
            };

            A = A.Divide(Norm.Norm1(A));

            double[,] X = S.Multiply(A);

            IndependentComponentAnalysis ica = new IndependentComponentAnalysis(X, IndependentComponentAlgorithm.Deflation);

            Assert.AreEqual(IndependentComponentAlgorithm.Deflation, ica.Algorithm);

            ica.Compute(2);

            var result = ica.Result;
            var mixingMatrix = ica.MixingMatrix;
            var revertMatrix = ica.DemixingMatrix;

            // Verify mixing matrix
            mixingMatrix = mixingMatrix.Divide(Norm.Norm1(mixingMatrix));
            Assert.IsTrue(A.IsEqual(mixingMatrix, 0.05));


            // Verify demixing matrix
            double[,] expected =
            {
                { 3, -1 },        
                { 1,  1 },
            };

            expected = expected.Divide(Norm.Norm1(expected));

            revertMatrix = revertMatrix.Divide(Norm.Norm1(revertMatrix));
            Assert.IsTrue(expected.IsEqual(revertMatrix, 0.05));



            var reverted = Accord.Statistics.Tools.ZScores(result).Abs();
            var original = Accord.Statistics.Tools.ZScores(S).Abs();

            Assert.IsTrue(reverted.IsEqual(original, 0.1));
        }
        private void btnRunAnalysis_Click(object sender, EventArgs e)
        {
            // Retrieve the input data as a double[,] matrix
            double[,] data = input.ToMatrix(true).ToDouble();


            // Create a new Independent Component Analysis
            ica = new IndependentComponentAnalysis(data,
                AnalysisMethod.Standardize) { Overwrite = true };

            // Compute the analysis
            ica.Compute();


            // Separate the input signals
            demix = ica.Separate(input);


            btnSource1.Enabled = true;
            btnSource2.Enabled = true;
        }
        public void ConvergenceTest()
        {
            IndependentComponentAnalysis ica;

            // https://github.com/accord-net/framework/issues/225
            var mixedData = LoadData();

            ica = new IndependentComponentAnalysis(mixedData, AnalysisMethod.Standardize);
            ica.Overwrite = false;
            ica.Iterations = 1000;
            ica.Algorithm = IndependentComponentAlgorithm.Parallel;
            ica.Contrast = new Kurtosis();

            ica.Compute();

            Assert.AreEqual(3.2178976535060348, ica.WhiteningMatrix.Sum());
            Assert.AreEqual(1, ica.MixingMatrix.Sum(), 1e-7);
        }