noelm Posted January 12, 2011 Share Posted January 12, 2011 i need some help here guys, i have to write a program to calculate standard deviation without calling in any library functions, just in standard raw php. in the forum the user enters 5 numbers (floats) and i have to write a program to calculate the corresponding output. <?php $sample[0] = (float) $_GET['x0']; $sample[1] = (float) $_GET['x1']; $sample[2] = (float) $_GET['x2']; $sample[3] = (float) $_GET['x3']; $sample[4] = (float) $_GET['x4']; $array_count = 0; $array_total = 0; $mean = 0; $array_count_minus_one = $array_count - 1; $random_array = array(); $newarray = array(); $array_total_newarray = 0; $mean_minus_one = $mean - 1; $array_count = count($sample); $array_total = array_sum($sample); //**mean**// $mean = (($array_total/$array_count) / $array_count); // foreach ($sample as $samples) { $random_array[] = $samples - $mean; } foreach ($random_array as $random_arrays) { $newarray[] = pow($random_arrays, 2); } $array_total_newarray += array_sum($newarray); $array_total_newarray = $array_total_newarray / $mean_minus_one; $answer = sqrt($array_total_newarray); echo "<p>{$answer}</p>"; echo "<p>{$array_total}</p>"; echo "<p>{$array_count}</p>"; ?> i find this extremely difficult, and it's not homework that i will be getting marks for as my lecturer (jerk) would not give me an extension even though i had a total of 1 night to complete this assignment. Quote Link to comment https://forums.phpfreaks.com/topic/224201-php-standard-deviation/ Share on other sites More sharing options...
Maq Posted January 12, 2011 Share Posted January 12, 2011 What's the specific issue you're having? What happens? (Please use ) Quote Link to comment https://forums.phpfreaks.com/topic/224201-php-standard-deviation/#findComment-1158461 Share on other sites More sharing options...
Alex Posted January 12, 2011 Share Posted January 12, 2011 Well, you have a few issues there. For example, I'm not sure what you're trying to do here: $mean = (($array_total/$array_count) / $array_count); Shouldn't that be just $array_total / $array_count? Anyway, I'll attempt to explain how to go about solving this problem step by step.. Before we attempt to write a program to calculate standard deviation we must understand what standard deviation actually is, and often just writing down what you're trying to replicate in a program makes it much easier. Standard deviation is defined as: [tex]\sigma = \sqrt{\frac{\sum_{n = 1}^n (x - \overline{x})^2}{n}}[/tex] In words, this simple means that for each number we find the different between it and the average of the numbers, square that result, then find the average of all the results, and take the square root of that. So, now all we need to do is convert this into a procedure in code. First, we'll find the average of the set we're dealing with: $mean = array_sum($sample) / sizeof($sample); Now, we'll loop through all the values in array and find square of the difference between each number and the mean. $devs = array(); foreach($sample as $num) { $devs[] = pow($num - $mean, 2); } Now that we have an array ($devs) of the deviations, we simply find the average of those, then take the square root of that. So.. $standard_deviation = sqrt(array_sum($devs) / sizeof($devs)); Quote Link to comment https://forums.phpfreaks.com/topic/224201-php-standard-deviation/#findComment-1158471 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.