0

Dose anyone know how the Confidence interval for the standard deviation is calculated in normfit function in Matlab? I need a python code to calculate such a parameter. In MATLAB, normfit returns 4 parameter, mean, std and Confidence interval of mean (muCI) and Confidence interval of standard deviation (sigmaCI).

[muHat,sigmaHat,muCI,sigmaCI] = normfit(x)

The python code below gives three parameters, muHat, sigmaHat and muCI. But I need the confidence interval of std (sigmaCI) in Python.

def function(data, confidence= 0.95):
    a = 1.0 * np.array(data)
    n = len(a)
    m ,se =  np.mean(a), scipy.stats.sem(a)
    h = se * scipy.stats.t.ppf((1 + confidence) / 2., n - 1)
    sigma = np.std(data, ddof=1)
    return m, sigma, [m - h, m + h]
Magi
  • 341
  • 2
  • 15

1 Answers1

0

Here is the normfit function in Python.

 def normfit(self, data, confidence=0.95):
        a = 1.0 * np.array(data)
        n = len(a)
        m, se = np.mean(a), scipy.stats.sem(a)
        h = se * scipy.stats.t.ppf((1 + confidence) / 2., n - 1)
        var = np.var(data, ddof=1)
        varCI_upper = var * (n - 1) / (scipy.stats.chi2.ppf((1-confidence) / 2, n - 1))
        varCI_lower = var * (n - 1) / (scipy.stats.chi2.ppf(1-(1-confidence) / 2, n - 1))
        sigma = np.sqrt(var)
        sigmaCI_lower = np.sqrt(varCI_lower)
        sigmaCI_upper = np.sqrt(varCI_upper)

        return m, sigma, [m - h, m + h], [sigmaCI_lower, sigmaCI_upper]
Magi
  • 341
  • 2
  • 15