Monday, June 25, 2012

standard deviation php

standard deviation php

Standard Deviation

The Standard Deviation is a measure of how spread out numbers are.Its symbol is σ (the greek letter sigma). By using php you can find Standard Deviation in this example,I have created only for 10 numbers you expand it to any numbers you need.Here example 1 for population and example 2 is for sample.
example 1
standard deviation = 21.282856951077
Values


<?php
$num1=$_POST['val1'];
$num2=$_POST['val2'];
$num3=$_POST['val3'];
$num4=$_POST['val4'];
$num5=$_POST['val5'];
$num6=$_POST['val6'];
$num7=$_POST['val7'];
$num8=$_POST['val8'];
$num9=$_POST['val9'];
$num10=$_POST['val10'];
function standard_deviation_population ($a)
{
 
  $the_standard_deviation = 0.0;
  $the_variance = 0.0;
  $the_mean = 0.0;
  $the_array_sum = array_sum($a); //sum the elements
  $number_elements = count($a); //count the number of elements


  $the_mean = $the_array_sum / $number_elements;


  for ($i = 0; $i < $number_elements; $i++)
  {

    $the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
  }

  $the_variance = $the_variance / $number_elements;


  $the_standard_deviation = pow( $the_variance, 0.5);


  return $the_standard_deviation;
}
$a = array($num1, $num2, $num3, $num4, $num5, $num6,$num7,$num8,$num9,$num10);

$standard_deviation = standard_deviation_population ($a);

echo "standard_deviation = $standard_deviation<br>";
?>
<table bgcolor=#d38a2a>
<form name="calc" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr bgcolor=#ffffff><td>Values</td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val1"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val2"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val3"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val4"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val5"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val6"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val7"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val8"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val9"></td></tr>
<tr bgcolor=#ffffff><td><input type="text" name="val10"></td></tr>
<tr bgcolor=#ffffff><tr bgcolor=#ffffff><td><input type="submit" vaue="Calculate"></td></tr>
</form>
</table>

Example2 

<?php
function standard_deviation_sample ($a)
{

  $the_standard_deviation = 0.0;
  $the_variance = 0.0;
  $the_mean = 0.0;
  $the_array_sum = array_sum($a);
  $number_elements = count($a);


  $the_mean = $the_array_sum / $number_elements;


  for ($i = 0; $i < $number_elements; $i++)
  {

    $the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
  }

  $the_variance = $the_variance / ($number_elements - 1.0);


  $the_standard_deviation = pow( $the_variance, 0.5);


  return $the_standard_deviation;
}

$a = array(0, 1, 2, 3, 4, 5);

$standard_deviation = standard_deviation_sample ($a);

echo "a[0] = $a[0]<br>";
echo "a[1] = $a[1]<br>";
echo "a[2] = $a[2]<br>";
echo "a[3] = $a[3]<br>";
echo "a[4] = $a[4]<br>";
echo "a[5] = $a[5]<br>";
echo "standard_deviation = $standard_deviation<br>";
?>

No comments:

Post a Comment