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

Add support for Chi-square distribution #67

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
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
Binary file added =
Binary file not shown.
64 changes: 40 additions & 24 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@

# to calculate mean
>>> print(exponential.calculate_mean(1))
2.0
2.0

# to calculate standard deviation
# to calculate standard deviation
>>> print(exponential.calculate_stdev(1))
2.0
2.0

# to calculate pdf
# to calculate pdf
>>> print(exponential.calculate_pdf(5, 5))
0.04104

Expand All @@ -129,11 +129,12 @@
>>> print(exponential.calculate_cdf(-2))
0.0

# plot pdf of exponential distribution
>>> exponential.plot_bar_pdf()
# plot pdf of exponential distribution
>>> exponential.plot_bar_pdf()
```

## For Gamma Distribution

```
>>> from probdists import Gamma

Expand Down Expand Up @@ -185,33 +186,34 @@
# The resulting gamma three will have k=3, theta=2.
# This add magic method fails if thetas are not equal since they wouldn't be summable
```
## For Uniform Distribution

## For Uniform Distribution

```
>>> from probdists import Uniform
>>> from probdists import Uniform

# default value of the interval is (1,10)
>>> uniform = Uniform()

>>> uniform2 = Uniform(1, 5)
# interval of uniform2 is (1,5)
# interval of uniform2 is (1,5)

>>> uniform.read_data_file('demo_uniform_data')
# pass in your filename to read data from filename

# to access data
# to access data
>>> print(uniform.data)
[4,5,2,3,3,2,2,5,4,3,1,3,5,3,4]
[4,5,2,3,3,2,2,5,4,3,1,3,5,3,4]

# to calculate mean
>>> print(uniform.calculate_mean())
5
5

# to calculate standard deviation
# to calculate standard deviation
>>> print(uniform.calculate_stdev())
2.89
2.89

# to calculate pdf
# to calculate pdf
>>> print(uniform.calculate_pdf(5))
0.1

Expand All @@ -225,10 +227,10 @@
# plot histogram of data
>>> uniform.plot_histogram()

# plot pdf of uniform distribution
>>> uniform.plot_bar_pdf()
# plot pdf of uniform distribution
>>> uniform.plot_bar_pdf()

# to calculate cdf
# to calculate cdf
>>> uniform.replace_stats_with_data()
>>> print(uniform.calculate_cdf(0))
0
Expand All @@ -237,6 +239,7 @@
>>> print(uniform.calculate_cdf(4))
0.75
```

## For Bernoulli Distribution

```
Expand Down Expand Up @@ -271,9 +274,9 @@
>>> print(bernoulli.calculate_pdf(1, 1))
0.3

# to calculate cdf
# to calculate cdf
>>> print(bernoulli.calculate_cdf(0.7))
0.7
0.7
>>> print(bernoulli.calculate_cdf(2))
1

Expand All @@ -297,6 +300,7 @@
```

## For Triangular Distribution

```
>>> from probdists import Triangular

Expand Down Expand Up @@ -324,17 +328,29 @@
>>> print(triangle.calculate_pdf(0.5))
2

# to calculate cdf
# to calculate cdf
>>> print(triangle.calculate_cdf(0.5))
0.5
>>> print(triangle.calculate_cdf(1))
1

# to access data
# to access data
>>> print(triangle.data)
[1, 2, 3, 4, 5, 5, 6, 8, 9, 13]

# plot pdf of triangular distribution
>>> triangle.plot_bar_pdf()
# plot pdf of triangular distribution
>>> triangle.plot_bar_pdf()

