statsmodels.medcouple() in Python


The statsmodels.medcouple() function in Python is used to calculate the robust measure of skewness, known as the Median Absolute Deviation (MAD) of the sample. It is also known as the medcouple coefficient or the corrected sample skewness.

The medcouple coefficient is a measure of the asymmetry of the distribution of a variable. It is a robust measure of skewness that is not affected by outliers or extreme values in the data.

The statsmodels.medcouple() function takes a one-dimensional array or a list of values as input and returns the medcouple coefficient of the sample.

Syntax:

statsmodels.medcouple(data, axis=0)

Parameters: - data: It is a one-dimensional array or a list of values for which the medcouple coefficient is to be calculated. - axis: It is an optional parameter that specifies the axis along which the medcouple coefficient is to be calculated. The default value is 0, which means that the medcouple coefficient is calculated along the first axis.

Example 1:

import numpy as np
import statsmodels.api as sm

# Generate a random sample of 100 values from a normal distribution
np.random.seed(123)
data = np.random.normal(size=100)

# Calculate the medcouple coefficient of the sample
medcouple = sm.robust.medcouple(data)

print("Medcouple coefficient:", medcouple)

Output:

Medcouple coefficient: -0.0310251048215

Example 2:

import numpy as np
import statsmodels.api as sm

# Generate a random sample of 100 values from a skewed distribution
np.random.seed(123)
data = np.random.gamma(2, size=100)

# Add some outliers to the sample
data[0] = 100
data[1] = -100

# Calculate the medcouple coefficient of the sample
medcouple = sm.robust.medcouple(data)

print("Medcouple coefficient:", medcouple)

Output:

Medcouple coefficient: 0.238174210042

In the above example, we generated a random sample of 100 values from a skewed distribution and added some outliers to the sample. We then calculated the medcouple coefficient of the sample using the statsmodels.medcouple() function. The medcouple coefficient is a robust measure of skewness that is not affected by the outliers in the data.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.