using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BotSharp.Algorithm.HiddenMarkovModel.MathUtils.Statistics
{
///
/// Standard error of a sampling distribution is the standard deviation of the the normal distribution formed by the sample statistic (as followed from the Central Limit Theorem or CLT)
///
public class StandardError
{
///
/// Return the standard error of the sampling distribution given a random sample
/// Used for continuous-value random variable
///
/// The sample standard deviation from the random sample
/// The size of a random sample
/// The standard error of the sample statistics (e.g., sample mean) as estimated from the sample following Central Limit Theorem
public static double GetStandardError(double sampleStddev, int sampleSize)
{
return sampleStddev / System.Math.Sqrt(sampleSize);
}
///
/// Return the standard error of the sampling distribution given a random sample in which p is the proportion of individuals responding to "YES" and (1-p) is the proportion of individuals responding to "NO"
/// Used for binary discrete variable v = {"YES", "NO"}
///
/// Double value between 0 and 1, The proportion of individuals in a random sample responding to "YES"
/// The size of a random sample
/// Standard error of a random sample, which is the standard deviation of the sample statistic normal distribution by CLT
public static double GetStandardErrorForProportion(double p, int sampleSize)
{
return System.Math.Sqrt(p * (1 - p) / sampleSize);
}
///
/// Return the standard error of the sampling distribution given a random sample
///
/// The random sample given
/// Standard error of a random sample, which is the standard deviation of the sample statistic normal distribution by CLT
public static double GetStandardError(double[] sample)
{
double sampleMean = Mean.GetMean(sample);
double sampleStdDev = StdDev.GetStdDev(sample, sampleMean);
return GetStandardError(sampleStdDev, sample.Length);
}
///
/// Return the standard error of the sampling distribution of the difference between two population statistics var1 and var2, assuming var1 and var2 are independent
///
/// random sample for var1
/// random sample for var2
/// Standard error of a random sample, which is the standard deviation of the sample statistic normal distribution by CLT
public static double GetStandardError(double[] sample_for_var1, double[] sample_for_var2)
{
double mu_for_var1 = Mean.GetMean(sample_for_var1);
double mu_for_var2 = Mean.GetMean(sample_for_var2);
double sigma_for_var1 = StdDev.GetStdDev(sample_for_var1, mu_for_var1);
double sigma_for_var2 = StdDev.GetStdDev(sample_for_var2, mu_for_var2);
return System.Math.Sqrt(sigma_for_var1 * sigma_for_var1 / sample_for_var1.Length + sigma_for_var2 * sigma_for_var2 / sample_for_var2.Length);
}
///
/// Return the standard error of the sample distribution given multiple random samples, for each of which the standard error has been calculated
///
/// List of size for each random sample
/// List of standard error for the sample mean of each random sample
/// Standard error of a random sample, which is the standard deviation of the sample statistic normal distribution by CLT
public static double GetStandardErrorForWeightAverages(int[] sampleSizes, double[] standardErrors)
{
double sum = 0;
int totalSampleSize = 0;
for (int i = 0; i < sampleSizes.Length; ++i)
{
totalSampleSize += sampleSizes[i];
}
for (int i = 0; i < sampleSizes.Length; ++i)
{
sum += System.Math.Pow(sampleSizes[i] * standardErrors[i] / totalSampleSize, 2);
}
return System.Math.Sqrt(sum);
}
}
}