Jump to content
Sign in to follow this  
glasglow

Correlation Calculation

Recommended Posts

glasglow

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 this post


Link to post
Share on other sites
enaiman

Raw code but here is your answer:

$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 this post


Link to post
Share on other sites
glasglow

Raw code but here is your answer:

$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 this post


Link to post
Share on other sites
enaiman

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 this post


Link to post
Share on other sites
glasglow

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. : )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.