-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp3.py
More file actions
50 lines (42 loc) · 1.42 KB
/
p3.py
File metadata and controls
50 lines (42 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from random import random
def create_transmitted_message(prob):
num = random()
message = 0 if num <= prob else 1
return message
def create_received_signal(message, e_0, e_1):
num = random()
if message == 0:
if num <= e_0:
signal = 1
else:
signal = 0
else:
if num > e_1:
signal = 1
else:
signal = 0
return signal
def calculate_conditional_probability(e_0, e_1, num_experiments):
"""Calculates the conditional probability P(R=1|S=1).
Args:
e_0: The probability of a 0 bit being received incorrectly.
e_1: The probability of a 1 bit being received incorrectly.
num_experiments: The number of experiments to perform.
Returns:
The conditional probability P(R=1|S=1).
"""
num_successes = 0
for _ in range(num_experiments):
message = create_transmitted_message(0.35)
signal = create_received_signal(message, e_0, e_1)
if message == 1 and signal == 1:
num_successes += 1
conditional_probability = num_successes / num_experiments
return conditional_probability
# Set the transmission error probabilities.
e_0 = 0.01
e_1 = 0.02
# Calculate the conditional probability.
conditional_probability = calculate_conditional_probability(e_0, e_1, 10000)
# Print the conditional probability.
print("Conditional probability P(R=1|S=1):", conditional_probability)