2010-03-16 96 views
8

在MySQL(5.1)数据库表存在,表示数据:MySQL数学 - 有可能在查询中计算相关性吗?

  • 用户需要多长时间来执行任务,并
  • 用户在任务期间有多少项目来处理。

MySQL会支持关联数据还是需要使用PHP/C#来计算?

我在哪里可以找到一个很好的公式来计算相关性(自从我上次做这件事以来已经很长时间了)?

回答

13

这里的一个粗略的实施样品相关系数的如描述:

Wikipedia - Correlation and Dependence

create table sample(x float not null, y float not null); 
insert into sample values (1, 10), (2, 4), (3, 5), (6,17); 

select @ax := avg(x), 
     @ay := avg(y), 
     @div := (stddev_samp(x) * stddev_samp(y)) 
from sample; 

select sum((x - @ax) * (y - @ay))/((count(x) -1) * @div) from sample; 
+---------------------------------------------------------+ 
| sum((x - @ax) * (y - @ay))/((count(x) -1) * @div) | 
+---------------------------------------------------------+ 
|          0.700885077729073 | 
+---------------------------------------------------------+ 
+0

谢谢马丁。 工程很好 - 我得到了.39的相关性 - 有点弱,但在正确的轨道上。 – 2010-03-18 14:39:35

0

有Pearson相关系数的两种口味,一个用于样品,一个用于整个种群。这些都是单向的,我相信这两个公式都是正确的:

-- Methods for calculating the two Pearson correlation coefficients 
SELECT 
     -- For Population 
     (avg(x * y) - avg(x) * avg(y))/
     (sqrt(avg(x * x) - avg(x) * avg(x)) * sqrt(avg(y * y) - avg(y) * avg(y))) 
     AS correlation_coefficient_population, 
     -- For Sample 
     (count(*) * sum(x * y) - sum(x) * sum(y))/
     (sqrt(count(*) * sum(x * x) - sum(x) * sum(x)) * sqrt(count(*) * sum(y * y) - sum(y) * sum(y))) 
     AS correlation_coefficient_sample 
    FROM your_table; 

我开发并测试了它作为T-SQL。生成测试数据的代码没有转换为MySQL,但公式应该。确保你的x和y是小数值;整数数学可以显着影响这些计算。