mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-21 05:48:41 +00:00
Add calculation for sample size
This commit is contained in:
parent
59f43a6a87
commit
15936c3a1a
|
|
@ -25,9 +25,11 @@ namespace phpOMS\Math\Stochastic\Distribution;
|
|||
final class NormalDistribution
|
||||
{
|
||||
/**
|
||||
* Chi square table.
|
||||
* Normal table.
|
||||
*
|
||||
* @var array<int, array<string, float>>
|
||||
* Z-Score.
|
||||
*
|
||||
* @var array<string, float>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public const TABLE = [
|
||||
|
|
@ -35,6 +37,39 @@ final class NormalDistribution
|
|||
'0.85' => 1.44, '0.90' => 1.64, '0.95' => 1.96, '0.96' => 2.05, '0.97' => 2.17, '0.98' => 2.33, '0.99' => 2.58,
|
||||
];
|
||||
|
||||
/**
|
||||
* Calculate the sample size
|
||||
*
|
||||
* @param float $zScore Z-Score
|
||||
* @param float $errorMargin Error margin
|
||||
* @param int $populationSize Population size
|
||||
* @param float $populationProportion Proportion of the population (percentage)
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function getSampleSizeFromPopulation(float $zScore, float $errorMargin, int $populationSize, float $populationProportion) : float
|
||||
{
|
||||
return self::getSampleSizeFromInfinitePopulation($zScore, $errorMargin, $populationProportion) / (1 + $zScore ** 2 * $populationProportion * (1 - $populationProportion) / ($errorMargin ** 2 * $populationSize));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the sample size
|
||||
*
|
||||
* @param float $zScore Z-Score
|
||||
* @param float $errorMargin Error margin
|
||||
* @param float $populationProportion Proportion of the population (percentage)
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function getSampleSizeFromInfinitePopulation(float $zScore, float $errorMargin, float $populationProportion) : float
|
||||
{
|
||||
return ($zScore ** 2) * $populationProportion * (1 - $populationProportion) / ($errorMargin ** 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get probability density function.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -83,4 +83,14 @@ class NormalDistributionTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
self::assertEquals($sig, NormalDistribution::getStandardDeviation($sig));
|
||||
}
|
||||
|
||||
public function testSampleSizeCalculation() : void
|
||||
{
|
||||
self::assertEqualsWithDelta(277.54, NormalDistribution::getSampleSizeFromPopulation(NormalDistribution::TABLE['0.95'], 0.05, 1000, 0.5), 0.01);
|
||||
}
|
||||
|
||||
public function testSampleSizeInfiniteCalculation() : void
|
||||
{
|
||||
self::assertEqualsWithDelta(384.16, NormalDistribution::getSampleSizeFromInfinitePopulation(NormalDistribution::TABLE['0.95'], 0.05, 0.5), 0.01);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user