• PHP Video Tutorials

PHP - Stats Dens Gamma() Function



Definition and Usage

The stats_dens_gamma() function is a probability density function of the gamma distribution.

Syntax

float stats_dens_gamma( float $x, float $shape, float $scale )

Parameters

Sr.No Parameter Description
1

x

The value at which the probability density is calculated

2

shape

The shape parameter of the distribution

3

scale

The scale parameter of the distribution

Return Values

The stats_dens_gamma() function can return the probability density at x, where the random variable can follow the gamma distribution of which shape parameter is the shape and scale parameter is the scale, or false for failure.

Dependencies

This function was first introduced in statistics extension (PHP 4.0.0 and PEAR 1.4.0). We have used latest release of stats-2.0.3 (PHP 7.0.0 or newer and PEAR 1.4.0 or newer) for this tutorial.

Example

In the following example, we compute probability density for each x.

<?php
   // check for each x
   foreach (range(0.5, 2, 0.5) as $x) {
      var_dump(round(stats_dens_gamma($x, 1, 1), 6));
      echo "<br>";
   }
?>

Output

This will produce following result −

  float(0.606531)
  float(0.367879)
  float(0.22313)
  float(0.135335)

Example

In the following example, we compute probability density for each shape.

<?php
   // check for each shape
   foreach (range(0.5, 2, 0.5) as $shape) {
      var_dump(round(stats_dens_gamma(1, $shape, 1), 6));
      echo "<br>";
   }
?>

Output

This will produce following result −

  float(0.207554)
  float(0.367879)
  float(0.415107)
  float(0.367879)

Example

In the following example, we compute probability density for each dfr2.

<?php
   // check for each scale
   foreach (range(0.5, 2, 0.5) as $scale) {
      var_dump(round(stats_dens_gamma(1, 1, $scale), 6));
      echo "<br>";
   }
?>

Output

This will produce following result −

  float(0.270671)
  float(0.367879)
  float(0.342278)
  float(0.303265)

Example

Following is an error case. In the following example, we pass scale = 0. A warning is displayed in logs.

<?php
   // error cases
   var_dump(stats_dens_exponential(1, 0)); // scale = 0
?>

Output

This will produce following result and a warning in logs PHP Warning: stats_dens_gamma(): scale == 0.0

bool(false)
Advertisements