You can download this code by clicking the button below.
This code is now available for download.
This function calculates the sample mean deviation from a sample or from a specified population mean. If the population mean is not specified, the mean of the data is used.
Technology Stack : SciPy
Code Type : Statistical function
Code Difficulty : Intermediate
import numpy as np
from scipy.stats import ttest_1samp
def sample_mean_deviation(data, mu=None):
"""
Calculate the sample mean deviation from a sample or from a specified population mean.
Parameters:
- data: array_like
The data from which to calculate the sample mean deviation.
- mu: float, optional
The population mean. If None, the mean of data is used.
Returns:
- sample_mean_deviation: float
The sample mean deviation.
"""
data = np.asarray(data)
if mu is None:
mu = np.mean(data)
return np.mean(np.abs(data - mu))