By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,773 Members | 1,779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,773 IT Pros & Developers. It's quick & easy.

fonction ecarttype en php

P: n/a
mat
Je l'ai cherché partout, je ne l'ai pas trouvé, alors je l'ai
écrite.

Si ça peut servir à d'autres ...

echo "Calcul d'un ecart type";
echo "<br>";

$datas = array("11","22","45","67","21","3","20");

$ecart = ecarttype($datas);

if ($ecart)
{
echo $ecart;
}
else
{
echo "Jeu de valeurs incohérent !";
}

function ecarttype($array, $nbdecimals=2) {

if (is_array($array))
{
//moyenne des valeurs
reset($array);
$somme=0;
$nbelement=count($array);
foreach ($array as $value) {
$somme += floatval($value);
}
$moyenne = $somme/$nbelement;
//numerateur
reset($array);
$sigma=0;
foreach ($array as $value) {
$sigma += pow((floatval($value)-$moyenne),2);
}
//denominateur = $nbelement
$ecarttype = sqrt($sigma/$nbelement);
return number_format($ecarttype, $nbdecimals);
}
else
{
return false;
}
}

Aug 29 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
mat wrote:
Je l'ai cherché partout, je ne l'ai pas trouvé, alors je l'ai
écrite.

Si ça peut servir à d'autres ...

<snip>

Thanks. Would be nice, if you could use English in future. Got it
translated with Google:
<Google translation
url="http://translate.google.com/translate?u=http://groups.google.com/group/comp.lang.php/msg/b4524aca9cf1ca5d&langpair=fr%7Cen&hl=en&ie=UTF8">

I sought it everywhere, I did not find it, then I have it
written.

If that can be used for others...

echo "Calculation of a standard deviation";
echo "< Br >";

$$datas = array("11", "22"," 45", "67"," 21 ", "3"," 20");

$$ecart = ecarttype($datas);

yew ($$ecart)
{
echo $$ecart;
}

else
{
echo "incoherent Set of values!";

}

function ecarttype($array, $$nbdecimals=2) {

yew (is_array($array))
{
// average of the values
reset($array);
$$somme=0;
$$nbelement=count($array);
foreach ($$array have $$value) {
$$somme + = floatval($value);
}
$$moyenne = $$somme/$nbelement;
// numerator
reset($array);
$$sigma=0;
foreach ($$array have $$value) {
$$sigma + = pow((floatval($value)-$moyenne),2);
}
// denominator = $$nbelement
$$ecarttype = sqrt($sigma/$nbelement);
return number_format($ecarttype, $$nbdecimals);
}
else
{
return false;
}
}
</Google translation>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Aug 30 '05 #2

P: n/a
On Tue, 30 Aug 2005 07:33:03 -0700, R. Rajesh Jeba Anbiah wrote:
mat wrote:
Je l'ai cherché partout, je ne l'ai pas trouvé, alors je l'ai
écrite.

Si ça peut servir Ã* d'autres ...

<snip>

Thanks. Would be nice, if you could use English in future. Got it
translated with Google:
<Google translation
url="http://translate.google.com/translate?u=http://groups.google.com/group/comp.lang.php/msg/b4524aca9cf1ca5d&langpair=fr%7Cen&hl=en&ie=UTF8">

I sought it everywhere, I did not find it, then I have it
written.

If that can be used for others...

echo "Calculation of a standard deviation";
echo "< Br >";

$$datas = array("11", "22"," 45", "67"," 21 ", "3"," 20");

$$ecart = ecarttype($datas);

yew ($$ecart)
{
echo $$ecart;
}

else
{
echo "incoherent Set of values!";

}

function ecarttype($array, $$nbdecimals=2) {

yew (is_array($array))
{
// average of the values
reset($array);
$$somme=0;
$$nbelement=count($array);
foreach ($$array have $$value) {
$$somme + = floatval($value);
}
$$moyenne = $$somme/$nbelement;
// numerator
reset($array);
$$sigma=0;
foreach ($$array have $$value) {
$$sigma + = pow((floatval($value)-$moyenne),2);
}
// denominator = $$nbelement
$$ecarttype = sqrt($sigma/$nbelement);
return number_format($ecarttype, $$nbdecimals);
}
else
{
return false;
}
}
</Google translation>

Also Note there are two calculations for standard deviation.
The first as shown above is called the "population" deviation and implies
that the sample used to calculate the deviation is the entire sample.

This is defined as sqrt( sum ((x - mean(x))**2) / N)

There is a weighted "sample" deviation calculation which implies that the
sample used is just that, a sample and NOT the entire population.

This is defined as sqrt( sum ((x - mean(2))**2) / N - 1)

In this case, the code shown above should subtract 1 from $nbelement
before the sqtr() function near the bottom.

Chris
Aug 31 '05 #3

P: n/a
Hello,

on 08/30/2005 11:09 PM Christopher Pomasl said the following:
Also Note there are two calculations for standard deviation.
The first as shown above is called the "population" deviation and implies
that the sample used to calculate the deviation is the entire sample.

This is defined as sqrt( sum ((x - mean(x))**2) / N)

There is a weighted "sample" deviation calculation which implies that the
sample used is just that, a sample and NOT the entire population.

This is defined as sqrt( sum ((x - mean(2))**2) / N - 1)

In this case, the code shown above should subtract 1 from $nbelement
before the sqtr() function near the bottom.


You may also want to take a look at this class that computes standard
deviation:

http://www.phpclasses.org/probabilistic
--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Aug 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.