Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eta service #24

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions eta_service/plot_skewnorm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import skewnorm


def plot_skewnorm_distribution(mean=0.7, stdev=0.1, skewness=-5):
# The default params are params for a sample distribution
# mu=mean, nu=median, sigma=stdev, gamma=skewness
# Negative values are left skewed, positive values are right skewed

distribution = skewnorm(a=skewness, loc=mean, scale=stdev)
skewnorm_mean = round(distribution.mean(), 4)
skewnorm_median = round(distribution.median(), 4)
skewnorm_stdev = round(distribution.std(), 4)
minus1_skewnorm_stdev = round(distribution.mean() - distribution.std(), 4)
plus1_skewnorm_stdev = round(distribution.mean() + distribution.std(), 4)
minus2_skewnorm_stdev = round(distribution.mean() - 2 * distribution.std(), 4)
plus2_skewnorm_stdev = round(distribution.mean() + 2 * distribution.std(), 4)

text = rf"""
$\mu$ = {mean}
$\sigma$ = {stdev}
$\gamma$ = {skewness}

Skewed $\mu$ = {skewnorm_mean}
Skewed $\nu$ = {skewnorm_median}
Skewed $\sigma$ = {skewnorm_stdev}

Skewed +/- $\sigma$ = [{minus1_skewnorm_stdev}, {plus1_skewnorm_stdev}]
Skewed +/- 2$\sigma$ = [{minus2_skewnorm_stdev}, {plus2_skewnorm_stdev}]
"""

fig, ax = plt.subplots(1, 1, figsize=(15, 5))
sample_pts = np.linspace(0, 1, 10000)
ax.plot(sample_pts, distribution.pdf(sample_pts))

# Mean
ax.axvline(x=skewnorm_mean, color="red", ls="--", lw=0.75)
ax.text(
skewnorm_mean,
0.99,
s=rf"$\mu$, {skewnorm_mean}",
fontsize=8,
horizontalalignment="right",
verticalalignment="top",
rotation=90,
transform=ax.transAxes,
)
# 1 stdev
ax.axvline(x=minus1_skewnorm_stdev, color="salmon", ls="--", lw=0.75)
ax.axvline(x=plus1_skewnorm_stdev, color="salmon", ls="--", lw=0.75)
ax.text(
minus1_skewnorm_stdev,
0.99,
s=rf"-$\sigma$, {minus1_skewnorm_stdev}",
fontsize=8,
horizontalalignment="right",
verticalalignment="top",
rotation=90,
transform=ax.transAxes,
)
ax.text(
plus1_skewnorm_stdev,
0.99,
s=rf"+$\sigma$, {plus1_skewnorm_stdev}",
fontsize=8,
horizontalalignment="right",
verticalalignment="top",
rotation=90,
transform=ax.transAxes,
)
# 2 stdev
ax.axvline(x=minus2_skewnorm_stdev, color="pink", ls="--", lw=0.75)
ax.axvline(x=plus2_skewnorm_stdev, color="pink", ls="--", lw=0.75)
ax.text(
minus2_skewnorm_stdev,
0.99,
s=rf"-2$\sigma$, {minus2_skewnorm_stdev}",
fontsize=8,
horizontalalignment="right",
verticalalignment="top",
rotation=90,
transform=ax.transAxes,
)
ax.text(
plus2_skewnorm_stdev,
0.99,
s=rf"+2$\sigma$, {plus2_skewnorm_stdev}",
fontsize=8,
horizontalalignment="right",
verticalalignment="top",
rotation=90,
transform=ax.transAxes,
)
# Info
ax.text(
0.05, 0.9, s=text, fontsize=11, verticalalignment="top", transform=ax.transAxes
)

# Axis and labels
ax.set_xlim(0, 1)
ax.set_xticks(np.linspace(0, 1, 11), minor=False)
ax.set_ylim(ymin=0)
ax.set_xlabel("Percentage")
ax.set_ylabel("Density")
ax.set_title(
rf"Probability Density Function ($\mu$={mean}, $\sigma$={stdev}, $\gamma$={skewness})"
)

# Save image
Path("./sample_distributions").mkdir(parents=True, exist_ok=True)
fig.savefig(
f"./sample_distributions/PDF_mean{mean}_stdev{stdev}_skewness{skewness}.png"
)


if __name__ == "__main__":
plot_skewnorm_distribution(mean=0.7, stdev=0.1, skewness=-5)
19 changes: 19 additions & 0 deletions eta_service/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contourpy==1.1.1
cycler==0.12.1
fonttools==4.43.1
greenlet==3.0.1
kiwisolver==1.4.5
matplotlib==3.8.0
numpy==1.26.1
packaging==23.2
pandas==2.1.2
Pillow==10.1.0
psycopg2==2.9.9
pyparsing==3.1.1
python-dateutil==2.8.2
pytz==2023.3.post1
scipy==1.11.3
six==1.16.0
SQLAlchemy==2.0.22
typing_extensions==4.8.0
tzdata==2023.3
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.