# Find combined mean and variance of two series

Given two different series arr1[n] and arr2[m] of size n and m. The task is to find the mean and variance of combined series.

Examples :

Input : arr1[] = {3, 5, 1, 7, 8, 5}
arr2[] = {5, 9, 7, 1, 5, 4, 7, 3}
Output : Mean1: 4.83333   mean2: 5.125
StandardDeviation1: 5.47222
StandardDeviation2: 5.60938
Combined Mean: 5
d1_square: 0.0277777
d2_square: 0.015625
Combined Variance: 5.57143

Input : arr1[] = {23, 45, 34, 78, 12, 76, 34}
arr2[] = {65, 67, 34, 23, 45}
Output : Mean1: 43.1429   mean2: 46.8
StandardDeviation1: 548.694
StandardDeviation2: 294.56
Combined Mean: 44.6667
d1_square: 2.32199
d2_square: 4.55112
Combined Variance: 446.056


## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
The variance of the combined series is given by Where , and , is the mean of combined series.   , are the means and , are the standard deviations of two series.  Below is the implementation of above formula:

## C++

 // C++ program to find combined mean  // and variance of two series.  #include  using namespace std;     // Function to find mean of series.  float mean(int arr[], int n)  {      int sum = 0;      for (int i = 0; i < n; i++)          sum = sum + arr[i];             float mean = (float)sum / n;      return mean;  }     // Function to find the standard   // deviation of series.  float sd(int arr[], int n)  {      float sum = 0;      for (int i = 0; i < n; i++)          sum = sum + (arr[i] - mean(arr, n)) *                (arr[i] - mean(arr, n));             float sdd = sum / n;      return sdd;  }     // Function to find combined variance  // of two different series.  float combinedVariance(int arr1[], int arr2[],                                  int n, int m)  {      // mean1 and mean2 are the mean       // of two arrays.      float mean1 = mean(arr1, n);      float mean2 = mean(arr2, m);             cout << "Mean1: " << mean1            << " mean2: " << mean2 << endl;         // sd1 and sd2 are the standard       // deviation of two array.      float sd1 = sd(arr1, n);      float sd2 = sd(arr2, m);             cout << "StandardDeviation1: " << sd1            << " StandardDeviation2: " << sd2           << endl;         // combinedMean is variable to store      // the combined mean of both array.      float combinedMean = (float)(n * mean1 +                           m * mean2) / (n + m);             cout << "Combined Mean: " << combinedMean            << endl;         // d1_square and d2_square are       // the combined mean deviation.      float d1_square = (mean1 - combinedMean) *                        (mean1 - combinedMean);      float d2_square = (mean2 - combinedMean) *                         (mean2 - combinedMean);             cout << "d1 square: " << d1_square            << " d2_square: " << d2_square            << endl;         // combinedVar is variable to store       // combined variance of both array.      float combinedVar = (n * (sd1 + d1_square) + m *                         (sd2 + d2_square)) / (n + m);      return combinedVar;  }     // Driver function.  int main()  {      int arr1[] = { 23, 45, 34, 78, 12, 76, 34 };      int arr2[] = { 65, 67, 34, 23, 45 };      int n = sizeof(arr1) / sizeof(arr1);      int m = sizeof(arr2) / sizeof(arr2);             // Function call to combined mean.      cout << "Combined Variance: "           << combinedVariance(arr1, arr2, n, m);      return 0;  }

## Java

 // Java program to find combined mean  // and variance of two series.     import java.io.*;     class GFG {             // Function to find mean of series.      static float mean(int arr[], int n)      {          int sum = 0;                     for (int i = 0; i < n; i++)              sum = sum + arr[i];                     float mean = (float)sum / n;                     return mean;      }             // Function to find the standard       // deviation of series.      static float sd(int arr[], int n)      {          float sum = 0;          for (int i = 0; i < n; i++)              sum = sum + (arr[i] - mean(arr, n)) *                              (arr[i] - mean(arr, n));                     float sdd = sum / n;                     return sdd;      }             // Function to find combined variance      // of two different series.      static float combinedVariance(int arr1[],                          int arr2[], int n, int m)      {          // mean1 and mean2 are the mean           // of two arrays.          float mean1 = mean(arr1, n);          float mean2 = mean(arr2, m);                     System.out.print("Mean1: " + mean1 + " ") ;          System.out.println("Mean2: " + mean2) ;                 // sd1 and sd2 are the standard           // deviation of two array.          float sd1 = sd(arr1, n);          float sd2 = sd(arr2, m);                     System.out.print("StandardDeviation1: "                                     + sd1 + " ") ;                                                System.out.println("StandardDeviation2: "                                     + sd2 + " ");                        // combinedMean is variable to store          // the combined mean of both array.          float combinedMean = (float)(n * mean1 +                                  m * mean2) / (n + m);                     System.out.println( "Combined Mean: "                             + combinedMean + " ");                                 // d1_square and d2_square are           // the combined mean deviation.          float d1_square = (mean1 - combinedMean) *                              (mean1 - combinedMean);                     float d2_square = (mean2 - combinedMean) *                               (mean2 - combinedMean);                     System.out.print("d1 square: " + d1_square + " " );           System.out.println("d2_square: " + d2_square);                  // combinedVar is variable to store           // combined variance of both array.          float combinedVar = (n * (sd1 + d1_square)                      + m * (sd2 + d2_square)) / (n + m);                     return combinedVar;      }                // Driver function.          public static void main (String[] args) {                     int arr1[] = { 23, 45, 34, 78, 12, 76, 34 };          int arr2[] = { 65, 67, 34, 23, 45 };          int n = arr1.length;          int m = arr2.length;                     // Function call to combined mean.              System.out.println("Combined Variance: "                 + combinedVariance(arr1, arr2, n, m));          }  }        // This code is contributed by vt_m.

