-
-
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
Changes from 9 commits
710f64e
32279e0
8fb17b5
0f872bf
6c3d5a2
77c3a91
ba59bad
c9e1d69
61661ad
a6d6ad8
6a26182
eea508d
acebaa0
53f630b
8ce6629
257ea79
4b07535
232b7b8
b301636
824a378
a3d1b12
5a86c99
f99e4d5
d80e546
6a4b223
a0c7fdb
198558b
cc3db54
848077e
c453741
7a94fe2
e5e5a91
544b648
463c067
a9b592f
bb64341
74ca473
d853110
c5bcbb1
66c5acb
9168beb
26cf878
c1009e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, y_values, x_probabilities, y_probabilities) -> dict: | ||
leonado10000 marked this conversation as resolved.
Show resolved
Hide resolved
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, probabilities) -> float: | ||
leonado10000 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 Please provide type hint for the parameter: Please provide type hint for the parameter: 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 Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
""" | ||
>>>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, probabilities) -> float: | ||
leonado10000 marked this conversation as resolved.
Show resolved
Hide resolved
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, y_values, x_probabilities, y_probabilities) -> float: | ||
leonado10000 marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
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) -> float: | ||
leonado10000 marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
|
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