Skip to main content

An implementation of the Fernandez-Steel skewed Student's t-distribution.

Project description

Skew-T Distribution (Fernandez & Steel)

PyPI version License: MIT

This package provides a Python implementation of the skewed Student's t-distribution as proposed by Fernandez and Steel (1998). It is created as a scipy.stats compatible continuous random variable, making it easy to use for statistical modeling and analysis.

Key Features

  • Easy to use: Implemented as a standard scipy.stats object.
  • Flexible: Control both skewness (gamma) and tail thickness (df).
  • Standard Methods: Includes pdf, cdf, ppf, rvs, and stats (mean/variance).

Installation

pip install skewt-fs

Mathematical Details

The implementation follows the original paper:

Fernández, C., & Steel, M. F. J. (1998). On Bayesian Modeling of Fat Tails and Skewness. Journal of the American Statistical Association, 93(441), 359-371.

Let $g_\nu(x)$ be the probability density function (PDF) of the standard symmetric Student's t-distribution with $\nu$ degrees of freedom. The PDF of the skewed Student's t-distribution $f(x | \nu, \gamma)$ is defined as:

$$ f(x | \nu, \gamma) = \frac{2}{\gamma + \frac{1}{\gamma}} \left[ g_\nu\left(\frac{x}{\gamma}\right)I(x \ge 0) + g_\nu(\gamma x)I(x < 0) \right] $$

where:

  • $\nu$ is the degrees of freedom (df).
  • $\gamma$ is the skewness parameter (gamma).
  • $I(\cdot)$ is the indicator function.

Moments

The mean and variance of the distribution are:

Mean ($E[X]$), for $\nu > 1$: $$ E[X] = M_1 \left(\gamma - \frac{1}{\gamma}\right) $$

Variance ($Var(X)$), for $\nu > 2$: $$ Var(X) = (M_2 - M_1^2)\left(\gamma^2 + \frac{1}{\gamma^2}\right) + 2M_1^2 - M_2 $$

where $M_1 = E[|Z|]$ and $M_2 = E[Z^2]$ for a standard Student's t-distributed random variable $Z \sim g_\nu$.

How to Use the Package

The main object is skewt, which behaves like any other scipy.stats distribution object.

Importing

from skewt_fs import skewt

Basic Operations

You can use all the standard methods. The shape parameters df and gamma are passed as arguments.

# Define parameters: 5 degrees of freedom, right-skew (gamma > 1)
df = 5
gamma = 1.8

# Get theoretical mean and variance
mean, var = skewt.stats(df=df, gamma=gamma, moments='mv')
print(f"Theoretical Mean: {mean:.4f}")
print(f"Theoretical Variance: {var:.4f}")

# Evaluate the PDF at a point
pdf_val = skewt.pdf(x=1.0, df=df, gamma=gamma)
print(f"PDF at x=1: {pdf_val:.4f}")

# Evaluate the CDF at a point
cdf_val = skewt.cdf(x=1.0, df=df, gamma=gamma)
print(f"CDF at x=1: {cdf_val:.4f}")

# Find a percentile with the PPF (inverse CDF)
# For example, the 95th percentile
ppf_val = skewt.ppf(q=0.95, df=df, gamma=gamma)
print(f"95th percentile: {ppf_val:.4f}")

Generating Random Samples

Use the .rvs() method to generate random variates.

# Generate 1000 random samples
samples = skewt.rvs(df=df, gamma=gamma, size=1000)

# Verify sample moments
print(f"Sample Mean: {np.mean(samples):.4f}")
print(f"Sample Variance: {np.var(samples):.4f}")

Visualization

You can easily plot the distribution to visualize the effect of the parameters.

# Set up the plot
fig, ax = plt.subplots(figsize=(10, 6))
x = np.linspace(skewt.ppf(0.001, df, gamma), skewt.ppf(0.999, df, gamma), 200)

# Plot the PDF
ax.plot(x, skewt.pdf(x, df=df, gamma=gamma), 'r-', lw=3, alpha=0.8, label=f'skewt pdf (df={df}, γ={gamma})')

# Plot a histogram of the random samples
ax.hist(samples, bins=50, density=True, histtype='stepfilled', alpha=0.3, label='Sample Histogram')

# Add lines for mean and median
ax.axvline(mean, color='k', linestyle='--', label=f'Mean: {mean:.2f}')
ax.axvline(skewt.ppf(0.5, df, gamma), color='g', linestyle='-', label=f'Median: {skewt.ppf(0.5, df, gamma):.2f}')

ax.set_title("Fernandez-Steel Skewed Student's t-Distribution")
ax.set_xlabel("x")
ax.set_ylabel("Density")
ax.legend()
ax.grid(True, linestyle='--', alpha=0.6)
plt.show()

API Reference

skewt

An instance of the skewt_gen class. It is a continuous random variable object from scipy.stats.

Shape Parameters:

  • df (float): Degrees of freedom, must be greater than 0.
  • gamma (float): Skewness parameter, must be greater than 0.
    • gamma > 1: Right (positive) skew.
    • gamma < 1: Left (negative) skew.
    • gamma = 1: Symmetric (reverts to the standard Student's t-distribution).

Location and Scale: Like all scipy.stats objects, it also accepts loc (for mean/location) and scale parameters.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on the project's GitHub repository.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

skewt_fs-0.1.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

skewt_fs-0.1.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file skewt_fs-0.1.2.tar.gz.

File metadata

  • Download URL: skewt_fs-0.1.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for skewt_fs-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5ac867a26ca9a115e35dad2288b778f113ed463fe91598437060a467ba4845eb
MD5 cc8b03ddc006f42dc43a34cb37debaea
BLAKE2b-256 d90c4d540852c49232f779ed8dc6660ed33dc7dba7c8023836096ec3ac557095

See more details on using hashes here.

File details

Details for the file skewt_fs-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: skewt_fs-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for skewt_fs-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6f64ac6f1ece66ab583f636d22f964486d6aa882fe2735a175b4d9fb8e87b79d
MD5 aa93520df85e06db1b9c8668082f8103
BLAKE2b-256 e882f5668c594728de91030d3929faed6dd583fa9788179cea61ed823f4f6a7a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page