如何确定矩阵是否是“正定”矩阵通过 SQL?
有什么方法,纯粹在MSSQL中,可以确定以下 maxtrix 是否会计算为正定?
A C D G H I
A 1.00 0.68 0.24 0.62 0.90 0.00
C 0.68 1.00 0.25 0.46 0.61 0.00
D 0.24 0.25 1.00 0.60 0.08 0.00
G 0.62 0.46 0.60 1.00 0.46 0.00
H 0.90 0.61 0.08 0.46 1.00 0.00
I 0.00 0.00 0.00 0.00 0.00 1.00
现在,我们正在使用第 3 方应用程序 ExtremeNumerics,以相当黑盒的方式处理确定。如果我有一个可以输入资产、相关资产和价值的 SQL 表,是否有办法进行数学计算?
我查了一下,并没有真正看到 MSSQL 中有任何处理矩阵数学的东西。
谢谢。
编辑:微软 SQL 2008
Is there any way, purely in MSSQL, to determine if the following maxtrix would calculate out as positive definite?
A C D G H I
A 1.00 0.68 0.24 0.62 0.90 0.00
C 0.68 1.00 0.25 0.46 0.61 0.00
D 0.24 0.25 1.00 0.60 0.08 0.00
G 0.62 0.46 0.60 1.00 0.46 0.00
H 0.90 0.61 0.08 0.46 1.00 0.00
I 0.00 0.00 0.00 0.00 0.00 1.00
Right now we're using a 3rd party app, ExtremeNumerics, to handle the determination in a rather blackbox way. If I had a SQL table that I could enter the assets, the correlated asset and the value, would there be a way to do the math?
I poked around some and I haven't really seen anything in MSSQL that handles matrix math.
thanks.
edit: Microsoft SQL 2008
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我们开始吧。这确实有效,但确实给人一种我们必须强制 SQL Server 去做它并不真正想做的事情的感觉。我不愿意建议在现实生活中这样做 - 我相当确定它的矩阵大小将缩放为
O(n^3)
。也许有更好的方法,进行 Cholesky 分解 而不是这种方式 - 我可能会在稍后的日期。说完这些注意事项,让我们继续:这需要 SQL Server 2008,因为它的
表
数据类型(即使这样,也没有达到应有的帮助) ,正如我们将看到的...)
首先是方法。我们将使用西尔维斯特准则,因为它是最容易理解的:实对称矩阵是 PD 当且仅当所有主次要因素的行列式都是正的。所以我们需要一种计算行列式的方法。同样,我们将使用一种简单的方法(拉普拉斯展开),而不是任何为计算而设计的方法效率。
基础工作
我们首先定义将用于传递矩阵的用户定义表类型:
计算行列式
为此,我们将定义两个相互递归函数,因为这是我可以使其工作的唯一方法考虑到 SQL Server 2008 中
table
类型数据的功能有限。首先,入口点(也处理基本情况):
确定我们不在基本情况(1x1 矩阵)时,我们现在有工作要做。第一件事是将输入中的行数和列数“规范化”,从现在的行数和列数变为
1..n
我们现在准备使用 拉普拉斯展开式。这涉及到调用我们的相互递归同志,它将按我们请求的行和列进行小数化,然后回调我们来计算小数的行列式,
事实证明,DeterminantOfMinor 非常简单,如果
table
值在 SQL Server 中更一流,则没有必要:有了行列式计算器,我们就快成功了。
正定性测试
根据 Sylvester 准则,矩阵是 PD 当且仅当所有的行列式其主要未成年人呈阳性。因此,我们可以构建一个(自)递归函数来检查这一点,唯一的问题是,值得确保我们首先执行廉价行列式:
我们构建我们的矩阵输入没有最后一行和最后一列:
并向下递归,仅如果我们所有的主要次要因素都被确认为 PD,则计算我们输入的行列式:
就是这样!
测试
我使用了您的示例:
这些结果与我从此在线计算器获得的结果一致,所以我很高兴这有效。
时间
在我测试的系统上使用测试数据的前
n
列:令人担忧的趋势,我相信您会同意。因此我的:
警告
我认为这段代码只不过是一个概念证明:
O(n^3)
增长Matrix
输入值中的值没有意义,将导致一切 也就是说,这是一个有趣的练习,希望能为您提供一些有用的指导,告诉您如何在现实生活中实际实现这一点。
也许稍后我会考虑使用 Cholesky 分解 来实现......
Right, here we go. This works, but does give the feeling that we are having to strong-arm SQL Server into doing things it doesn't really want to. I'd be disinclined to recommend doing this in real life - it's going to scale as
O(n^3)
in matrix size, I'm reasonably sure. Perhaps there's a better way, doing Cholesky decomposition rather than this way - I might look into this at a later date. With the caveats out of the say, let's proceed:This requires SQL Server 2008, for its
table
datatype(and even that falls somewhat short of being as helpful as it might be, as we'll see...)
First, the approach. We're going to use Sylvester's criterion, as it's the easiest to understand: a real symmetric matrix is PD iff the determinants of all the principal minors are positive. So we'll need a way of calculating determinants. Again, we're going to use a simple approach (Laplace expansion) rather than anything designed for computational efficiency.
Groundwork
We start with defining the user-defined table type we're going to use to pass matrices around:
Calculating determinants
For this, we're going to define two mutually-recursive functions, because that was the only way I could make it work given the limited capabilities of
table
type data in SQL Server 2008.First, the entry point (which also handles the base case):
Having established we're not at the base case (a 1x1 matrix), we now have work to do. The first thing is to 'canonicalize' the row and column numbers in our input from whatever they are now to
1..n
We're now ready to recursively compute the determinant using Laplace expansion. This involves a call out to our mutually-recursive comrade, which will minorize by the row and column we request, then call us back to compute the determinant of the minor
As it turns out
DeterminantOfMinor
is very simple, and wouldn't be necessary iftable
values were more first-class in SQL Server:With a determinant calculator available, we're nearly there.
Testing for positive-definiteness
According to Sylvester's criterion, a matrix is PD iff the determinants of all its principal minors are positive. So we can build a (self-)recursive function to check this, the only twist being that it's worth making sure we do the cheap determinants (the smaller matrices) first:
We build the matrix which is our input without its last row and column:
and recurse down, only computing the determinant of our input if all our principal minors are confirmed to be PD:
And that's it!
Testing
I used your sample:
These results agree with what I got from this online calculator, so I'm happy this works.
Timings
Using the first
n
columns of your test data, on the system I tested on:Worrying trend, I'm sure you'll agree. Hence my:
Caveats
I would regard this code as no more than a proof of concept:
O(n^3)
in matrix sizeMatrix
input value that don't make sense, will cause everything to fall in a heapThat said, it's an interesting exercise and hopefully will give you some useful pointers as to how you might actually approach this in real life.
Perhaps I'll look at doing it using Cholesky decomposition at a later date...