forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Phpml\Math\Statistic; | ||
|
||
use Phpml\Exception\InvalidArgumentException; | ||
|
||
class Correlation | ||
{ | ||
/** | ||
* @param array|int[]|float[] $x | ||
* @param array|int[]|float[] $y | ||
* | ||
* @return float | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function pearson(array $x, array $y) | ||
{ | ||
if (count($x) !== count($y)) { | ||
throw InvalidArgumentException::arraySizeNotMatch(); | ||
} | ||
|
||
$count = count($x); | ||
$meanX = array_sum($x) / $count; | ||
$meanY = array_sum($y) / $count; | ||
|
||
$axb = 0; | ||
$a2 = 0; | ||
$b2 = 0; | ||
|
||
for ($i = 0;$i < $count;++$i) { | ||
$a = $x[$i] - $meanX; | ||
$b = $y[$i] - $meanY; | ||
$axb = $axb + ($a * $b); | ||
$a2 = $a2 + pow($a, 2); | ||
$b2 = $b2 + pow($b, 2); | ||
} | ||
|
||
$corr = $axb / sqrt($a2 * $b2); | ||
|
||
return $corr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Phpml\Math\Statistic; | ||
|
||
use Phpml\Exception\InvalidArgumentException; | ||
|
||
class StandardDeviation | ||
{ | ||
|
||
/** | ||
* @param array|float[] $a | ||
* @param bool $sample | ||
* @param bool $sample | ||
* | ||
* @return float | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function population(array $a, $sample = true) | ||
{ | ||
if(empty($a)) { | ||
if (empty($a)) { | ||
throw InvalidArgumentException::arrayCantBeEmpty(); | ||
} | ||
|
||
$n = count($a); | ||
|
||
if ($sample && $n === 1) { | ||
throw InvalidArgumentException::arraySizeToSmall(2); | ||
} | ||
|
||
$mean = array_sum($a) / $n; | ||
$carry = 0.0; | ||
foreach ($a as $val) { | ||
$d = $val - $mean; | ||
$carry += $d * $d; | ||
}; | ||
|
||
if($sample) { | ||
if ($sample) { | ||
--$n; | ||
} | ||
|
||
return sqrt($carry / $n); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace test\Phpml\Math\StandardDeviation; | ||
|
||
use Phpml\Math\Statistic\Correlation; | ||
|
||
class CorrelationTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testPearsonCorrelation() | ||
{ | ||
//http://www.stat.wmich.edu/s216/book/node126.html | ||
$delta = 0.001; | ||
$x = [9300, 10565, 15000, 15000, 17764, 57000, 65940, 73676, 77006, 93739, 146088, 153260]; | ||
$y = [7100, 15500, 4400, 4400, 5900, 4600, 8800, 2000, 2750, 2550, 960, 1025]; | ||
$this->assertEquals(-0.641, Correlation::pearson($x, $y), '', $delta); | ||
|
||
//http://www.statisticshowto.com/how-to-compute-pearsons-correlation-coefficients/ | ||
$delta = 0.001; | ||
$x = [43, 21, 25, 42, 57, 59]; | ||
$y = [99, 65, 79, 75, 87, 82]; | ||
$this->assertEquals(0.549, Correlation::pearson($x, $y), '', $delta); | ||
} | ||
|
||
/** | ||
* @expectedException \Phpml\Exception\InvalidArgumentException | ||
*/ | ||
public function testThrowExceptionOnInvalidArgumentsForPearsonCorrelation() | ||
{ | ||
Correlation::pearson([1, 2, 4], [3, 5]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,5 +39,4 @@ public function testThrowExceptionOnToSmallArray() | |
{ | ||
StandardDeviation::population([1]); | ||
} | ||
|
||
} |