def _DegreesOfFreedom(v1, v2, n1, n2):
  """Calculates degrees of freedom using the Welch-Satterthwaite formula.

  Degrees of freedom is a measure of sample size. For other types of tests,
  degrees of freedom is sometimes N - 1, where N is the sample size. However,

  Args:
    v1: Variance of sample 1.
    v2: Variance of sample 2.
    n1: Size of sample 2.
    n2: Size of sample 2.

  Returns:
    An estimate of degrees of freedom. Must be at least 1.0.
  """
  # When there's no variance in either sample, return 1.
  if v1 == 0 and v2 == 0:
    return 1
  # If the sample size is too small, also return the minimum (1).
  if n1 <= 1 or n2 <= 2:
    return 1
  df = (((v1 / n1 + v2 / n2) ** 2) /
        ((v1 ** 2) / ((n1 ** 2) * (n1 - 1)) +
         (v2 ** 2) / ((n2 ** 2) * (n2 - 1))))
  return max(1, df)