concept standard deviation in category data

appears as: stdard deviation, The stdard deviation, standard deviation, stdard deviations, stdard deviation
Data Science Bookcamp: Five Python Projects

This is an excerpt from Manning's book Data Science Bookcamp: Five Python Projects.

The variance is powerful measure of data dispersion. However, statisticians often use an alternative measure, which they call the standard deviation. The standard deviation is equal to the square-root of the variance. It can be computed by calling np.std. Squaring the output of np.std will naturally return the variance.

Listing 5.41. Computing the standard deviation
data = [1, 2, 3]
standard_deviation = np.std(data)
assert standard_deviation ** 2 == np.var(data)

Why use the standard deviation instead of variance? The answer has to do with unit-tracking. All measurements have units. Our measured temperatures had units of Fahrenheit. When we squared the distances of the temperature to their mean, we also squared their units. Therefore, our variance was in units of Fahrenheit-squared. Such squared units are very tricky to conceptualize. Taking the square root converts the units back to Fahrenheit again. Thus, a standard deviation in units of Fahrenheit is more easily interpretable than the variance.

Listing 6.4. Computing mean and standard deviation of a histogram
mean_normal = np.average(bin_edges[:-1], weights=likelihoods)
var_normal = weighted_variance(bin_edges[:-1], likelihoods)
std_normal = var_normal ** 0.5
print(f"Mean is approximately {mean_normal:.2f}")
print(f"Standard deviation is approximately {std_normal:.3f}")
Mean is approximately 0.50
Standard deviation is approximately 0.005

The distribution’s mean is approximately 0.5, and its standard deviation is approximately 0.005. In a Normal distribution, these values can be computed directly from the distribution’s peak. We just need the peak’s x-value and y-value coordinates. The x-value equals the distribution’s mean. Meanwhile, the standard deviation is equal to the inverse of the y-value multiplied by 2π1/2. These properties are derived from the mathematical analysis of the Normal curve. Let’s re-compute the mean and standard deviation using just the coordinates of the peak.

Listing 6.5. Computing mean and standard deviation from peak coordinates
import math
peak_x_value = bin_edges[likelihoods.argmax()]
print(f"Mean is approximately {peak_x_value:.2f}")
peak_y_value = likelihoods.max()
std_from_peak = (peak_y_value * (2* math.pi) ** 0.5) ** -1
print(f"Standard deviation is approximately {std_from_peak:.3f}")
Mean is approximately 0.50
Standard deviation is approximately 0.005

The plotted curve fits nicely over the histogram. The curve’s peak sits at an x-axis position of 0.5, and rises to a y-axis position of approximately 80. As a reminder, the peak’s x and y coordinates are a direct function of fitted_mean and fitted_std. In order to emphasize this import relationship, lets do a simple exercise. We’ll shift the peak 0.01 units to the right, while also doubling the peak’s height. How do we execute the shift? Well, the peak’s axis is equal to the mean, so we’ll adjust the input mean to fitted_mean + 0.01. Also, the peak’s height is inversely proportional to the standard deviation. Therefore, inputting fitted_std / 2 should double the height of the peak.

Listing 6.8. Manipulating a Normal curve’s peak coordinates
adjusted_likelihoods = stats.norm.pdf(bin_edges, fitted_mean + 0.01,
                                      fitted_std / 2)
plt.plot(bin_edges, adjusted_likelihoods, color='k', linestyle='--')
plt.hist(sample_means, bins='auto', alpha=0.2, color='r', density=True)
plt.xlabel('Sample Mean')
plt.ylabel('Relative Likelihood')
plt.show()
Figure 6.3. A modified Normal curve whose center is .01 units to the right of the histogram. The peak of the curve is twice the height of the histogram’s peak. These modifications were achieved by manipulating the histogram’s mean and standard deviation.
fig6 3