繁体   English   中英

矩阵必须是正定的(Math.Net C# 库)

[英]Matrix must be positive definite (Math.Net C# library)

我在 C# 的 Math.Net Numerics 库中遇到了一个奇怪的问题。 直到最近,我的代码都运行良好(据我所知,没有任何变化),但我现在从标题中尝试计算多元回归的行中收到错误消息。

每个列表都有 493 个双精度值,所以有人知道我可以做些什么来解决这些问题吗?

Vector<double> vectorArrayBuy = CreateVector.Dense(listMRInfoBuy.ElementAt(0).OutputBuy.ToArray());

var matrixArrayBuy = CreateMatrix.DenseOfColumnArrays(listMRInfoBuy.ElementAt(0).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(1).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(2).ListValuesBuy.ToArray(),
                                listMRInfoBuy.ElementAt(3).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(4).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(5).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(6).ListValuesBuy.ToArray(),
                                listMRInfoBuy.ElementAt(7).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(8).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(9).ListValuesBuy.ToArray(), listMRInfoBuy.ElementAt(10).ListValuesBuy.ToArray(),
                                listMRInfoBuy.ElementAt(11).ListValuesBuy.ToArray());

var itemsBuy = MultipleRegression.NormalEquations(matrixArrayBuy, vectorArrayBuy);

“矩阵非正定”可能意味着您的独立方程少于 n 个,这反过来意味着您没有 n 个独立数据,这可能意味着您的数据在某些方面存在缺陷(例如,它们被错误地读取并且它们'实际上都是一样的或类似的东西)。

也许您可以编辑您的问题以显示您正在使用的数据是什么。 也许您开始时的数据少于 n 个。

只是为那些不记得你学生时代的线性代数或高级统计数据的人(像我一样)添加一个解决方案。

  1. 如果您还没有,请在 Excel 中应用“分析工具库”加载项
  2. 将您的自变量和因变量粘贴到工作表中
  3. 转到数据 -> 数据分析 -> 回归
  4. 提供它要求的范围并运行回归
  5. 在回归结果中,您会发现一个或多个 p 值或 t-stats 返回 div/0 或 NUM 错误。
  6. 从您对 MathNet 回归的调用中删除这些自变量并再次运行。

这应该解决它。

然后我继续添加一个迭代 try 和 catch,它会根据特定情况删除自变量,然后再次运行它。

IHTH

我通过动态切换不同方程来解决这个问题,看看哪个方程返回了正确的答案并且没有抛出这个异常。 这是我对这个问题的解决方案,希望对其他人有所帮助。

public Vector<double> CalculateWithQR(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

        try
        {
            result = MultipleRegression.QR(x, y);

            // check for NaN and infinity
            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }
        }
        catch (Exception ex)
        {
        }

        return result;
    }

    public Vector<double> CalculateWithNormal(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

        try
        {
            result = MultipleRegression.NormalEquations(x, y);

            // check for NaN and infinity
            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }
        }
        catch (Exception ex)
        {
        }

        return result;
    }

    public Vector<double> CalculateWithSVD(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

        try
        {
            result = MultipleRegression.Svd(x, y);

            // check for NaN and infinity
            for (int i = 0; i < result.Count; i++)
            {
                var value = result.ElementAt(i);

                if (Double.IsNaN(value) || Double.IsInfinity(value))
                {
                    return null;
                }
            }
        }
        catch (Exception ex)
        {
        }

        return result;
    }

    public Vector<double> FindBestMRSolution(Matrix<double> x, Vector<double> y)
    {
        Vector<double> result = null;

        try
        {
            result = CalculateWithNormal(x, y);

            if (result != null)
            {
                return result;
            }
            else
            {
                result = CalculateWithSVD(x, y);

                if (result != null)
                {
                    return result;
                }
                else
                {
                    result = CalculateWithQR(x, y);

                    if (result != null)
                    {
                        return result;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
        }

        return result;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM