-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
[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
Changes from 12 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
710f64e
Create joint_probability_distribution.py
leonado10000 32279e0
Update joint_probability_distribution.py
leonado10000 8fb17b5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0f872bf
Update joint_probability_distribution.py
leonado10000 6c3d5a2
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 77c3a91
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ba59bad
Merge branch 'TheAlgorithms:master' into master
leonado10000 c9e1d69
Update joint_probability_distribution.py
leonado10000 61661ad
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 a6d6ad8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6a26182
Update joint_probability_distribution.py
leonado10000 eea508d
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 acebaa0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 53f630b
Update joint_probability_distribution.py
leonado10000 8ce6629
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 257ea79
Update joint_probability_distribution.py
leonado10000 4b07535
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 232b7b8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b301636
Update joint_probability_distribution.py
leonado10000 824a378
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 a3d1b12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5a86c99
Update joint_probability_distribution.py
leonado10000 f99e4d5
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 d80e546
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6a4b223
Update joint_probability_distribution.py
leonado10000 a0c7fdb
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 198558b
Update joint_probability_distribution.py
leonado10000 cc3db54
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 848077e
Update joint_probability_distribution.py
leonado10000 c453741
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 7a94fe2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e5e5a91
Update joint_probability_distribution.py
leonado10000 544b648
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 463c067
Update joint_probability_distribution.py
leonado10000 a9b592f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bb64341
Update maclaurin_series.py
leonado10000 74ca473
Merge branch 'master' of https://github.com/leonado10000/Python
leonado10000 d853110
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c5bcbb1
Revert changes to maclaurin_series.py
cclauss 66c5acb
Revert changes to maclaurin_series.py
cclauss 9168beb
Update joint_probability_distribution.py
cclauss 26cf878
Update joint_probability_distribution.py
cclauss c1009e5
Update joint_probability_distribution.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# https://en.wikipedia.org/wiki/Joint_probability_distribution | ||
# Function to calculate the joint probability distribution | ||
def calculate_joint_probability(x_values : list, y_values : list, x_probabilities: list, y_probabilities: list) -> dict: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>>calculate_joint_probability([1,2],[-2,5,8],[0.7,0.3],[0.3,0.5,0.2]) | ||
P(X=1, Y=-2) = 0.21 | ||
P(X=1, Y=5) = 0.35 | ||
P(X=1, Y=8) = 0.13999999999999999 | ||
P(X=2, Y=-2) = 0.09 | ||
P(X=2, Y=5) = 0.15 | ||
P(X=2, Y=8) = 0.06 | ||
P(X=1, Y=-2) = 0.21 | ||
P(X=1, Y=5) = 0.35 | ||
P(X=1, Y=8) = 0.13999999999999999 | ||
P(X=2, Y=-2) = 0.09 | ||
P(X=2, Y=5) = 0.15 | ||
P(X=2, Y=8) = 0.06 | ||
|
||
""" | ||
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 | ||
print(joint_distribution) | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return joint_distribution | ||
|
||
|
||
# Function to calculate the expectation (mean) | ||
def expectation(values: list, probabilities: list) -> float: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>>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: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>>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: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
return variance**0.5 | ||
|
||
|
||
# Input values for X and Y | ||
leonado10000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() | ||
|
||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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}") | ||
leonado10000 marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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