From 2305380945db7118efe6df311849520dc0060611 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 25 Feb 2018 16:12:31 +0100 Subject: [PATCH] More tests (math tests) and fixes --- Math/Functions/Functions.php | 21 +- Math/Functions/Gamma.php | 122 ++++++ Math/Number/Complex.php | 360 +++++++++++++++++- .../Distribution/BernoulliDistribution.php | 47 ++- .../Distribution/BinomialDistribution.php | 12 +- .../Distribution/ChiSquaredDistribution.php | 3 +- Message/Http/BrowserType.php | 1 + Message/Http/Header.php | 3 + Message/Http/OSType.php | 1 + Message/Http/Request.php | 8 +- Message/RequestAbstract.php | 9 - Validation/ModelValidationTrait.php | 93 ----- Views/ViewAbstract.php | 9 - tests/Business/Finance/StockBondsTest.php | 8 + tests/Math/Functions/FunctionsTest.php | 2 - tests/Math/Functions/GammaTest.php | 46 +++ tests/Math/Number/ComplexTest.php | 93 ++++- .../BernoulliDistributionTest.php | 80 +++- .../Distribution/BinomialDistributionTest.php | 82 +++- .../Distribution/CauchyDistributionTest.php | 23 +- tests/Message/Http/BrowserTypeTest.php | 3 +- tests/Message/Http/OSTypeTest.php | 2 +- tests/Message/Http/RequestTest.php | 6 +- 23 files changed, 861 insertions(+), 173 deletions(-) create mode 100644 Math/Functions/Gamma.php delete mode 100644 Validation/ModelValidationTrait.php create mode 100644 tests/Math/Functions/GammaTest.php diff --git a/Math/Functions/Functions.php b/Math/Functions/Functions.php index 79ca16a8a..85ec63fc1 100644 --- a/Math/Functions/Functions.php +++ b/Math/Functions/Functions.php @@ -4,7 +4,7 @@ * * PHP Version 7.1 * - * @package TBD + * @package phpOMS\Math\Functions * @copyright Dennis Eichhorn * @license OMS License 1.0 * @version 1.0.0 @@ -17,29 +17,14 @@ namespace phpOMS\Math\Functions; /** * Well known functions and helpers class. * - * @package Framework + * @package phpOMS\Math\Functions * @license OMS License 1.0 * @link http://website.orange-management.de * @since 1.0.0 */ class Functions { - /** - * Calculate gammar function value. - * - * Example: (7) - * - * @param int $k Variable - * - * @return int - * - * @since 1.0.0 - */ - public static function getGammaInteger(int $k) : int - { - return self::fact($k - 1); - } - + /** * Calculate gammar function value. * diff --git a/Math/Functions/Gamma.php b/Math/Functions/Gamma.php new file mode 100644 index 000000000..dcd74a4b9 --- /dev/null +++ b/Math/Functions/Gamma.php @@ -0,0 +1,122 @@ +re = $re; + $this->im = $im; } - public function setReal() /* : void */ + /** + * Get real part + * + * @return mixed + * + * @since 1.0.0 + */ + public function re() { + return $this->re; } - public function setImaginary() /* : void */ + /** + * Get imaginary part + * + * @return mixed + * + * @since 1.0.0 + */ + public function im() { + return $this->im; } - public function getImaginary() + /** + * Conjugate + * + * @latex z = a - b*i + * + * @return Complex + * + * @since 1.0.0 + */ + public function conjugate() : Complex { + return new self($this->re, -$this->im); } - public function pow() + /** + * Reciprocal + * + * @return Complex + * + * @since 1.0.0 + */ + public function reciprocal() : Complex { + return new self( + $this->re / ($this->re ** 2 + $this->im ** 2), + -$this->im / ($this->re ** 2 + $this->im ** 2) + ); } - public function add() + /** + * Square root + * + * @return Complex + * + * @since 1.0.0 + */ + public function sqrt() : Complex { + return new self( + sqrt(($this->re + sqrt($this->re ** 2 + $this->im ** 2)) / 2), + ($this->im <=> 0) * sqrt((-$this->re + sqrt($this->re ** 2 + $this->im ** 2)) / 2) + ); } - public function sub() + /** + * Absolute + * + * @return mixed + * + * @since 1.0.0 + */ + public function abs() { + return sqrt($this->re ** 2 + $this->im ** 2); } - public function mult() + /** + * Square + * + * @return Complex + * + * @since 1.0.0 + */ + public function square() : Complex { + return $this->multComplex($this, $this); } - public function div() + public function pow($value) : Complex { + if (is_numeric($value)) { + return $this->powScalar($value); + } elseif ($value instanceof Complex) { + return $this->powComplex($value); + } + + throw new \InvalidArgumentException(); + } + + public function powComplex() : Complex + { + + } + + public function powScalar() : Complex + { + + } + + /** + * Add opperator + * + * @param mixed $value Value to add + * + * @return Complex + * + * @throws \InvalidArgumentException + * + * @since 1.0.0 + */ + public function add($value) : Complex + { + if (is_numeric($value)) { + return $this->addScalar($value); + } elseif ($value instanceof Complex) { + return $this->addComplex($value); + } + + throw new \InvalidArgumentException(); + } + + /** + * Add opperator + * + * @param Complex $cpl Value to add + * + * @return Complex + * + * @since 1.0.0 + */ + private function addComplex(Complex $cpl) : Complex + { + return new self($this->re + $cpl->re(), $this->im + $cpl->im()); + } + + /** + * Add opperator + * + * @param mixed $val Value to add + * + * @return Complex + * + * @since 1.0.0 + */ + private function addScalar($val) : Complex + { + return new self($this->re + $val, $this->im); + } + + /** + * Sub opperator + * + * @param mixed $value Value to sub + * + * @return Complex + * + * @throws \InvalidArgumentException + * + * @since 1.0.0 + */ + public function sub($value) : Complex + { + if (is_numeric($value)) { + return $this->subScalar($value); + } elseif ($value instanceof Complex) { + return $this->subComplex($value); + } + + throw new \InvalidArgumentException(); + } + + /** + * Sub opperator + * + * @param Complex $cpl Value to sub + * + * @return Complex + * + * @since 1.0.0 + */ + private function subComplex(Complex $cpl) : Complex + { + return new self($this->re - $cpl->re(), $this->im - $cpl->im()); + } + + /** + * Sub opperator + * + * @param mixed $val Value to sub + * + * @return Complex + * + * @since 1.0.0 + */ + private function subScalar($val) : Complex + { + return new self($this->re - $val, $this->im); + } + + /** + * Mult opperator + * + * @param mixed $value Value to mult + * + * @return Complex + * + * @throws \InvalidArgumentException + * + * @since 1.0.0 + */ + public function mult($value) : Complex + { + if (is_numeric($value)) { + return $this->multScalar($value); + } elseif ($value instanceof Complex) { + return $this->multComplex($value); + } + + throw new \InvalidArgumentException(); + } + + /** + * Mult opperator + * + * @param Complex $cpl Value to mult + * + * @return Complex + * + * @since 1.0.0 + */ + private function multComplex(Complex $cpl) : Complex + { + return new self( + $this->re * $cpl->re() - $this->im * $cpl->im(), + $this->re * $cpl->im() + $this->im * $cpl->re() + ); + } + + /** + * Mult opperator + * + * @param mixed $val Value to mult + * + * @return Complex + * + * @since 1.0.0 + */ + private function multScalar($val) : Complex + { + return new self($this->re * $val, $this->im * $val); + } + + /** + * Div opperator + * + * @param mixed $value Value to div + * + * @return Complex + * + * @throws \InvalidArgumentException + * + * @since 1.0.0 + */ + public function div($value) : Complex + { + if (is_numeric($value)) { + return $this->divScalar($value); + } elseif ($value instanceof Complex) { + return $this->divComplex($value); + } + + throw new \InvalidArgumentException(); + } + + /** + * Div opperator + * + * @param Complex $cpl Value to div + * + * @return Complex + * + * @since 1.0.0 + */ + private function divComplex(Complex $cpl) : Complex + { + return new self( + ($this->re * $cpl->re() + $this->im * $cpl->im()) / ($cpl->re() ** 2 + $cpl->im() ** 2), + ($this->im * $cpl->re() - $this->re * $cpl->im()) / ($cpl->re() ** 2 + $cpl->im() ** 2) + ); + } + + /** + * Div opperator + * + * @param mixed $val Value to div + * + * @return Complex + * + * @since 1.0.0 + */ + private function divScalar($val) : Complex + { + return new self($this->re / $val, $this->im / $val); + } + + /** + * Render complex number + * + * @param int $precision Output precision + * + * @return string + * + * @since 1.0.0 + */ + public function render(int $precision = 2) : string + { + return ($this->re !== 0 ? number_format($this->re, $precision) : '') + . ($this->im > 0 && $this->re !== 0 ? ' +' : '') + . ($this->im < 0 && $this->re !== 0 ? ' -' : '') + . ($this->im !== 0 ? ( + ($this->re !== 0 ? ' ' : '') . number_format( + ($this->im < 0 && $this->re === 0 ? $this->im : abs($this->im)), $precision + ) . 'i' + ) : ''); } } diff --git a/Math/Stochastic/Distribution/BernoulliDistribution.php b/Math/Stochastic/Distribution/BernoulliDistribution.php index 350e8fe0c..5d26ac9f7 100644 --- a/Math/Stochastic/Distribution/BernoulliDistribution.php +++ b/Math/Stochastic/Distribution/BernoulliDistribution.php @@ -42,9 +42,30 @@ class BernoulliDistribution return 1 - $p; } elseif ($k === 1) { return $p; - } else { - throw new \Exception('wrong parameter'); } + + throw new \Exception('wrong parameter'); + } + + /** + * Get cummulative distribution function. + * + * @param float $p Value p + * @param float $k Value k + * + * @return float + * + * @since 1.0.0 + */ + public static function getCdf(float $p, float $k) : float + { + if ($k < 0) { + return 0; + } elseif ($k >= 1) { + return 1; + } + + return 1 - $p; } /** @@ -62,9 +83,9 @@ class BernoulliDistribution return 0; } elseif ($p > 0.5) { return 1; - } else { - return 0; } + + return 0; } /** @@ -96,9 +117,9 @@ class BernoulliDistribution return 0.5; } elseif ($p > 0.5) { return 1; - } else { - return 0; } + + return 0; } /** @@ -144,6 +165,20 @@ class BernoulliDistribution return (1 - 2 * $p) / sqrt($p * (1 - $p)); } + /** + * Get entropy. + * + * @param float $p Value p + * + * @return float + * + * @since 1.0.0 + */ + public static function getEntropy(float $p) : float + { + return -(1 - $p) * log(1 - $p) - $p * log($p); + } + /** * Get Fisher information. * diff --git a/Math/Stochastic/Distribution/BinomialDistribution.php b/Math/Stochastic/Distribution/BinomialDistribution.php index b44d78871..83bb27c59 100644 --- a/Math/Stochastic/Distribution/BinomialDistribution.php +++ b/Math/Stochastic/Distribution/BinomialDistribution.php @@ -41,15 +41,7 @@ class BinomialDistribution */ public static function getMode(int $n, float $p) : float { - if (($temp = ($n + 1) * $p) === 0 || !is_int($temp)) { - return floor($temp); - } elseif ($temp >= 1 && $temp <= $n) { - return $temp; - } elseif ($temp === $n + 1) { - return $n; - } else { - throw new \Exception('Unexpected Values'); - } + return floor(($n + 1) * $p); } /** @@ -128,7 +120,7 @@ class BinomialDistribution { $sum = 0.0; - for ($i = 0; $i < $x; ++$i) { + for ($i = 1; $i < $x; ++$i) { $sum += self::getPmf($n, $i, $p); } diff --git a/Math/Stochastic/Distribution/ChiSquaredDistribution.php b/Math/Stochastic/Distribution/ChiSquaredDistribution.php index 81989b602..76cd712aa 100644 --- a/Math/Stochastic/Distribution/ChiSquaredDistribution.php +++ b/Math/Stochastic/Distribution/ChiSquaredDistribution.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace phpOMS\Math\Stochastic\Distribution; use phpOMS\Math\Functions\Functions; +use phpOMS\Math\Functions\Gamma; /** * Chi squared distribution. @@ -157,7 +158,7 @@ class ChiSquaredDistribution throw new \Exception('Out of bounds'); } - return 1 / (pow(2, $df / 2) * (Functions::getGammaInteger((int) ($df / 2)))) * pow($x, $df / 2 - 1) * exp(-$x / 2); + return 1 / (pow(2, $df / 2) * Gamma::lanczosApproximationReal(($df / 2))) * pow($x, $df / 2 - 1) * exp(-$x / 2); } /** diff --git a/Message/Http/BrowserType.php b/Message/Http/BrowserType.php index c63828248..567611bbc 100644 --- a/Message/Http/BrowserType.php +++ b/Message/Http/BrowserType.php @@ -39,4 +39,5 @@ abstract class BrowserType extends Enum /* public */ const KONQUEROR = 'konqueror'; /* Konqueror */ /* public */ const HANDHELD = 'mobile'; /* Handheld Browser */ /* public */ const BLINK = 'blink'; /* Blink Browser */ + /* public */ const UNKNOWN = 'unknown'; } diff --git a/Message/Http/Header.php b/Message/Http/Header.php index cd102861e..35408560b 100644 --- a/Message/Http/Header.php +++ b/Message/Http/Header.php @@ -145,7 +145,9 @@ class Header extends HeaderAbstract public static function getAllHeaders() : array { if (function_exists('getallheaders')) { + // @codeCoverageIgnoreStart return getallheaders(); + // @codeCoverageIgnoreEnd } $headers = []; @@ -228,6 +230,7 @@ class Header extends HeaderAbstract * @return void * * @since 1.0.0 + * @codeCoverageIgnore */ public function push() /* : void */ { diff --git a/Message/Http/OSType.php b/Message/Http/OSType.php index dc520b39c..cb8a09fda 100644 --- a/Message/Http/OSType.php +++ b/Message/Http/OSType.php @@ -51,4 +51,5 @@ abstract class OSType extends Enum /* public */ const ANDROID = 'android'; /* Android */ /* public */ const BLACKBERRY = 'blackberry'; /* Blackberry */ /* public */ const MOBILE = 'webos'; /* Mobile */ + /* public */ const UNKNOWN = 'unknown'; } diff --git a/Message/Http/Request.php b/Message/Http/Request.php index fc20ced02..bf1fe9973 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -49,7 +49,7 @@ class Request extends RequestAbstract * @var string * @since 1.0.0 */ - private $browser = BrowserType::CHROME; + private $browser = null; /** * OS type. @@ -57,7 +57,7 @@ class Request extends RequestAbstract * @var string * @since 1.0.0 */ - private $os = OSType::LINUX; + private $os = null; /** * Request information. @@ -291,6 +291,8 @@ class Request extends RequestAbstract return $this->browser; } } + + $this->browser = BrowserType::UNKNOWN; } return $this->browser; @@ -330,6 +332,8 @@ class Request extends RequestAbstract return $this->os; } } + + $this->os = OSType::UNKNOWN; } return $this->os; diff --git a/Message/RequestAbstract.php b/Message/RequestAbstract.php index d0b225ab1..b6a3902e7 100644 --- a/Message/RequestAbstract.php +++ b/Message/RequestAbstract.php @@ -101,15 +101,6 @@ abstract class RequestAbstract implements MessageInterface */ protected $header = null; - /** - * Constructor. - * - * @since 1.0.0 - */ - public function __construct() - { - } - /** * Get request uri. * diff --git a/Validation/ModelValidationTrait.php b/Validation/ModelValidationTrait.php deleted file mode 100644 index b5a1305b8..000000000 --- a/Validation/ModelValidationTrait.php +++ /dev/null @@ -1,93 +0,0 @@ -{$name} = $var; - } - - /** @noinspection PhpUnusedPrivateMethodInspection */ - /** - * Validate member variable. - * - * @param mixed $var Variable to validate - * @param string $name Name of the variable - * - * @return bool - * - * @since 1.0.0 - */ - protected function isValid($var, $name) : bool - { - /** @noinspection PhpUndefinedFieldInspection */ - if (!isset(self::${$name . '_validate'})) { - return true; - } - - /** @noinspection PhpUndefinedFieldInspection */ - return Validator::isValid($var, self::$validation[$name]); - } - - /** - * Set validated member variable. - * - * @param mixed $var Variable to validate - * @param string $name Name of the variable - * - * @return bool - * - * @throws \Exception - * - * @since 1.0.0 - */ - protected function setValidation($var, $name) /* : void */ - { - /** @noinspection PhpUndefinedFieldInspection */ - if (!isset(self::${$name . '_validate'}) || Validator::isValid($var, self::$validation[$name]) === true) { - $this->{$name} = $var; - } else { - throw new \Exception('Invalid data for variable ' . $name); - } - } -} diff --git a/Views/ViewAbstract.php b/Views/ViewAbstract.php index 8cbde67f1..310f40d40 100644 --- a/Views/ViewAbstract.php +++ b/Views/ViewAbstract.php @@ -43,15 +43,6 @@ abstract class ViewAbstract implements \Serializable */ protected $views = []; - /** - * Constructor. - * - * @since 1.0.0 - */ - public function __construct() - { - } - /** * Sort views by order. * diff --git a/tests/Business/Finance/StockBondsTest.php b/tests/Business/Finance/StockBondsTest.php index 57987a0b8..bf5e09857 100644 --- a/tests/Business/Finance/StockBondsTest.php +++ b/tests/Business/Finance/StockBondsTest.php @@ -58,6 +58,14 @@ class StockBondsTest extends \PHPUnit\Framework\TestCase self::assertEquals(0.10355, StockBonds::getHoldingPeriodReturn($r), '', 0.01); } + public function testTaxEquivalentYield() + { + $free = 0.15; + $rate = 0.05; + + self::assertEquals(0.15789, StockBonds::getTaxEquivalentYield($free, $rate), '', 0.01); + } + public function testNetAssetValue() { $assets = 1000; diff --git a/tests/Math/Functions/FunctionsTest.php b/tests/Math/Functions/FunctionsTest.php index 9fa493b10..96ffae6ca 100644 --- a/tests/Math/Functions/FunctionsTest.php +++ b/tests/Math/Functions/FunctionsTest.php @@ -19,8 +19,6 @@ class FunctionsTest extends \PHPUnit\Framework\TestCase { public function testFactorial() { - self::assertEquals(Functions::fact(4), Functions::getGammaInteger(5)); - self::assertEquals(120, Functions::fact(5)); self::assertEquals(39916800, Functions::fact(11)); diff --git a/tests/Math/Functions/GammaTest.php b/tests/Math/Functions/GammaTest.php new file mode 100644 index 000000000..6c34876a8 --- /dev/null +++ b/tests/Math/Functions/GammaTest.php @@ -0,0 +1,46 @@ +re()); + self::assertEquals(0, $cpl->im()); + self::assertEquals('', $cpl->render()); + } + + public function testSetGet() + { + $cpl = new Complex(1, 2); + self::assertEquals(1, $cpl->re()); + self::assertEquals(2, $cpl->im()); + } + + public function testBasics() + { + $cpl1 = new Complex(2, 3); + $cpl2 = new Complex(3, 4); + + self::assertEquals('5.00 + 7.00i', $cpl1->add($cpl2)->render()); + self::assertEquals('6.00 + 3.00i', $cpl1->add(4)->render()); + + self::assertEquals('-1.00 - 1.00i', $cpl1->sub($cpl2)->render()); + self::assertEquals('-2.00 + 3.00i', $cpl1->sub(4)->render()); + + self::assertEquals('-6.00 + 17.00i', $cpl1->mult($cpl2)->render()); + self::assertEquals('8.00 + 12.00i', $cpl1->mult(4)->render()); + + self::assertEquals('0.72 + 0.04i', $cpl1->div($cpl2)->render(2)); + self::assertEquals('0.50 + 0.75i', $cpl1->div(4)->render(2)); + } + + public function testSpecial() + { + $cpl = new Complex(4, 3); + + self::assertEquals('4 - 3i', $cpl->conjugate()->render(0)); + self::assertEquals('0.16 - 0.12i', $cpl->reciprocal()->render(2)); + + self::assertEquals('7.00 + 24.00i', $cpl->square()->render()); + self::assertEquals(5, $cpl->abs(), '', 0.01); + + self::assertEquals('2.12 + 0.71i', $cpl->sqrt()->render()); + + $cpl2 = new Complex(-1, 3); + self::assertEquals('1.04 + 1.44i', $cpl2->sqrt()->render()); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidAdd() + { + $cpl = new Complex(4, 3); + $cpl->add(true); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidSub() + { + $cpl = new Complex(4, 3); + $cpl->sub(true); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidMult() + { + $cpl = new Complex(4, 3); + $cpl->mult(true); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidDiv() + { + $cpl = new Complex(4, 3); + $cpl->div(true); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidPow() + { + $cpl = new Complex(4, 3); + $cpl->pow(true); } } diff --git a/tests/Math/Stochastic/Distribution/BernoulliDistributionTest.php b/tests/Math/Stochastic/Distribution/BernoulliDistributionTest.php index aafb94d58..f1e4ccce2 100644 --- a/tests/Math/Stochastic/Distribution/BernoulliDistributionTest.php +++ b/tests/Math/Stochastic/Distribution/BernoulliDistributionTest.php @@ -17,8 +17,84 @@ use phpOMS\Math\Stochastic\Distribution\BernoulliDistribution; class BernoulliDistributionTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + public function testPmf() { - self::markTestIncomplete(); + self::assertEquals(0.3, BernoulliDistribution::getPmf(0.7, 0), '', 0.01); + self::assertEquals(0.7, BernoulliDistribution::getPmf(0.7, 1), '', 0.01); + } + + public function testMode() + { + self::assertEquals(1, BernoulliDistribution::getMode(0.7), '', 0.01); + self::assertEquals(0, BernoulliDistribution::getMode(0.5), '', 0.01); + self::assertEquals(0, BernoulliDistribution::getMode(0.3), '', 0.01); + } + + public function testMean() + { + self::assertEquals(0.4, BernoulliDistribution::getMean(0.4), '', 0.01); + } + + public function testCdf() + { + self::assertEquals(0, BernoulliDistribution::getCdf(0.4, -2), '', 0.01); + self::assertEquals(1, BernoulliDistribution::getCdf(0.4, 2), '', 0.01); + self::assertEquals(0.3, BernoulliDistribution::getCdf(0.7, 0.4), '', 0.01); + } + + public function testMedian() + { + self::assertEquals(0.5, BernoulliDistribution::getMedian(0.5), '', 0.01); + self::assertEquals(1, BernoulliDistribution::getMedian(0.7), '', 0.01); + self::assertEquals(0, BernoulliDistribution::getMedian(0.3), '', 0.01); + } + + public function testVariance() + { + $p = 0.3; + $q = 1 - $p; + + self::assertEquals($p * $q, BernoulliDistribution::getVariance($p), '', 0.01); + } + + public function testSkewness() + { + $p = 0.3; + $q = 1 - $p; + + self::assertEquals((1 - 2 * $p) / sqrt($p * $q), BernoulliDistribution::getSkewness($p), '', 0.01); + } + + public function testExKurtosis() + { + $p = 0.3; + $q = 1 - $p; + + self::assertEquals((1 - 6 * $p * $q) / ($p * $q), BernoulliDistribution::getExKurtosis($p), '', 0.01); + } + + public function testEntropy() + { + $p = 0.3; + $q = 1 - $p; + + self::assertEquals(-$q * log($q) - $p * log($p), BernoulliDistribution::getEntropy($p), '', 0.01); + } + + public function testMgf() + { + $p = 0.3; + $q = 1 - $p; + $t = 2; + + self::assertEquals($q + $p * exp($t), BernoulliDistribution::getMgf($p, $t), '', 0.01); + } + + public function testFisherInformation() + { + $p = 0.3; + $q = 1 - $p; + + self::assertEquals(1 / ($p * $q), BernoulliDistribution::getFisherInformation($p), '', 0.01); } } diff --git a/tests/Math/Stochastic/Distribution/BinomialDistributionTest.php b/tests/Math/Stochastic/Distribution/BinomialDistributionTest.php index 0bff28fab..0debad305 100644 --- a/tests/Math/Stochastic/Distribution/BinomialDistributionTest.php +++ b/tests/Math/Stochastic/Distribution/BinomialDistributionTest.php @@ -17,8 +17,86 @@ use phpOMS\Math\Stochastic\Distribution\BinomialDistribution; class BinomialDistributionTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + public function testPmf() { - self::markTestIncomplete(); + $p = 0.4; + $n = 20; + $k = 7; + + self::assertEquals(0.1659, BinomialDistribution::getPmf($n, $k, $p), '', 0.01); + } + + public function testCdf() + { + $p = 0.4; + $n = 20; + $k = 7; + + self::assertEquals(0.25, BinomialDistribution::getCdf($n, $k, $p), '', 0.01); + } + + public function testMean() + { + $n = 20; + $p = 0.4; + + self::assertEquals($n * $p, BinomialDistribution::getMean($n, $p), '', 0.01); + } + + public function testMedian() + { + $n = 20; + $p = 0.4; + + self::assertEquals(floor($n * $p), BinomialDistribution::getMedian($n, $p), '', 0.01); + } + + public function testMode() + { + $n = 20; + $p = 0.4; + + self::assertEquals(floor(($n + 1) * $p), BinomialDistribution::getMode($n, $p), '', 0.01); + } + + public function testVariance() + { + $n = 20; + $p = 0.4; + + self::assertEquals($n * $p * (1 - $p), BinomialDistribution::getVariance($n, $p), '', 0.01); + } + + public function testSkewness() + { + $n = 20; + $p = 0.4; + + self::assertEquals((1 - 2 * $p) / sqrt($n * $p * (1 - $p)), BinomialDistribution::getSkewness($n, $p), '', 0.01); + } + + public function testExKurtosis() + { + $n = 20; + $p = 0.4; + + self::assertEquals((1 - 6 * $p * (1 - $p)) / ($n * $p * (1 - $p)), BinomialDistribution::getExKurtosis($n, $p), '', 0.01); + } + + public function testMgf() + { + $n = 20; + $p = 0.4; + $t = 3; + + self::assertEquals((1 - $p + $p * exp($t)) ** $n, BinomialDistribution::getMgf($n, $t, $p), '', 0.01); + } + + public function testFisherInformation() + { + $n = 20; + $p = 0.4; + + self::assertEquals($n / ($p * (1 - $p)), BinomialDistribution::getFisherInformation($n, $p), '', 0.01); } } diff --git a/tests/Math/Stochastic/Distribution/CauchyDistributionTest.php b/tests/Math/Stochastic/Distribution/CauchyDistributionTest.php index 45cce8070..75f3c6f5f 100644 --- a/tests/Math/Stochastic/Distribution/CauchyDistributionTest.php +++ b/tests/Math/Stochastic/Distribution/CauchyDistributionTest.php @@ -17,8 +17,27 @@ use phpOMS\Math\Stochastic\Distribution\CauchyDistribution; class CauchyDistributionTest extends \PHPUnit\Framework\TestCase { - public function testPlaceholder() + public function testMedianMode() { - self::markTestIncomplete(); + self::assertEquals(3.2, CauchyDistribution::getMedian(3.2)); + self::assertEquals(3.2, CauchyDistribution::getMode(3.2)); + } + + public function testPdf() + { + $x = 1; + $x0 = 0.5; + $gamma = 2; + + self::assertEquals(0.14979, CauchyDistribution::getPdf($x, $x0, $gamma), '', 0.01); + } + + public function testCdf() + { + $x = 1; + $x0 = 0.5; + $gamma = 2; + + self::assertEquals(0.57798, CauchyDistribution::getCdf($x, $x0, $gamma), '', 0.01); } } diff --git a/tests/Message/Http/BrowserTypeTest.php b/tests/Message/Http/BrowserTypeTest.php index 7e79f4622..bed0159ef 100644 --- a/tests/Message/Http/BrowserTypeTest.php +++ b/tests/Message/Http/BrowserTypeTest.php @@ -19,7 +19,7 @@ class BrowserTypeTest extends \PHPUnit\Framework\TestCase { public function testEnums() { - self::assertEquals(11, BrowserType::count()); + self::assertEquals(12, BrowserType::count()); self::assertEquals(BrowserType::getConstants(), array_unique(BrowserType::getConstants())); self::assertEquals('msie', BrowserType::IE); @@ -33,5 +33,6 @@ class BrowserTypeTest extends \PHPUnit\Framework\TestCase self::assertEquals('konqueror', BrowserType::KONQUEROR); self::assertEquals('mobile', BrowserType::HANDHELD); self::assertEquals('blink', BrowserType::BLINK); + self::assertEquals('unknown', BrowserType::UNKNOWN); } } diff --git a/tests/Message/Http/OSTypeTest.php b/tests/Message/Http/OSTypeTest.php index 75a10f938..092b71c56 100644 --- a/tests/Message/Http/OSTypeTest.php +++ b/tests/Message/Http/OSTypeTest.php @@ -19,7 +19,7 @@ class OSTypeTest extends \PHPUnit\Framework\TestCase { public function testEnums() { - self::assertEquals(23, count(OSType::getConstants())); + self::assertEquals(24, count(OSType::getConstants())); self::assertEquals(OSType::getConstants(), array_unique(OSType::getConstants())); } } diff --git a/tests/Message/Http/RequestTest.php b/tests/Message/Http/RequestTest.php index 6f15f67af..e7f48ecb6 100644 --- a/tests/Message/Http/RequestTest.php +++ b/tests/Message/Http/RequestTest.php @@ -29,10 +29,12 @@ class RequestTest extends \PHPUnit\Framework\TestCase { $request = new Request(); + $_SERVER['HTTP_USER_AGENT'] = OSType::UNKNOWN . BrowserType::UNKNOWN; + self::assertEquals('en', $request->getHeader()->getL11n()->getLanguage()); self::assertFalse($request->isMobile()); - self::assertEquals(BrowserType::CHROME, $request->getBrowser()); - self::assertEquals(OSType::LINUX, $request->getOS()); + self::assertEquals(BrowserType::UNKNOWN, $request->getBrowser()); + self::assertEquals(OSType::UNKNOWN, $request->getOS()); self::assertEquals('127.0.0.1', $request->getOrigin()); self::assertFalse($request->isHttps()); self::assertEquals([], $request->getHash());