Exemple #1
0
            public static double Corr(Vector bfactor1, Vector bfactor2, bool ignore_nan = false)
            {
                double hcorr = HMath.HCorr(bfactor1, bfactor2, ignore_nan);

                if (HDebug.IsDebuggerAttached)
                {
                    double corr = double.NaN;
                    using (new Matlab.NamedLock("CORR"))
                    {
                        Matlab.Clear("CORR");
                        Matlab.PutVector("CORR.bfactor1", bfactor1);
                        Matlab.PutVector("CORR.bfactor2", bfactor2);
                        if (ignore_nan)
                        {
                            Matlab.Execute("CORR.idxnan = isnan(CORR.bfactor1) | isnan(CORR.bfactor2);");
                            Matlab.Execute("CORR.bfactor1 = CORR.bfactor1(~CORR.idxnan);");
                            Matlab.Execute("CORR.bfactor2 = CORR.bfactor2(~CORR.idxnan);");
                        }
                        if (Matlab.GetValueInt("min(size(CORR.bfactor1))") != 0)
                        {
                            corr = Matlab.GetValue("corr(CORR.bfactor1, CORR.bfactor2)");
                        }
                        Matlab.Clear("CORR");
                    }
                    if ((double.IsNaN(hcorr) && double.IsNaN(corr)) == false)
                    {
                        HDebug.AssertTolerance(0.00000001, hcorr - corr);
                    }
                    //HDebug.ToDo("use HMath.HCorr(...) instead");
                }
                return(hcorr);
            }
            public bool CheckModeWithHess(MatrixByArr hess, double[] masses, ILinAlg la, Mode[] modes = null, double tolerance = 0.00001)
            {
                if (modes == null)
                {
                    modes = ToModes(masses);
                }

                HessMatrix ohess = new HessMatrixDense
                {
                    hess = modes.GetHessian(masses, la),
                };
                double corr = HMath.HCorr(hess.ToArray().HToArray1D(), ohess.ToArray().HToArray1D());

                System.Console.Write("corr(TestHess,HessVibr:{0}  ", corr);

                {
                    HDebug.Assert(hess.ColSize == ohess.ColSize);
                    HDebug.Assert(hess.RowSize == ohess.RowSize);
                    double ltolerance = hess.ToArray().HAbs().HMax() * tolerance;
                    ltolerance = tolerance;
                    for (int c = 0; c < ohess.ColSize; c++)
                    {
                        for (int r = 0; r < ohess.RowSize; r++)
                        {
                            double v    = hess[c, r];
                            double mwv  = ohess[c, r];
                            double diff = Math.Abs(v - mwv);
                            if (diff >= ltolerance)
                            {
                                return(false);
                            }
                        }
                    }
                }
                //{
                //    Matrix vhess  =  hess /  hess.ToArray().HToArray1D().HVar();
                //    Matrix vohess = ohess / ohess.ToArray().HToArray1D().HVar();
                //
                //    HDebug.Assert(hess.ColSize == ohess.ColSize);
                //    HDebug.Assert(hess.RowSize == ohess.RowSize);
                //    HDebug.Assert(vhess.Size == vohess.Size);
                //    for(int c=0; c<vhess.ColSize; c++)
                //    for(int r=0; r<vhess.RowSize; r++)
                //    {
                //        double v = vhess[c,r];
                //        double vo = vohess[c,r];
                //        double diff = Math.Abs(v - vo);
                //        if(diff >= tolerance)
                //        {
                //            HDebug.Assert(false);
                //            return false;
                //        }
                //    }
                //}
                return(true);
            }
            public static double[,] ModeContribToBFactorCorr(IList <Mode> modes1, IList <Mode> modes2)
            {
                /// Similar to "double[] ModeContribToBFactorCorr(bfactor1, modes2)",
                /// the correlation between bfactor1 and bfactor 2 can be decomposed as
                /// the list of correlation contributions by "normalized bfactor by modes1.m_i"
                ///                                      and "normalized bfactor by modes2.m_j"
                /// in a matrix form.
                ///
                /// therefore, the correlation of bfactor1 and bfactor(modes2) is determined as their normalized dot-product:
                ///     corr = dot(nbfactor1, nbfactor2) / (n-1)
                ///          = 1/(n-1) *   dot(nbfactor1, nmwbfactor(modes2.m1)                 + nmwbfactor(modes2.m2)  + ... )
                ///          = 1/(n-1) * ( dot(nbfactor1, nmwbfactor(modes2.m1)) + dot(nbfactor1, nmwbfactor(modes2.m2)) + ... )
                ///          = 1/(n-1) * ( dot(nmwbfactor(modes1.m1), nmwbfactor(modes2.m1)) + dot(nmwbfactor(modes1.m1), nmwbfactor(modes2.m2)) + ...
                ///                        dot(nmwbfactor(modes1.m2), nmwbfactor(modes2.m1)) + dot(nmwbfactor(modes1.m2), nmwbfactor(modes2.m2)) + ... )
                ///          = 1/(n-1) * sum_{ i1=1..m1, i2=1..m2 } dot(nmwbfactor(modes1.m_i), nmwbfactor(modes2.m_j))
                ///          = sum_{ i1=1..m1, i2=1..m2 } dot(nmwbfactor(modes1.m_i), nmwbfactor(modes2.m_j))/(n-1)
                ///          = sum_{ i1=1..m1, i2=1..m2 } "correlation contribution by modes1.m_i and modes2.m_j"
                ///
                Vector[] nbfactor1mw = GetBFactorModewiseNormalized(modes1);
                Vector[] nbfactor2mw = GetBFactorModewiseNormalized(modes2);

                int n  = nbfactor1mw[0].Size;
                int m1 = modes1.Count; HDebug.Assert(nbfactor1mw.Length == m1);
                int m2 = modes2.Count; HDebug.Assert(nbfactor2mw.Length == m2);

                MatrixByArr contrib = new double[m1, m2];

                for (int i1 = 0; i1 < m1; i1++)
                {
                    for (int i2 = 0; i2 < m2; i2++)
                    {
                        HDebug.Assert(nbfactor1mw[i1].Size == n);
                        HDebug.Assert(nbfactor2mw[i2].Size == n);
                        contrib[i1, i2] = LinAlg.VtV(nbfactor1mw[i1], nbfactor2mw[i2]) / (n - 1);
                    }
                }

                if (HDebug.IsDebuggerAttached)
                {
                    Vector bfactor1  = modes1.GetBFactor().ToArray();
                    Vector nbfactor1 = GetBFactorNormalized(bfactor1);
                    Vector bfactor2  = modes2.GetBFactor().ToArray();
                    Vector nbfactor2 = GetBFactorNormalized(bfactor2);
                    double corr0     = HMath.HCorr(bfactor1, bfactor2);
                    double corr1     = LinAlg.VtV(nbfactor1, nbfactor2) / (n - 1);
                    double corr2     = contrib.ToArray().HSum();
                    HDebug.AssertTolerance(0.00000001, corr0 - corr1, corr0 - corr2, corr1 - corr2);
                }

                return(contrib);
            }
            public static double[] ModeContribToBFactorCorr(Vector bfactor1, IList <Mode> modes2)
            {
                /// the correlation(corr) of two bfactors (bfactor1, bfactor 2) is the same to
                /// the dot product of their normalizeds (mean 0, variance 1):
                ///   corr = Math.HCorr( bfactor1,  bfactor2)
                ///        = Math.HCorr(nbfactor1, nbfactor2)
                ///        = LinAlg.VtV(nbfactor1, nbfactor2)            (for   biased estimation)
                ///        = LinAlg.VtV(nbfactor1, nbfactor2) / (n-1)    (for unbiased estimation)
                ///
                /// bfactor of mode(m1,m2,...) can be determined by the sum of bfactors of each mode:
                ///     bfactor(mode) = bfactor(m1) + bfactor(m2) + ...
                ///
                /// in the similar manner, the normalized bfactor of mode(m1,m2,...) is determined by
                /// sum of normalized-modewise-bfactor (GetBFactorModewiseNormalized):
                ///     normal-bfactor(mode) = nmwbfactor(m1) + nmwbfactor(m2) + ...
                ///
                /// therefore, the correlation of bfactor1 and bfactor(modes2) is determined as their normalized dot-product:
                ///     corr = dot(nbfactor1, nbfactor2)                                                                           / (n-1)
                ///          = dot(nbfactor1, nmwbfactor(modes2.m1)        + nmwbfactor(modes2.m2)                         + ... ) / (n-1)
                ///          = dot(nbfactor1, nmwbfactor(modes2.m1))/(n-1) + dot(nbfactor1, nmwbfactor(modes2.m2)) / (n-1) + ...
                ///
                Vector nbfactor1 = GetBFactorNormalized(bfactor1);

                Vector[] nbfactor2mw = GetBFactorModewiseNormalized(modes2);

                int n = bfactor1.Size;
                int m = modes2.Count;

                HDebug.Assert(nbfactor2mw.Length == m);

                Vector contrib = new double[m];

                for (int i = 0; i < m; i++)
                {
                    contrib[i] = LinAlg.VtV(nbfactor1, nbfactor2mw[i]) / (n - 1);
                }

                if (HDebug.IsDebuggerAttached)
                {
                    Vector bfactor2  = modes2.GetBFactor().ToArray();
                    Vector nbfactor2 = GetBFactorNormalized(bfactor2);
                    double corr0     = HMath.HCorr(bfactor1, bfactor2);
                    double corr1     = LinAlg.VtV(nbfactor1, nbfactor2) / (n - 1);
                    double corr2     = contrib.Sum();
                    HDebug.AssertTolerance(0.00000001, corr0 - corr1, corr0 - corr2, corr1 - corr2);
                }

                return(contrib);
            }