from typing import List
from typing import Tuple


def _combine_mean_std(means: List[float], stds: List[float], iterations: List[int]) -> Tuple[float, float]:
    """Combines multiple groups of means and standard deviations.

    The algorithm utilizes the algorithm as described by `Cochrane <https://training.cochrane.org/handbook/current/chapter-06#section-6-5-2>`_. The method is valid since the each subgroup is the result returned by a multiprocessing process that simulations the same group.

    Parameters
    ----------
    means
        List of means.
    stds
        List of standard deviations.
    iterations
        Number of samples in each subgroup.
    """
    m1, s1, n1 = means[0], stds[0], iterations[0]

    for m2, s2, n2 in zip(means[1:], stds[1:], iterations[1:]):

        n3 = n1 + n2
        m3 = (n1 * m1 + n2 * m2) / n3
        s3 = ((n1 - 1) * s1 ** 2 + (n2 - 1) * s2 ** 2 + n1 * n2 / n3 * (m1 ** 2 + m2 ** 2 - 2 * m1 * m2)) / (n3 - 1)
        m1, s1, n1 = m3, s3, n3

    return m1, s1