def _incremental_mean_and_var(X, last_mean=0.0, last_variance=None, last_sample_count=0):
 last_sum = (last_mean * last_sample_count)
 new_sum = X.sum(axis=0)
 new_sample_count = X.shape[0]
 updated_sample_count = (last_sample_count + new_sample_count)
 updated_mean = ((last_sum + new_sum) / updated_sample_count)
 if (last_variance is None):
  updated_variance = None
 else:
  new_unnormalized_variance = (X.var(axis=0) * new_sample_count)
  if (last_sample_count == 0):
   updated_unnormalized_variance = new_unnormalized_variance
  else:
   last_over_new_count = (last_sample_count / new_sample_count)
   last_unnormalized_variance = (last_variance * last_sample_count)
   updated_unnormalized_variance = ((last_unnormalized_variance + new_unnormalized_variance) + ((last_over_new_count / updated_sample_count) * (((last_sum / last_over_new_count) - new_sum) ** 2)))
  updated_variance = (updated_unnormalized_variance / updated_sample_count)
 return (updated_mean, updated_variance, updated_sample_count)

def _incremental_mean_and_var ( X , last_mean = 0.0 , last_variance = None , last_sample_count = 0 ) : last_sum = ( last_mean * last_sample_count ) new_sum = X . sum ( axis = 0 ) new_sample_count = X . shape [ 0 ] updated_sample_count = ( last_sample_count + new_sample_count ) updated_mean = ( ( last_sum + new_sum ) / updated_sample_count ) if ( last_variance is None ) : updated_variance = None else : new_unnormalized_variance = ( X . var ( axis = 0 ) * new_sample_count ) if ( last_sample_count == 0 ) : updated_unnormalized_variance = new_unnormalized_variance else : last_over_new_count = ( last_sample_count / new_sample_count ) last_unnormalized_variance = ( last_variance * last_sample_count ) updated_unnormalized_variance = ( ( last_unnormalized_variance + new_unnormalized_variance ) + ( ( last_over_new_count / updated_sample_count ) * ( ( ( last_sum / last_over_new_count ) - new_sum ) ** 2 ) ) ) updated_variance = ( updated_unnormalized_variance / updated_sample_count ) return ( updated_mean , updated_variance , updated_sample_count )
calculate mean update and a youngs and cramer variance update .