## Python3

 # Python3 program to find   # combined mean and variance  # of two series.     # Function to find  # mean of series.  def mean(arr, n):      sum = 0;      for i in range(n):          sum = sum + arr[i];      mean = sum / n;      return mean;     # Function to find the   # standard deviation   # of series.  def sd(arr, n):      sum = 0;      for i in range(n):          sum = sum + ((arr[i] - mean(arr, n)) *                       (arr[i] - mean(arr, n)));      sdd = sum / n;      return sdd;     # Function to find combined   # variance of two different  # series.  def combinedVariance(arr1, arr2, n, m):             # mean1 and mean2 are       # the mean of two arrays.      mean1 = mean(arr1, n);      mean2 = mean(arr2, m);             print("Mean1: ", round(mean1, 2),             " mean2: ", round(mean2, 2));         # sd1 and sd2 are the standard      # deviation of two array.      sd1 = sd(arr1, n);      sd2 = sd(arr2, m);             print("StandardDeviation1: ", round(sd1, 2),             " StandardDeviation2: ", round(sd2, 2));             # combinedMean is variable       # to store the combined       # mean of both array.      combinedMean = (n * mean1 +                      m * mean2) / (n + m);      print("Combined Mean: ",              round(combinedMean, 2));             # d1_square and d2_square are      # the combined mean deviation.      d1_square = ((mean1 - combinedMean) *                   (mean1 - combinedMean));      d2_square = ((mean2 - combinedMean) *                   (mean2 - combinedMean));      print("d1 square: ", round(d1_square, 2),            " d2_square: ", round(d2_square, 2));             # combinedVar is variable to       # store combined variance of       # both array.      combinedVar = (n * (sd1 + d1_square) +                     m * (sd2 + d2_square)) / (n + m);      print("Combined Variance: ",            round(combinedVar, 2));     # Driver Code  arr1 = [ 23, 45, 34, 78, 12, 76, 34 ];  arr2 = [ 65, 67, 34, 23, 45 ];  n = len(arr1);  m = len(arr2);         # Function call to combined mean.  combinedVariance(arr1, arr2, n ,m);     # This code is contributed by mits

## C#

 // C# program to find combined mean  // and variance of two series.  using System;     class GFG {             // Function to find mean of series.      static float mean(int []arr, int n)      {          int sum = 0;                     for (int i = 0; i < n; i++)              sum = sum + arr[i];                     float mean = (float)sum / n;                     return mean;      }             // Function to find the standard       // deviation of series.      static float sd(int []arr, int n)      {          float sum = 0;          for (int i = 0; i < n; i++)              sum = sum + (arr[i] - mean(arr, n)) *                          (arr[i] - mean(arr, n));                     float sdd = sum / n;          return sdd;      }             // Function to find combined variance      // of two different series.      static float combinedVariance(int []arr1,                                       int []arr2,                                        int n, int m)      {                     // mean1 and mean2 are the           // mean of two arrays.          float mean1 = mean(arr1, n);          float mean2 = mean(arr2, m);                     Console.Write("Mean1: " + mean1 + " ") ;          Console.WriteLine("Mean2: " + mean2) ;                 // sd1 and sd2 are the standard           // deviation of two array.          float sd1 = sd(arr1, n);          float sd2 = sd(arr2, m);                     Console.Write("StandardDeviation1: "                                 + sd1 + " ") ;                                                 Console.WriteLine("StandardDeviation2: "                                 + sd2 + " ");                        // combinedMean is variable to store          // the combined mean of both array.          float combinedMean = (float)(n * mean1 +                                m * mean2) / (n + m);                     Console.WriteLine("Combined Mean: "                            + combinedMean + " ");                                 // d1_square and d2_square are           // the combined mean deviation.          float d1_square = (mean1 - combinedMean) *                               (mean1 - combinedMean);                     float d2_square = (mean2 - combinedMean) *                             (mean2 - combinedMean);                     Console.Write("d1 square: " +                          d1_square + " " );           Console.WriteLine("d2_square: " +                              d2_square);                  // combinedVar is variable to store           // combined variance of both array.          float combinedVar = (n * (sd1 + d1_square)    +                               m * (sd2 + d2_square)) /                              (n + m);                     return combinedVar;      }         // Driver code  public static void Main ()  {          int []arr1 = {23, 45, 34, 78, 12, 76, 34};          int []arr2 = {65, 67, 34, 23, 45};          int n = arr1.Length;          int m = arr2.Length;                     // Function call to combined mean.          Console.WriteLine("Combined Variance: " +                              combinedVariance(arr1, arr2,                             n, m));          }  }     // This code is contributed by vt_m.

## PHP

 

Output :

Mean1: 43.1429 mean2: 46.8
StandardDeviation1: 548.694 StandardDeviation2: 294.56
Combined Mean: 44.6667
d1 square: 2.322 d2_square: 4.5511
Combined Variance: 446.056


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, jit_t, Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.