# Correlation Calculation

## Recommended Posts

Can anyone get a coorelation.. This is the formula off of wikipedia. (http://en.wikipedia.org/wiki/Correlation). I was trying to work it out in autoit...

```\$sum_sq_x = 0
\$sum_sq_y = 0
\$sum_coproduct = 0
\$mean_x = \$x[1]
\$mean_y = \$y[1]
for \$i in 2 to \$N:
\$sweep = (\$i - 1.0) / \$i
\$delta_x = \$x[\$i] - \$mean_x
\$delta_y = \$y[\$i] - \$mean_y
\$sum_sq_x += \$delta_x * \$delta_x * \$sweep
\$sum_sq_y += \$delta_y * \$delta_y * \$sweep
\$sum_coproduct += \$delta_x * \$delta_y * \$sweep
\$mean_x += \$delta_x / \$i
\$mean_y += \$delta_y / \$i
\$pop_sd_x = sqrt( \$sum_sq_x / \$N )
\$pop_sd_y = sqrt( \$sum_sq_y / \$N )
\$cov_x_y = \$sum_coproduct / \$N
\$correlation = \$cov_x_y / (\$pop_sd_x * \$pop_sd_y)```

The original formula is:

```sum_sq_x = 0
sum_sq_y = 0
sum_coproduct = 0
mean_x = x[1]
mean_y = y[1]
for i in 2 to N:
sweep = (i - 1.0) / i
delta_x = x[i] - mean_x
delta_y = y[i] - mean_y
sum_sq_x += delta_x * delta_x * sweep
sum_sq_y += delta_y * delta_y * sweep
sum_coproduct += delta_x * delta_y * sweep
mean_x += delta_x / i
mean_y += delta_y / i
pop_sd_x = sqrt( sum_sq_x / N )
pop_sd_y = sqrt( sum_sq_y / N )
cov_x_y = sum_coproduct / N
correlation = cov_x_y / (pop_sd_x * pop_sd_y)```

##### Share on other sites

```\$sum_sq_x = 0
\$sum_sq_y = 0
\$sum_coproduct = 0
\$N = 4
\$mean_x = 0
\$mean_y = 0

Dim \$x[\$N] = [50, 75, 82, 96]
Dim \$y[\$N] = [25, 55, 72, 86]

For \$i = 0 to \$N - 1
\$mean_x += \$x[\$i]
\$mean_y += \$y[\$i]
Next

\$mean_x = \$mean_x / 4
\$mean_y = \$mean_y / 4

For \$i = 2 to \$N
\$sweep = (\$i - 1.0) / \$i
\$delta_x = \$x[\$i-2] - \$mean_x
\$delta_y = \$y[\$i-2] - \$mean_y
\$sum_sq_x += \$delta_x * \$delta_x * \$sweep
\$sum_sq_y += \$delta_y * \$delta_y * \$sweep
\$sum_coproduct += \$delta_x * \$delta_y * \$sweep
\$mean_x += \$delta_x / \$i
\$mean_y += \$delta_y / \$i
Next
\$pop_sd_x = sqrt( \$sum_sq_x / \$N )
\$pop_sd_y = sqrt( \$sum_sq_y / \$N )
\$cov_x_y = \$sum_coproduct / \$N
\$correlation = \$cov_x_y / (\$pop_sd_x * \$pop_sd_y)
MsgBox(0, "correlation", \$correlation)```

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

##### Share on other sites

```\$sum_sq_x = 0
\$sum_sq_y = 0
\$sum_coproduct = 0
\$N = 4
\$mean_x = 0
\$mean_y = 0

Dim \$x[\$N] = [50, 75, 82, 96]
Dim \$y[\$N] = [25, 55, 72, 86]

For \$i = 0 to \$N - 1
\$mean_x += \$x[\$i]
\$mean_y += \$y[\$i]
Next

\$mean_x = \$mean_x / 4
\$mean_y = \$mean_y / 4

For \$i = 2 to \$N
\$sweep = (\$i - 1.0) / \$i
\$delta_x = \$x[\$i-2] - \$mean_x
\$delta_y = \$y[\$i-2] - \$mean_y
\$sum_sq_x += \$delta_x * \$delta_x * \$sweep
\$sum_sq_y += \$delta_y * \$delta_y * \$sweep
\$sum_coproduct += \$delta_x * \$delta_y * \$sweep
\$mean_x += \$delta_x / \$i
\$mean_y += \$delta_y / \$i
Next
\$pop_sd_x = sqrt( \$sum_sq_x / \$N )
\$pop_sd_y = sqrt( \$sum_sq_y / \$N )
\$cov_x_y = \$sum_coproduct / \$N
\$correlation = \$cov_x_y / (\$pop_sd_x * \$pop_sd_y)
MsgBox(0, "correlation", \$correlation)```
En.. I see I see.. I had a few problems there with the code. Thank you for clearing it up.

##### Share on other sites

I said it's still raw code because it needs some checks for dividing by 0 and such. You can do that

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

##### Share on other sites

I said it's still raw code because it needs some checks for dividing by 0 and such. You can do that

Yes I noticed some variation in the actual outcomes .. no worries.. it's a REALLY good start. : )

## Create an account

Register a new account

×

• Wiki

• Back

• Git