I have got a simple problem to calculate Posterior after some signal disturbances.

Say the input signal is a sequence of 0 and 1 with 70% of chance is 1. And somehow we know that after the transmission, the output have 0 an 1 of equal chance. I want to calculate what the probability when we get 1 in output and it is truly 1 in the input.

I write the following code to implement the simulation. I want to randomly set seeds and see what happened to my simulation.

```
using Random, StatsBase
k = rand(1:2000);
Random.seed!(k);
t = sample(0:1,weights([0.3,0.7]),1000);
Random.seed!(k);
r = sample(0:1,weights([0.5,0.5]),1000);
Post = sum(t[r.==1])/sum(r[r.==1])
Prior =sum(r[t.==1])/sum(t[t.==1])
```

The result is not right, with Post always 1.0 and Prior around 0.7.

I am thinking of several potential problems:

- I was conceptually wrong to implement the simulation this way. Maybe r is not the correct realization of t after disturbance, and I should modify t base on my knowledge of the disturbance instead of generating r directly.
- I used sample() in a wrong way.
- I calculated Post and Prior wrongly.

Please help me out on this simulation problem.

I am not an expert at all, but my guess would be:

```
Prior = sum(t[t.==1])/length(t)
Post = sum(t[r.==1 .& t.==1])/sum(t[t.==1])
```

and both should read ~0.7.

I.e., the probability of the input being in state 1 given that the transmission was in state 1, should be 0.7 because the transmission is not changing the two-state probabilities. Seems analogous to the red-yellow cab problem well explained here Bayes and the cabs.

I think the problem is not fully specified. You don’t link the output with the input, you only talk about their individual distributions:

So you have a probability vector before transmission `[0.7 0.3]`

, and transmission procedure representable as 2x2 stochastic matrix `Q`

and you tell us that `[0.7 0.3]*Q = [0.5 0.5]`

. There are many `Q`

which give you that.

```
julia> Q(λ) = [λ 1-λ; 5/3-7λ/3 1-( 5/3-7λ/3)]
Q (generic function with 1 method)
julia> Q(0.4)
2×2 Array{Float64,2}:
0.4 0.6
0.733333 0.266667
julia> [0.7 0.3]*Q(0.4)
1×2 Array{Float64,2}:
0.5 0.5
```

Here the entries of this particular `Q = [ 0.4 0.6; 0.733 0.267]`

specify that a `1`

gets corrupted to a 0 with probability 0.6 and a zero `0`

gets corrupted to a `1`

with probability `0.7333`

.

1 Like

PS:

```
n = 10000
p = [0.7,0.3]
t = sample(1:-1:0, weights(p), n)
Q(λ) = [λ 1-λ; 5/3-7λ/3 1-( 5/3-7λ/3)]
λ = 0.4
r = [sample(1:-1:0, weights(vec(Q(λ)[i==1 ? 1 : 2, :]))) for i in t]
```

Then the conditional probability of being 1 given that that 1 was observed is computed and estimated as follows

```
julia> prob = (p[1]*Q(λ)[1,1])/(p[1]*Q(λ)[1,1] + p[2]*Q(λ)[2, 1])
0.56
julia> prob_est = dot(t,r)/n/mean(r)
0.5670391061452514
```

1 Like

Thank you, @mschauer, very clear explanation and helpful solution, too!

There are additional info on the disturbance process for us to model r.

It seems that independently generate r is not the correct solution. I will try as you suggested.