Skip to content

[ADD] : maths joint probabilty distribution #10508

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

Merged
merged 43 commits into from
Oct 15, 2023
Merged
Changes from 14 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
710f64e
Create joint_probability_distribution.py
leonado10000 Oct 15, 2023
32279e0
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
8fb17b5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
0f872bf
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
6c3d5a2
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
77c3a91
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
ba59bad
Merge branch 'TheAlgorithms:master' into master
leonado10000 Oct 15, 2023
c9e1d69
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
61661ad
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
a6d6ad8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
6a26182
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
eea508d
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
acebaa0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
53f630b
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
8ce6629
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
257ea79
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
4b07535
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
232b7b8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
b301636
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
824a378
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
a3d1b12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
5a86c99
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
f99e4d5
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
d80e546
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
6a4b223
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
a0c7fdb
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
198558b
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
cc3db54
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
848077e
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
c453741
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
7a94fe2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
e5e5a91
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
544b648
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
463c067
Update joint_probability_distribution.py
leonado10000 Oct 15, 2023
a9b592f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
bb64341
Update maclaurin_series.py
leonado10000 Oct 15, 2023
74ca473
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 Oct 15, 2023
d853110
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
c5bcbb1
Revert changes to maclaurin_series.py
cclauss Oct 15, 2023
66c5acb
Revert changes to maclaurin_series.py
cclauss Oct 15, 2023
9168beb
Update joint_probability_distribution.py
cclauss Oct 15, 2023
26cf878
Update joint_probability_distribution.py
cclauss Oct 15, 2023
c1009e5
Update joint_probability_distribution.py
cclauss Oct 15, 2023
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
124 changes: 124 additions & 0 deletions maths/joint_probability_distribution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# https://en.wikipedia.org/wiki/Joint_probability_distribution

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: maths/joint_probability_distribution.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.
tokenizer error: unterminated string literal

# https://en.wikipedia.org/wiki/Joint_probability_distribution
^

# Function to calculate the joint probability distribution
def calculate_joint_probability(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/joint_probability_distribution.py, please provide doctest for the function calculate_joint_probability

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/joint_probability_distribution.py, please provide doctest for the function calculate_joint_probability

x_values: list, y_values: list, x_probabilities: list, y_probabilities: list
) -> dict:
"""
>>>calculate_joint_probability([1],[1],[1],[1])
{(1,1):1}

"""
joint_distribution = {}

# Calculate the joint probability for all combinations of (X, Y)
for x, x_prob in zip(x_values, x_probabilities):
for y, y_prob in zip(y_values, y_probabilities):
joint_prob = x_prob * y_prob
joint_distribution[(x, y)] = joint_prob
return joint_distribution


# Function to calculate the expectation (mean)
def expectation(values: list, probabilities: list) -> float:
"""
>>>expectation([1,2],[0.7,0.3])
1.2999999999999999999999

"""
return sum(x * p for x, p in zip(values, probabilities))


# Function to calculate the variance
def variance(values: list, probabilities: list) -> float:
"""
>>>variance([1,2],[0.7,0.3])
0.21000
"""
mean = expectation(values, probabilities)
return sum((x - mean) ** 2 * p for x, p in zip(values, probabilities))


# Function to calculate the covariance
def covariance(
x_values: list, y_values: list, x_probabilities: list, y_probabilities: list
) -> float:
"""
>>>covariance([],[],[],[])

"""
mean_x = expectation(x_values, x_probabilities)
mean_y = expectation(y_values, y_probabilities)
return sum(
(x - mean_x) * (y - mean_y) * px * py
for x, px in zip(x_values, x_probabilities)
for y, py in zip(y_values, y_probabilities)
)


# Function to calculate the standard deviation
def standard_deviation(variance: list) -> float:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file maths/joint_probability_distribution.py, please provide doctest for the function standard_deviation

"""
>>>variance(4)
2

"""
return variance**0.5


# Input values for X and Y
x_values = input("Enter values of X separated by spaces: ").split()
y_values = input("Enter values of Y separated by spaces: ").split()

# Convert input values to integers
x_values = [int(x) for x in x_values]
y_values = [int(y) for y in y_values]

# Input probabilities for X and Y
x_probabilities = input("Enter probabilities for X separated by spaces: ").split()
y_probabilities = input("Enter probabilities for Y separated by spaces: ").split()

# Convert input probabilities to floats
x_probabilities = [float(p) for p in x_probabilities]
y_probabilities = [float(p) for p in y_probabilities]

# Calculate the joint probability distribution
joint_distribution = calculate_joint_probability(
x_values, y_values, x_probabilities, y_probabilities
)

# Print the joint probability distribution
for (x, y), probability in joint_distribution.items():
print(f"P(X={x}, Y={y}) = {probability}")


# Calculate the joint probability distribution
joint_distribution = calculate_joint_probability(
x_values, y_values, x_probabilities, y_probabilities
)

# Print the joint probability distribution
for (x, y), probability in joint_distribution.items():
print(f"P(X={x}, Y={y}) = {probability}")

# Calculate the statistics
mean_x = expectation(x_values, x_probabilities)
mean_y = expectation(y_values, y_probabilities)
mean_xy = expectation(
[x * y for x in x_values for y in y_values],
[px * py for px in x_probabilities for py in y_probabilities],
)
variance_x = variance(x_values, x_probabilities)
variance_y = variance(y_values, y_probabilities)
cov_xy = covariance(x_values, y_values, x_probabilities, y_probabilities)
stddev_x = standard_deviation(variance_x)
stddev_y = standard_deviation(variance_y)

# Print the results
print(f"Expectation (mean) of X: {mean_x}")
print(f"Expectation (mean) of Y: {mean_y}")
print(f"Expectation (mean) of XY: {mean_xy}")
print(f"Variance of X: {variance_x}")
print(f"Variance of Y: {variance_y}")
print(f"Covariance between X and Y: {cov_xy}")
print(f"Standard Deviation of X: {stddev_x}")
print(f"Standard Deviation of Y: {stddev_y}")