fandreacci Posted January 13, 2011 Share Posted January 13, 2011 //SAMPLE 1 DATA $x[] = 1301.45; $x[] = 1028.28; $x[] = 2386.98; $x[] = 479.66; $x[] = 4017.31; $x[] = 2947.39; $x[] = 1561.90; $x[] = 2510.86; $x[] = 1808.22; $x[] = 987.93; $x[] = 2658.48; $x[] = 1110.40; $x[] = 465.75; $x[] = 1782.85; $x[] = 1512.25; $x[] = 1748.03; $x[] = 1003.62; $x[] = 578.13; $x[] = 2103.99; $x[] = 6249.16; $x[] = 743.12; $x[] = 789.02; $x[] = 457.58; $x[] = 540.98; $x[] = 449.46; $x[] = 500.44; $x[] = 1181.20; $x[] = 1422.96; $x[] = 4493.26; $x[] = 673.63; $x[] = 664.99; $x[] = 3598.93; $x[] = 1652.60; $x[] = 1887.02; $x[] = 2109.16; $x[] = 3190.90; $x[] = 2025.76; $x[] = 1786.44; $x[] = 2065.75; $x[] = 2160.60; $x[] = 709.32; $x[] = 632.55; $x[] = 522.68; $x[] = 782.89; $x[] = 830.31; $x[] = 699.99; $x[] = 599.99; $x[] = 417.27; $x[] = 235.12; $x[] = 243.44; $x[] = 235.12; $x[] = 578.14; $x[] = 686.87; $x[] = 460.73; $x[] = 343.43; $x[] = 347.80; $x[] = 521.70; $x[] = 1135.06; $x[] = 808.50; $x[] = 273.84; $x[] = 848.52; $x[] = 1298.37; $x[] = 517.25; $x[] = 345.53; $x[] = 1108.37; $x[] = 1101.49; $x[] = 432.19; $x[] = 2665.15; $x[] = 4211.44; $x[] = 432.19; $x[] = 3674.36; $x[] = 3115.58; $x[] = 2387.14; $x[] = 2843.54; $x[] = 2337.72; $x[] = 1187.00; $x[] = 1121.48; $x[] = 2193.11; $x[] = 1759.18; $x[] = 2392.31; $x[] = 1657.36; $x[] = 1652.35; $x[] = 1279.14; $x[] = 2141.22; $x[] = 1824.40; $x[] = 2588.53; $x[] = 1147.30; $x[] = 1047.59; $x[] = 1453.04; $x[] = 1116.75; $x[] = 725.48; $x[] = 371.68; $x[] = 425.70; $x[] = 796.70; //SAMPLE 2 DATA $y[] = 1538.44; $y[] = 4162.40; $y[] = 890.86; $y[] = 935.39; $y[] = 2123.34; $y[] = 794.65; $y[] = 1205.56; $y[] = 2726.06; $y[] = 1222.14; $y[] = 1520.81; $y[] = 1342.54; $y[] = 3836.90; $y[] = 698.63; $y[] = 3647.81; $y[] = 671.83; $y[] = 1851.27; $y[] = 3232.88; $y[] = 5082.69; $y[] = 3221.18; $y[] = 1846.11; $y[] = 3685.61; $y[] = 9521.83; $y[] = 3309.90; $y[] = 5828.21; $y[] = 8243.15; $y[] = 3082.19; $y[] = 2630.29; $y[] = 698.63; $y[] = 753.56; $y[] = 602.91; $y[] = 1000.40; $y[] = 1041.48; $y[] = 1753.65; $y[] = 411.20; $y[] = 3439.41; $y[] = 4074.20; $y[] = 3179.73; $y[] = 6746.77; $y[] = 2311.18; $y[] = 3107.68; $y[] = 2464.01; $y[] = 2957.40; $y[] = 879.64; $y[] = 815.64; $y[] = 1864.97; $y[] = 976.47; $y[] = 1010.01; $y[] = 989.93; $y[] = 976.47; $y[] = 589.34; $y[] = 729.22; $y[] = 968.86; $y[] = 912.29; $y[] = 1205.43; $y[] = 675.79; $y[] = 452.81; $y[] = 476.22; $y[] = 392.89; $y[] = 822.52; $y[] = 835.76; $y[] = 1026.29; $y[] = 809.29; $y[] = 1469.14; $y[] = 739.07; $y[] = 1839.42; $y[] = 3748.92; $y[] = 2664.70; $y[] = 847.59; $y[] = 3190.39; $y[] = 4695.29; $y[] = 4005.25; $y[] = 1437.19; $y[] = 4128.00; $y[] = 4085.50; $y[] = 1309.04; $y[] = 4533.39; $y[] = 3809.06; $y[] = 749.13; $y[] = 3474.56; $y[] = 2657.05; $y[] = 2629.93; $y[] = 2714.28; $y[] = 4233.91; $y[] = 2215.41; $y[] = 1145.49; $y[] = 1474.78; $y[] = 1286.16; $y[] = 1337.04; $y[] = 2222.28; $y[] = 2621.11; $y[] = 2571.50; $y[] = 3223.01; $y[] = 2759.02; $y[] = 3045.74; $y[] = 2493.63; $y[] = 1134.24; $y[] = 1308.62; $y[] = 1212.77; $y[] = 3093.44; $y[] = 1428.42; //SIMPLE CORRELATION FUNCION function simplecorrelation($x, $y) { if(count($x) != count($y)) { print "series must have same size"; return false; } // Number of values or elements $n = count($x); //Sum of the product of first and Second Scores $sumPxy = 0; for($i=0;$i<count($x);$i++) { $sumPxy += $x[$i]*$y[$i]; } //Sum of First Scores $sumx = 0; //Sum of square First Scores $sumxs = 0; //Sum of Second Scores $sumy = 0; //Sum of square Second Scores $sumys = 0; for($i=0;$i<count($x);$i++) { $sumx += $x[$i]; $sumxs += $x[$i]*$x[$i]; $sumy += $y[$i]; $sumys += $y[$i]*$y[$i]; } $correlation = ((($n*$sumPxy)-($sumx*$sumy)) / sqrt(($n*$sumxs - ($sumx*$sumx))*($n*$sumys - ($sumy*$sumy)))); return $correlation; } //TEST $a = array(60,61,62,63,65); $b = array(3.1,3.6,3.8,4,4.1); print simplecorrelation($a,$b); //IT RETURNS 0.91187249533773 Ok, it works. However my big problem is that my $x and $y have different sizes. $x has 94 as $y has 100 values. I must write a code that removes the difference (in this case, remove 6 values from $y) giving the best correlation as possible. So the code must not remove 6 random elements, the code must remove the 6 which removed will give to me the best correlation (using my symplecorrelation()) between $x and $y. Got it? Thanks! Link to comment https://forums.phpfreaks.com/topic/224303-php-logics-correlations/ Share on other sites More sharing options...
fandreacci Posted January 13, 2011 Author Share Posted January 13, 2011 I did it! however it's non functional to large data. Can someone help me to improve? my total number of combations $total = $num * $num; is 10000, but i'm getting duplicate values that uses the array space. And if I increase the $total value, php can't handle with the large data. //SIMPLE CORRELATION FUNCION function simplecorrelation($x, $y) { if(count($x) != count($y)) { print "series must have same size"; return false; } // Number of values or elements $n = count($x); //Sum of the product of first and Second Scores $sumPxy = 0; for($i=0;$i<count($x);$i++) { $sumPxy += $x[$i]*$y[$i]; } //Sum of First Scores $sumx = 0; //Sum of square First Scores $sumxs = 0; //Sum of Second Scores $sumy = 0; //Sum of square Second Scores $sumys = 0; for($i=0;$i<count($x);$i++) { $sumx += $x[$i]; $sumxs += $x[$i]*$x[$i]; $sumy += $y[$i]; $sumys += $y[$i]*$y[$i]; } $correlation = ((($n*$sumPxy)-($sumx*$sumy)) / sqrt(($n*$sumxs - ($sumx*$sumx))*($n*$sumys - ($sumy*$sumy)))); return $correlation; } $words = array(); for($i=1;$i<=100;$i++) array_push($words,$i); $num = count($words); $w =0; //The total number of possible combinations $total = $num * $num; $temp = array(); $comb = array(); //I NEED TO REMOVE GROUPS OF 6 ELEMENTS $remove = 6; //Loop through each possible combination //PROBABLY HERE IS THE BUG //I'M GETTINT TO MUCH SIMILAR COMBINATIONS for ($i = 0; $i < $total; $i++) { //For each combination check if each bit is set for ($j = 0; $j < $num; $j++) { //Is bit $j set in $i? //if (pow(2, $j) & $i) echo $words[$j] . ' '; if (pow(2, $j) & $i) { $w++; $temp[] = $words[$j]; } if($w == $remove) { $comb[$i] = $temp; $w = 0; $temp = array(); } } } //REMOVING DUPLICATE VALUES foreach($comb as $p) { $b[] = array_unique($p); } foreach($b as $c) { if(count($c) == $remove) { $final[] = $c; } } $master = array(); foreach($final as &$t) { sort($t); } function multi_unique($array) { foreach ($array as $k=>$na) $new[$k] = serialize($na); $uniq = array_unique($new); foreach($uniq as $k=>$ser) $new1[$k] = unserialize($ser); return ($new1); } $final = multi_unique($final); //REAL VALUES $a = array(796.70, 425.70,371.68,725.48,1116.75,1453.04,1047.59,1147.30,2588.53,1824.40,2141.22,1279.14,1652.35,1657.36,2392.31,1759.18,2193.11,1121.48,1187.00,2337.72,2843.54,2387.14,3115.58,3674.36,432.19,4211.44,2665.15,432.19,1101.49,1108.37,345.53,517.25,1298.37,848.52,273.84,808.50,1135.06,521.70,347.80,343.43,460.73,686.87,578.14,235.12,243.44,235.12,417.27,599.99,699.99,830.31,782.89,522.68,632.55,709.32,2160.60,2065.75,1786.44,2025.76,3190.90,2109.16,1887.02,1652.60,3598.93,664.99,673.63,4493.26,1422.96,1181.20,500.44,449.46,540.98,457.587,89.02,743.12,6249.16,2103.99,578.13,1003.62,1748.03,1512.25,1782.85,465.75,1110.40,2658.48,987.93,1808.22,2510.86,1561.90,2947.39,4017.31,479.66,2386.98,1028.28,1301.45); $b = array(1428.42,3093.44,1212.77,1308.62,1134.24,2493.63,3045.74,2759.02,3223.01,2571.50,2621.11,2222.28,1337.04,1286.16,1474.78,1145.49,2215.41,4233.91,2714.28,2629.93,2657.05,3474.56,749.13,3809.06,4533.39,1309.04,4085.50,4128.00,1437.19,4005.25,4695.29,3190.39,847.59,2664.70,3748.92,1839.42,739.07,1469.14,809.29,1026.29,835.76,822.52,392.89,476.22,452.81,675.79,1205.43,912.29,968.86,729.22,589.34,976.47,989.93,1010.01,976.47,1864.97,815.64,879.64,2957.40,2464.01,3107.68,2311.18,6746.77,3179.73,4074.20,3439.41,411.20,1753.65,1041.48,1000.40,602.91,753.56,698.63,2630.29,3082.19,8243.15,5828.21,3309.90,9521.83,3685.61,1846.11,3221.18,5082.69,3232.88,1851.27,671.83,3647.81,698.63,3836.90,1342.54,1520.81,1222.14,2726.06,1205.56,794.65,2123.34,935.39,890.86,4162.40,1538.44); //MINUMUM CORRELATION $correl = -1; $keys = array(); foreach($final as $cur) { $tb = $b; foreach($cur as $p) { unset($tb[$p]); } $correlation = simplecorrelation($a,$tb); if($correlation > $correl) { $correl = $correlation; $keys = $cur; } } print $correl; //KEYS WITCH REMOVED IMPROVE THE CORRELATIONS print_r($keys); Link to comment https://forums.phpfreaks.com/topic/224303-php-logics-correlations/#findComment-1158980 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.