Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
矩阵必须是正定的(Math.Net C#library)_C#_Statistics_Mathnet Numerics - Fatal编程技术网

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

矩阵必须是正定的(Math.Net C#library),c#,statistics,mathnet-numerics,C#,Statistics,Mathnet Numerics,我在C#的Math.Net Numerics库中遇到了一个奇怪的问题。直到最近,我的代码还运行得很好(据我所知,没有任何变化),但现在我从标题中得到了错误消息,它试图计算多元回归 每个列表都有493个双值,所以有人知道我可以做些什么来解决这些问题吗 Vector<double> vectorArrayBuy = CreateVector.Dense(listMRInfoBuy.ElementAt(0).OutputBuy.ToArray()); var matrixArrayBuy

我在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);
Vector vectorArrayBuy=CreateVector.Dense(listMRInfoBuy.ElementAt(0.OutputBuy.ToArray());
var matrixArrayBuy=CreateMatrix.denseOfColumnArray(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=多重回归。正态方程组(MatrixarArrayBuy,vectorArrayBuy);
“矩阵不正定”可能意味着你的独立方程少于n个,这反过来意味着你没有n个独立的数据,这可能意味着你的数据在某些方面有缺陷(例如,它们被错误地读取,实际上都是一样的或类似的)


也许您可以编辑您的问题,以显示您正在处理的数据是什么。可能您要开始的数据少于n个。

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

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;
    }
public Vector CalculateWithQR(矩阵x,向量y)
{
向量结果=空;
尝试
{
结果=多重回归。QR(x,y);
//检查NaN和无穷大
for(int i=0;i
只是为那些(像我一样)不记得你上学时的线性代数或高级统计数据的人添加了一个解决方案

  • 如果还没有,请在Excel中应用“AnalysisToolPak”加载项
  • 将自变量和因变量粘贴到工作表中
  • 转到数据->数据分析->回归
  • 提供它要求的范围并运行回归
  • 在回归结果中,您会发现一个或多个p值或t统计返回a div/0或NUM错误
  • 从MathNet回归调用中删除这些自变量,然后再次运行
  • 这应该可以解决它

    然后我继续添加一个迭代的try-and-catch,根据具体情况删除独立变量,然后再次运行它


    IHTH

    正规方程组使用Cholesky分解,其矩阵应为hernitian正定矩阵。正如错误消息所说-你的矩阵不是。@Evk很抱歉听起来像个白痴,但我如何用math.net数值确定我的矩阵是正定的,以及如何修正它使其是正定的?你的系统可能是病态的。
    matrixArrayBuy.ConditionNumber()
    返回什么?如果您使用
    multiperegresion.QR
    multiperegresion.NormalEquations,它是否偶然起作用