More tests (math tests) and fixes

This commit is contained in:
Dennis Eichhorn 2018-02-25 16:12:31 +01:00
parent 3aedae39e4
commit 2305380945
23 changed files with 861 additions and 173 deletions

View File

@ -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.
*

122
Math/Functions/Gamma.php Normal file
View File

@ -0,0 +1,122 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package phpOMS\Math\Functions
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\Math\Functions;
/**
* Gamma function
*
* @package phpOMS\Math\Functions
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Gamma
{
/**
* approximation values.
*
* @var float[]
* @since 1.0.0
*/
/* private */ const LANCZOSAPPROXIMATION = [
0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059,
12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7
];
/**
* Calculate gamma with Lanczos approximation
*
* @param mixed $z Value
*
* @return float
*
* @since 1.0.0
*/
public static function lanczosApproximationReal($z) : float
{
if ($z < 0.5) {
return M_PI / (sin(M_PI * $z) * self::lanczosApproximationReal(1 - $z));
}
$z -= 1;
$a = self::LANCZOSAPPROXIMATION[0];
$t = $z + 7.5;
for ($i = 1; $i < 9; ++$i) {
$a += self::LANCZOSAPPROXIMATION[$i] / ($z + $i);
}
return sqrt(2 * M_PI) * pow($t, $z + 0.5) * exp(-$t) * $a;
}
/**
* Calculate gamma with Stirling approximation
*
* @param mixed $x Value
*
* @return float
*
* @since 1.0.0
*/
public static function stirlingApproximation($x) : float
{
return sqrt(2.0 * M_PI / $x) * pow($x / M_E, $x);
}
/**
* Calculate gamma with Spouge approximation
*
* @param mixed $z Value
*
* @return float
*
* @since 1.0.0
*/
public static function spougeApproximation($z) : float
{
$k1_fact = 1.0;
$c = [sqrt(2.0 * M_PI)];
for ($k = 1; $k < 12; ++$k) {
$c[$k] = exp(12 - $k) * pow(12 - $k, $k - 0.5) / $k1_fact;
$k1_fact *= -$k;
}
$accm = $c[0];
for ($k = 1; $k < 12; ++$k) {
$accm += $c[$k] / ($z + $k);
}
$accm *= exp(-$z - 12) * pow($z + 12, $z + 0.5);
return $accm / $z;
}
/**
* Calculate gamma function value.
*
* Example: (7)
*
* @param int $k Variable
*
* @return int
*
* @since 1.0.0
*/
public static function getGammaInteger(int $k) : int
{
return Functions::fact($k - 1);
}
}

View File

@ -4,7 +4,7 @@
*
* PHP Version 7.1
*
* @package TBD
* @package phpOMS\Math\Number
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
@ -14,45 +14,383 @@ declare(strict_types=1);
namespace phpOMS\Math\Number;
/**
* Complex number class.
*
* @package phpOMS\Math\Number
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Complex
{
private $real = null;
/**
* Real part.
*
* @var mixed
* @since 1.0.0
*/
private $re = null;
/**
* Imaginary part.
*
* @var mixed
* @since 1.0.0
*/
private $im = null;
public function getReal()
/**
* Constructor.
*
* @param mixed $re Real part
* @param mixed $im Imaginary part
*
* @since 1.0.0
*/
public function __construct($re = 0, $im = 0)
{
$this->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'
) : '');
}
}

View File

@ -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.
*

View File

@ -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);
}

View File

@ -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);
}
/**

View File

@ -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';
}

View File

@ -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 */
{

View File

@ -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';
}

View File

@ -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;

View File

@ -101,15 +101,6 @@ abstract class RequestAbstract implements MessageInterface
*/
protected $header = null;
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
}
/**
* Get request uri.
*

View File

@ -1,93 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package phpOMS\Validation
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\Validation;
/**
* Model validation trait.
*
* @package phpOMS\Validation
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
trait ModelValidationTrait
{
/** @noinspection PhpUnusedPrivateMethodInspection */
/**
* Set variable without validating it.
*
* @param mixed $var Variable to set
* @param string $name Name of the variable
*
* @return void
*
* @throws \Exception
*
* @since 1.0.0
*/
public function setForce($var, $name) /* : void */
{
if (!property_exists($this, $var)) {
throw new \Exception('Unknown property.');
}
$this->{$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);
}
}
}

View File

@ -43,15 +43,6 @@ abstract class ViewAbstract implements \Serializable
*/
protected $views = [];
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
}
/**
* Sort views by order.
*

View File

@ -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;

View File

@ -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));

View File

@ -0,0 +1,46 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\Math\Functions;
use phpOMS\Math\Functions\Functions;
use phpOMS\Math\Functions\Gamma;
class GammaTest extends \PHPUnit\Framework\TestCase
{
public function testFactorial()
{
self::assertEquals(Functions::fact(4), Gamma::getGammaInteger(5));
}
public function testApproximations()
{
$stirling = [
2.15697602, 1.20285073, 0.92213701, 0.83974270, 0.85919025, 0.95950218, 1.14910642, 1.45849038, 1.94540320, 2.70976382,
];
$spouge = [
2.67893853, 1.35411794, 1.00000000, 0.89297951, 0.90274529, 1.00000000, 1.19063935, 1.50457549, 2.00000000, 2.77815848,
];
$gsl = [
2.67893853, 1.35411794, 1.00000000, 0.89297951, 0.90274529, 1.00000000, 1.19063935, 1.50457549, 2.00000000, 2.77815848,
];
for ($i = 1; $i <= 10; $i++) {
self::assertEquals($stirling[$i - 1], Gamma::stirlingApproximation($i / 3), '', 0.01);
self::assertEquals($spouge[$i - 1], Gamma::spougeApproximation($i / 3), '', 0.01);
self::assertEquals($gsl[$i - 1], Gamma::lanczosApproximationReal($i / 3), '', 0.01);
}
}
}

View File

@ -17,8 +17,97 @@ use phpOMS\Math\Number\Complex;
class ComplexTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
public function testDefault()
{
self::markTestIncomplete();
$cpl = new Complex();
self::assertEquals(0, $cpl->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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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()));
}
}

View File

@ -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());