/** * __construct Function * * Constructor function for this regression model. Takes two arrays * of data that are parallel arrays of independent and dependent * observations. * * @param array $datax The series of independent variables * @param array $datay The series of dependent variables * @return LogarithmicRegression An object representing the regression model */ public function __construct($datax, $datay) { $logx = array(); foreach ($datax as $x) { $logx[] = log($x); } $this->r = \PHPStats\Stats::correlation($logx, $datay); $this->beta = \PHPStats\Stats::covariance($logx, $datay) / \PHPStats\Stats::variance($logx); $this->alpha = \PHPStats\Stats::average($datay) - $this->beta * \PHPStats\Stats::average($logx); }
/** * __construct Function * * Constructor function for this regression model. Takes two arrays * of data that are parallel arrays of independent and dependent * observations. * * @param array $datax The series of independent variables * @param array $datay The series of dependent variables * @return ExponentialRegression An object representing the regression model */ public function __construct(array $datax, array $datay) { $logy = array(); foreach ($datay as $y) { $logy[] = log($y); } $this->r = \PHPStats\Stats::correlation($datax, $logy); $logbeta = \PHPStats\Stats::covariance($datax, $logy) / \PHPStats\Stats::variance($datax); $logalpha = \PHPStats\Stats::average($logy) - $logbeta * \PHPStats\Stats::average($datax); $this->beta = exp($logbeta); $this->alpha = exp($logalpha); }
/** Returns the cumulative distribution function, the probability of getting the test value or something below it @param float $x The test value @param int $L The population size @param int $m The number of interesting elements in the population @param int $n The number of draws from the population @return float The probability */ static function getCdf($x, $L = 1, $m = 1, $n = 1) { $x = floor($x); $L = floor($L); $m = floor($m); $n = floor($n); if ($L >= 1 && $m >= 0 && $n >= 0) { $sum = 0; for ($i = 0; $i <= $x; $i++) { $sum += \PHPStats\Stats::combinations($m, $i) * \PHPStats\Stats::combinations($L - $m, $n - $i) / \PHPStats\Stats::combinations($L, $n); } return $sum; } else { return 0.0; } }
/** Returns the cumulative distribution function, the probability of getting the test value or something below it @param float $x The test value @param float $k Shape parameter @param float $theta Scale parameter @return float The probability */ static function getCdf($x, $k = 1, $theta = 1) { return \PHPStats\Stats::lowerGamma($k, $x / $theta) / \PHPStats\Stats::gamma($k); }
/** Returns the cumulative distribution function, the probability of getting the test value or something below it @param float $x The test value @param float $k Shape parameter @return float The probability */ static function getCdf($x, $k = 1) { return \PHPStats\Stats::lowerGamma($k / 2.0, $x / 2) / \PHPStats\Stats::gamma($k / 2.0); }
/** Returns the cumulative distribution function, the probability of getting the test value or something below it @param float $x The test value @param float $mu The location parameter. Default 0.0 @param float $variance The scale parameter. Default 1.0 @return float The probability */ static function getCdf($x, $mu = 0.0, $variance = 1.0) { return (1 + \PHPStats\Stats::erf(($x - $mu) / sqrt(2 * $variance))) / 2; }
/** Returns the probability mass function @param float $x The test value @param float $lambda The rate of events @return float The probability */ static function getPmf($x, $lambda = 1) { return exp(-$lambda) * pow($lambda, $x) / \PHPStats\Stats::factorial($x); }
/** * Returns the probability mass function * * @param float $x The test value * @param float $p The probability of success per trial * @param int $n The number of trials * @return float The probability */ public static function getPmf($x, $p = 0.5, $n = 1) { return \PHPStats\Stats::combinations($n, $x) * pow($p, $x) * pow(1 - $p, $n - $x); }
/** * __construct Function * * Constructor function for this regression model. Takes two arrays * of data that are parallel arrays of independent and dependent * observations. * * @param array $datax The series of independent variables * @param array $datay The series of dependent variables * @return LinearRegression An object representing the regression model */ public function __construct(array $datax, array $datay) { $this->beta = \PHPStats\Stats::covariance($datax, $datay) / \PHPStats\Stats::variance($datax); $this->alpha = \PHPStats\Stats::average($datay) - $this->beta * \PHPStats\Stats::average($datax); $this->r = \PHPStats\Stats::correlation($datax, $datay); }
/** Returns the cumulative distribution function, the probability of getting the test value or something below it @param float $x The test value @param float $alpha The minimum parameter. Default 0.0 @param float $beta The maximum parameter. Default 1.0 @return float The probability */ static function getCdf($x, $alpha = 1, $beta = 1) { return \PHPStats\Stats::regularizedIncompleteBeta($alpha, $beta, $x); }
/** Returns the cumulative distribution function, the probability of getting the test value or something below it @param float $x The test value @param float $df The degrees of freedeom. Default 1 @return float The probability */ static function getCdf($x, $df = 1) { $return = 1 - 0.5 * \PHPStats\Stats::regularizedIncompleteBeta($df / 2, 0.5, $df / (pow($x, 2) + $df)); //Valid only for $x > 0 if ($x < 0) { return 1 - $return; } elseif ($x == 0) { return 0.5; } else { return $return; } }
public function test_combinations() { $this->assertEquals(1, Stats::combinations(1, 1)); $this->assertEquals(2, Stats::combinations(2, 1)); $this->assertEquals(6, Stats::combinations(4, 2)); $this->assertEquals(1, Stats::combinations(5, 5)); $this->assertEquals(56, Stats::combinations(8, 5)); }