```

## For chi Square distribution

```
# importing Chi_squareDistribution
>>>from probdists import Chi_squareDistribution
>>>chi = Chi_squareDistribution()
# to read data from csv
>>>chi.read_data_file("filename.csv ")
# to print chi Square value
>>>chi.chi_square()
```
46 changes: 23 additions & 23 deletions probdists/Bernoullidistribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import matplotlib.pyplot as plt
from .Generaldistribution import Distribution
from .Binomialdistribution import Binomial
import seaborn as sns


class Bernoulli(Distribution):
""" Bernoulli distribution class for calculating and
"""Bernoulli distribution class for calculating and
visualizing a Bernoulli distribution.

Attributes:
Expand All @@ -23,7 +24,7 @@ def __init__(self, prob=0.5):
self.calculate_stdev())

def calculate_mean(self, round_to=2):
""" Method to calculate the mean of a Bernoulli distribution
"""Method to calculate the mean of a Bernoulli distribution

Args:
round_to (int): Round the mean value. Defaults to 2.
Expand Down Expand Up @@ -51,7 +52,7 @@ def calculate_stdev(self, round_to=2):
return round(self.stdev, round_to)

def replace_stats_with_data(self):
""" Method to calculate p from the data set
"""Method to calculate p from the data set

Args:
None
Expand All @@ -67,8 +68,8 @@ def replace_stats_with_data(self):
return self.p

def plot_bar(self):
""" Method to plot a histogram of the instance variable data using
matplotlib pyplot library.
"""Method to plot a histogram of the instance variable data using
seaborn pyplot library.

Args:
None
Expand All @@ -77,13 +78,12 @@ def plot_bar(self):
None
"""

plt.bar(x=['0', '1'], height=[(1 - self.p), self.p])
plt.title('Bar Chart of Data')
plt.xlabel('outcome')
plt.ylabel('count')
sns.barplot(x=["0", "1"], height=[(1 - self.p), self.p]).set(
title="Bar Chart of Data", xlabel="outcome", ylabel="count"
)

def calculate_pdf(self, k, round_to=2):
""" Method to calculate pdf for the bernoulli distribution.
"""Method to calculate pdf for the bernoulli distribution.

Args:
k (float): point for calculating the probability density function. Range of k: {0,1}
Expand All @@ -102,7 +102,7 @@ def calculate_pdf(self, k, round_to=2):
return round(self.pdf, round_to)

def calculate_cdf(self, k, round_to=2):
""" Method to calculate cdf for the bernoulli distribution.
"""Method to calculate cdf for the bernoulli distribution.

Args:
k (float): point for calculating the cumulative distribution function
Expand All @@ -112,7 +112,7 @@ def calculate_cdf(self, k, round_to=2):
float: cumulative distribution function output
"""

val = 0 # default value of cdf for k < 0
val = 0 # default value of cdf for k < 0
if 0 <= k < 1:
val = 1 - self.p
elif k > 1:
Expand All @@ -121,7 +121,7 @@ def calculate_cdf(self, k, round_to=2):
return round(self.cdf, round_to)

def plot_bar_pdf(self):
""" Method to plot the pdf of the bernoulli distribution
"""Method to plot the pdf of the bernoulli distribution

Args:
None
Expand All @@ -137,17 +137,15 @@ def plot_bar_pdf(self):
y.append(self.pdf)

# draw the plots
plt.bar(x, y)
plt.title("Distribution of Outcomes")
plt.ylabel("Probability")
plt.xlabel("Outcome")

plt.barplot(x, y).set(
title="Distribution of Outcomes", ylabel="Probability", xlabel="Outcome"
)
plt.show()

return x, y

def __add__(self, other):
""" Method to add together two Bernoulli distributions with equal p
"""Method to add together two Bernoulli distributions with equal p

Args:
other (Bernoulli): Bernoulli instance
Expand All @@ -157,7 +155,7 @@ def __add__(self, other):
"""

try:
assert self.p == other.p, 'p values are not equal'
assert self.p == other.p, "p values are not equal"
except AssertionError:
raise

Expand All @@ -170,13 +168,15 @@ def __add__(self, other):
return result

def __repr__(self):
""" Method to output the characteristics of this Bernoulli instance
"""Method to output the characteristics of this Bernoulli instance
Args:
None

Returns:
string: characteristics of this Bernoulli instance
"""

return 'mean {0}, standard deviation {1}, \
p {2}, q {3}'.format(self.mean, self.stdev, self.p, 1.0 - self.p)
return "mean {0}, standard deviation {1}, \
p {2}, q {3}".format(
self.mean, self.stdev, self.p, 1.0 - self.p
)
35 changes: 17 additions & 18 deletions probdists/Binomialdistribution.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import math
import matplotlib.pyplot as plt
from .Generaldistribution import Distribution
import seaborn as sns


class Binomial(Distribution):
""" Binomial distribution class for calculating and
"""Binomial distribution class for calculating and
visualizing a Binomial distribution.

Attributes:
Expand All @@ -15,13 +16,12 @@ class Binomial(Distribution):
n (int) number of trials
"""

def __init__(self, prob=.5, size=20):
def __init__(self, prob=0.5, size=20):

self.n = size
self.p = prob

Distribution.__init__(self, self.calculate_mean(),
self.calculate_stdev())
Distribution.__init__(self, self.calculate_mean(), self.calculate_stdev())

def calculate_mean(self, round_to=2):
"""Function to calculate the mean from p and n
Expand Down Expand Up @@ -71,19 +71,18 @@ def replace_stats_with_data(self):

def plot_bar(self):
"""Function to output a histogram of the instance variable data using
matplotlib pyplot library.
seaborn library.

Args:
None

Returns:
None
"""

plt.bar(x=['0', '1'], height=[(1 - self.p) * self.n, self.p * self.n])
plt.title('Bar Chart of Data')
plt.xlabel('outcome')
plt.ylabel('count')
sns.barplot(x=[0, 1], y=[(1 - self.p) * self.n, self.p * self.n]).set(
title="Bar Chart of Data", xlabel="outcome", ylabel="count"
)
plt.show()

def calculate_pdf(self, k, round_to=2):
"""Probability density function calculator for the binomial distribution.
Expand Down Expand Up @@ -115,7 +114,7 @@ def calculate_cdf(self, k, round_to=2):
"""

total_p = 0
for i in range(k+1):
for i in range(k + 1):
self.calculate_pdf(i)
total_p += self.pdf
self.cdf = total_p
Expand All @@ -142,10 +141,10 @@ def plot_bar_pdf(self):
y.append(self.pdf)

# make the plots
plt.bar(x, y)
plt.title('Distribution of Outcomes')
plt.ylabel('Probability')
plt.xlabel('Outcome')

sns.barplot(x=x, y=y).set(
title="Distribution of Outcomes", ylabel="Probability", xlabel="Outcome"
)

plt.show()

Expand All @@ -162,7 +161,7 @@ def __add__(self, other):
"""

try:
assert self.p == other.p, 'p values are not equal'
assert self.p == other.p, "p values are not equal"
except AssertionError as error:
raise

Expand All @@ -184,5 +183,5 @@ def __repr__(self):
string: characteristics of the Binomial
"""

return f'mean {self.mean}, standard deviation {self.stdev}, \
p {self.p}, n {self.n}'
return f"mean {self.mean}, standard deviation {self.stdev}, \
p {self.p}, n {self.n}"
Loading