Dada una array, necesitamos calcular la varianza y la desviación estándar de los elementos de la array.
Ejemplos:
Input : arr[] = [1, 2, 3, 4, 5] Output : Variance = 2 Standard Deviation = 1 Input : arr[] = [7, 7, 8, 8, 3] Output : Variance = 3 Standard Deviation = 1
Hemos discutido el programa para encontrar la media de una array .
La media es el promedio del elemento.
Media de arr[0..n-1] = ∑(arr[i]) / n
donde 0 <= i < n
La varianza es la suma de las diferencias al cuadrado de la media dividida por el número de elementos.
Varianza = ∑(arr[i] – media) 2 / n
La desviación estándar es la raíz cuadrada de la varianza
Desviación estándar = √(varianza)
Consulte Media, varianza y desviación estándar para obtener más detalles.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to find variance // and standard deviation of // given array. #include <bits/stdc++.h> using namespace std; // Function for calculating variance int variance(int a[], int n) { // Compute mean (average of elements) int sum = 0; for (int i = 0; i < n; i++) sum += a[i]; double mean = (double)sum / (double)n; // Compute sum squared // differences with mean. double sqDiff = 0; for (int i = 0; i < n; i++) sqDiff += (a[i] - mean) * (a[i] - mean); return sqDiff / n; } double standardDeviation(int arr[], int n) { return sqrt(variance(arr, n)); } // Driver Code int main() { int arr[] = {600, 470, 170, 430, 300}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Variance: " << variance(arr, n) << "\n"; cout << "Standard Deviation: " << standardDeviation(arr, n) << "\n"; return 0; }
Java
// Java program to find variance // and standard deviation of // given array. import java.io.*; class GFG { // Function for calculating // variance static double variance(double a[], int n) { // Compute mean (average // of elements) double sum = 0; for (int i = 0; i < n; i++) sum += a[i]; double mean = (double)sum / (double)n; // Compute sum squared // differences with mean. double sqDiff = 0; for (int i = 0; i < n; i++) sqDiff += (a[i] - mean) * (a[i] - mean); return (double)sqDiff / n; } static double standardDeviation(double arr[], int n) { return Math.sqrt(variance(arr, n)); } // Driver Code public static void main (String[] args) { double arr[] = {600, 470, 170, 430, 300}; int n = arr.length; System.out.println( "Variance: " + variance(arr, n)); System.out.println ("Standard Deviation: " + standardDeviation(arr, n)); } } // This code is contributed by vt_m.
Python 3
# Python 3 program to find variance # and standard deviation of # given array. import math # Function for calculating variance def variance(a, n): # Compute mean (average of # elements) sum = 0 for i in range(0 ,n): sum += a[i] mean = sum /n # Compute sum squared # differences with mean. sqDiff = 0 for i in range(0 ,n): sqDiff += ((a[i] - mean) * (a[i] - mean)) return sqDiff / n def standardDeviation(arr, n): return math.sqrt(variance(arr, n)) # Driver Code arr = [600, 470, 170, 430, 300] n = len(arr) print("Variance: ", int(variance(arr, n))) print("Standard Deviation: ", round(standardDeviation(arr, n), 3)) # This code is contributed by Smitha
C#
// C# program to find variance and // standard deviation of given array. using System; class GFG { // Function for calculating // variance static float variance(double []a, int n) { // Compute mean (average // of elements) double sum = 0; for (int i = 0; i < n; i++) sum += a[i]; double mean = (double)sum / (double)n; // Compute sum squared // differences with mean. double sqDiff = 0; for (int i = 0; i < n; i++) sqDiff += (a[i] - mean) * (a[i] - mean); return (float)sqDiff / n; } static float standardDeviation(double []arr, int n) { return (float)Math.Sqrt(variance(arr, n)); } // Driver Code public static void Main () { double []arr = {600, 470, 170, 430, 300}; int n = arr.Length; Console.WriteLine( "Variance: " + variance(arr, n)); Console.WriteLine ("Standard Deviation: " + standardDeviation(arr, n)); } } // This code is contributed by vt_m.
PHP
<?php // PHP program to find variance // and standard deviation of // given array. // Function for calculating. // variance function variance( $a, $n) { // Compute mean (average // of elements) $sum = 0; for ( $i = 0; $i < $n; $i++) $sum += $a[$i]; $mean = $sum / $n; // Compute sum squared // differences with mean. $sqDiff = 0; for ( $i = 0; $i < $n; $i++) $sqDiff += ($a[$i] - $mean) * ($a[$i] - $mean); return $sqDiff / $n; } function standardDeviation($arr, $n) { return sqrt(variance($arr, $n)); } // Driver Code $arr = array(600, 470, 170, 430, 300); $n = count($arr); echo "Variance: " , variance($arr, $n) , "\n"; echo"Standard Deviation: " , standardDeviation($arr, $n) ,"\n"; // This code is contributed by anuj_67. ?>
Javascript
<script> // JavaScript program to find variance and // standard deviation of given array. // Function for calculating // variance function variance(a, n) { // Compute mean (average of elements) var sum = 0; for (var i = 0; i < n; i++){ sum += a[i]; } var mean = sum / n; // Compute sum squared // differences with mean. var sqDiff = 0; for (var i = 0; i < n; i++) { sqDiff += (a[i] - mean) * (a[i] - mean); } return sqDiff / n; } function standardDeviation(arr , n) { return Math.sqrt(variance(arr, n)); } // Driver Code var arr = [600, 470, 170, 430, 300] var n = arr.length; document.write( "Variance: " + variance(arr, n) + "<br>"); document.write ("Standard Deviation: " + standardDeviation(arr, n).toFixed(3)); </script>
Variance: 21704 Standard Deviation: 147.323
La complejidad temporal del programa es O(n) .
Este artículo es una contribución de Himanshu Ranjan . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA