Swift Program to Calculate Standard Deviation


In this article, we will learn how to write a swift program to calculate standard deviation. Standard deviation is a measure, which will represent how much variation from the mean exists, or we can say that it is used to calculate the extent to which the values are differ from the average.

$$\mathrm{\sigma\:=\:\sqrt{{\frac{1}{N}}\sum_{i=1}^N\:(X_i-\mu)^2}}$$

The mathematical formula of standard deviation is-

  • σ = standard deviation

  • N = total number of elements

  • Xi = ith element

  • \mu = Mean of the given elements

Therefore, in this article, we calculate the standard deviation of the given array by using the above mathematical formula. For example −

Arr = [2, 4, 5, 6, 7, 2]
Standard deviation = 1.9148542155126762

Algorithm

  • Step 1 − Create a function to find the standard deviation.

  • Step 2 − In this function, find the site of the array using the count property.

  • Step 3 − Use for loop to iterate through each element of the array and find their sum.

  • Step 4 − Calculate the mean by dividing the sum of the array elements by the size of the array.

  • Step 5 − Use for loop again to calculate the standard deviation with the help of mean and inbuilt functions like pow() and sqrt().

  • Step 6 − Create an array of integer types.

  • Step 7 − Call the function and pass the created array as a parameter in it to find the standard deviation.

  • Step 8 − Print the output.

Example

Following Swift program to calculate standard deviation.

import Foundation
import Glibc

// Function to calculate standard deviation
func standardDeviation(seq:[Int]){
   let size = seq.count
   var sum = 0
   var SD = 0.0
   var S = 0.0
   var resultSD = 0.0
   
   // Calculating the mean
   for x in 0..<size{
      sum += seq[x]
   }
   let meanValue = sum/size
   
   // Calculating standard deviation
   for y in 0..<size{
      SD += pow(Double(seq[y] - meanValue), 2)
   }
   S = SD/Double(size)
   resultSD = sqrt(S)
   
   print("Standard deviation is:", resultSD)
}

// Creating an array of integer type
var Myarray = [34, 5, 89, 3, 2, 11, 45, 6, 2]

print("Array is:", Myarray)

// Calling the function
standardDeviation(seq:Myarray)

Output

Array is: [34, 5, 89, 3, 2, 11, 45, 6, 2]
Standard deviation is: 27.880698221768647

Here in the above code, we have an array of integer types. Now we create a function to calculate the standard deviation of the given array. So for the standard deviation, we first calculate the mean of the given elements using meanValue = sum/size, where the sum is the sum of all the array elements and size is the total number of elements present in the array. After that, we find the standard deviation using the mean and the inbuilt functions like pow() and sqrt().

Conclusion

Therefore, this is how we can find the standard deviation of the given array using the mathematical formula. The standard deviation formula is the most efficient and easiest way to calculate the standard deviation.

Updated on: 29-Dec-2022

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements