|
Posted by Christopher Pomasl on 10/14/85 11:25
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
Navigation:
[Reply to this message]
|