Mutual Information in a Binary Erasure Channel - information-theory

Imagine a Binary Erasure Channel as depicted on Wikipedia.
One equation describing the mutual information is following:
I(x;y)
= H(x) - H(x|y)
= H(x) - p(y=0) • 0 - p(y=?) • H(x) -p(y=1) • 0
Why is it "p(y=?) • H(x)" and not "p(y=?) • H(x|y=?)"?

It can be proved using Bayes' theorem.
The channel:
x y
1-f
0 --------> 0
\
\ f
+------> ?
/
/ 1-f
1---------> 1
Let input distribution be P(x) = {p(x=0)=g; p(x=1)=1-g}
Then:
p(x=0/y=?) = p(y=?/x=0) * p(x=0) / p(y=?)
p(x=0/y=?) = (f * g) / (f * g - f * (1 - g)) = g;
p(x=1/y=?) = p(y=?/x=1) * p(x=1) / p(y=?)
p(x=1/y=?) = (f * (1 - g)) / (f * g - f * (1 - g)) = 1 - g;
As result:
p(x=0/y=?) = p(x=0)
p(x=1/y=?) = p(x=1)
From the defenitions of entropy and conditional entropy:
H(X) = p(x=0) * log(1 / p(x=0)) + p(x=1) * log(1 / p(x=1))
H(X/y=?) = p(x=0/y=?) * log(1 / p(x=0/y=?)) + p(x=1/y=?) * log(1 / p(x=1/y=?))
So:
H(X) = H(X/y=?)

Related

plotting Hamid Naderi Yeganehs parrot using ggplot

I am a) new to stackoverflow and b) an advanced beginner to R ;-)
i saw some bird artworks of Yeganeh with the associated functions in the web Drawing Birds in Flight With Mathematics and wanted to reproduce them in R to experiment a bit with colouring and so on.
However, while this one yielded a quite good result:
k <- 1:9830
X <- function(k) {
sin(pi * k / 20000) ^ 12 *
(0.5 * cos(31 * pi * k / 10000) ^ 16 *
sin(6 * pi * k / 10000) + (1 / 6 * sin(31 * pi * k / 10000)) ^ 20) +
3 * k / 20000 + cos(31 * pi * k / 10000) ^ 6 *
sin((pi / 2) * ((k - 10000) / 10000) ^ 7 - pi / 5)
}
Y <- function(k) {
-9 / 4 * cos(31 * pi * k / 10000) ^ 6 *
cos(pi / 2 * ((k - 10000) / 10000) ^ 7 - pi / 5) *
(2 / 3 + (sin(pi * k / 20000) * sin(3 * pi * k / 20000)) ^ 6) +
3 / 4 * cos(3 * pi * ((k - 10000) / 100000)) ^ 10 *
cos(9 * pi * ((k - 10000) / 100000)) ^ 10 *
cos(36 * pi * ((k - 10000) / 100000)) ^ 14 +
7 / 10 * ((k - 10000) / 10000) ^ 2
}
R <- function(k) {
sin(pi * k / 20000) ^ 10 *
(1 / 4 * cos(31 * pi * k / 10000 + 25 * pi / 32) ^ 20 +
1 / 20 * cos(31 * pi * k / 10000) ^ 2) +
1 / 30 * (3 / 2 - cos(62 * pi * k / 10000) ^ 2)
}
bird <- data.frame(x = X(k), y = Y(k), r = R(k))
library(tidyverse)
library(ggforce)
q <- ggplot() +
geom_circle(aes(x0 = x, y0 = y, r = r),
data = bird,
n = 30) +
coord_fixed() +
theme_void()
the following code yielded some weird result which should basically be related to the difference in the function. (x-A(k))+(y-B(k))=(R(k)) for the parrot below, whlie the bird above "simply" consisted of the k-th circle (X(k), Y(k)) and the radius of the k-th circle R(k)
k <- -10000:10000
A <- function(k) {
(3*k/20000)+(cos(37*pi*k/10000))*sin((k/10000)*(3*pi/5))+(9/7)*(cos(37*pi*k/10000))*(cos(pi*k/20000))*sin(pi*k/10000)
}
B <- function(k) {
(-5/4)*(cos(37*pi*k/10000))*cos((k/10000)*(3*pi/5))*(1+3*(cos(pi*k/20000)*cos(3*pi*k/20000)))+(2/3)*(cos(3*pi*k/200000)*cos(9*pi*k/200000)*cos(9*pi*k/100000))
}
R <- function(k) {
(1/32)+(1/15)*(sin(37*pi*k/10000))*((sin(pi*k/10000))+(3/2)*(cos(pi*k/20000)))
}
parrot <- data.frame(a = A(k), b = B(k), r = R(k))
q <- ggplot() +
geom_circle(aes(x0 = a, y0 = b, r = r),
data = parrot,
n=30) +
coord_fixed() +
theme_void()
q
Any help would be very much appreciated. Cartesian coords already applied as [explained here] (https://www.wikiwand.com/en/Hamid_Naderi_Yeganeh). From the visual point of view, it seems like the function is plotted properly but the "view" on it needs to be changed...
Thanks in advance!

Find maximum angle of box in slot

How would I find the maximum possible angle (a) which a rectangle of width (W) can be at within a slot of width (w) and depth (h) - see my crude drawing below
Considering w = hh + WW at the picture:
we can write equation
h * tan(a) + W / cos(a) = w
Then, using formulas for half-angles and t = tan(a/2) substitution
h * 2 * t / (1 - t^2) + W * (1 + t^2) / (1 - t^2) = w
h * 2 * t + W * (1 + t^2) = (1 - t^2) * w
t^2 * (W + w) + t * (2*h) + (W - w) = 0
We have quadratic equation, solve it for unknown t, then get critical angle as
a = 2 * atan(t)
Quick check: Python example for picture above gives correct angle value 18.3 degrees
import math
h = 2
W = 4.12
w = 5
t = (math.sqrt(h*h-W*W+w*w) - h) / (W + w)
a = math.degrees(2 * math.atan(t))
print(a)
Just to elaborate on the above answer as it is not necessarly obvious, this is why why you can write equation:
h * tan(a) + W / cos(a) = w
PS: I suppose that the justification for "why a is the maximum angle" is obvious

Error thrown when adding vectorised constraints to JuMP

I am trying to reproduce this model - the code in the tutorial is for an old version of JuMP/Julia and does not run.
However, when I try to add the constraint:
#constraint(model, con, c[i = 1:N] .== ( ((1 - τ) * (1 - l[i]) .* w[i]) + e[i]))
I get the error Unexpected assignment in expression 'c[i = 1:N]'.
Here is the reprex:
using Random
using Distributions
using JuMP
using Ipopt
Random.seed!(123)
N = 1000
γ = 0.5
τ = 0.2
ϵ = rand(Normal(0, 1), N)
wage = rand(Normal(10, 1), N)
consumption = (γ * (1 - τ) * wage) + (γ * ϵ)
leisure = (1 - γ) .+ (( 1 - γ) * ϵ) ./ (( 1 - τ ) * wage)
model = Model(Ipopt.Optimizer)
#variable(model, c[i = 1:N] >= 0)
#variable(model, 0 <= l[i = 1:N] <= 1)
#constraint(model, con, c[i = 1:N] .== ( ((1 - τ) * (1 - l[i]) .* w[i]) + e[i]))
#NLobjective(model, Max, sum(γ *log(c[i]) + (1-γ)*log(l[i]) for i in 1:N ) )
Does anyone know why this is being thrown and how to fix it?
Any and all help appreciated!
Running Julia 1.5.1
With the c[i = 1:N] in JuMP yo can only define variables.
With the constraints one way you could do is just:
w = wage # not in your code
e = ϵ # not in your code
#constraint(model, con[i = 1:N], c[i] == ( ((1 - τ) * (1 - l[i]) .* w[i]) + e[i]))
Przemyslaw's answer is a good one. If you want to stick with the vectorized syntax, you can go
N = 1_000
e = rand(N)
w = rand(N)
τ = 0.2
model = Model()
#variable(model, c[i = 1:N] >= 0)
#variable(model, 0 <= l[i = 1:N] <= 1)
#constraint(model, c .== (1 - τ) .* (1 .- l) .* w .+ e)
Here is the JuMP documentation for constraints https://jump.dev/JuMP.jl/stable/constraints

How I find the total area of a triangle from the areas of three sections out of six created by concurrent lines one from each vertex?

In this triange:
Given the areas of triangles UPZ ZPW and WPY, how do you calculate the total area?
I've already found the solution from the available submissions at the website. But I want to know how to derive that solution.
cin >> a >> b >> c;
// a is UPZ, b is ZPW, c is WPY
double n = b*(a+b)*(a+b+c);
double d = b*(a+b)-(a*c);
cout << (n / d) ;
Indeed, this question is kind of off topic, it is a geometry problem. The way to find the area of the big triangle UVW is to apply the link between areas and ratioes of lengths of the segments of the triangle UYW and then to apply Menelaus' theorem to derive the ratio
WY/WV which reveals the ratio between the areas of the triangle UYW and UVW.
Let h_p be the length of the height from point P to the edge UW. Then
a = UZ * h_p / 2 and b = ZW * h_p / 2
Thus:
a / b = (UZ * h_p / 2) / (ZW * h_p / 2) = UZ / ZW
Let h_W be the length of the height from point W to the line UY
a + b = Area(WPU) = PU * h_W / 2 and c = YP * h_w / 2
Thus:
c / (a + b) = (YP * h_W / 2) / (PU * h_W / 2) = YP / PU
By Menelaus' theorem for the triangle UWY and the line VZ, with P on VZ, we get:
1 = ( VW / VY ) * ( YP / PU ) * ( UZ / ZW ) = ( VW / WY ) * (c / (a + b)) * (a / b)
so
VY / VW = (c * a) / ( b * (a + b))
and therefore:
WY / VW = 1 - (VY / VW) = 1 - (c*a) / ( b*(a + b)) = (a*b + b^2 - a*c ) / (a*b + b^2)
Let h_U be the length of the height from the point U to the edge VW. Then
Area(UVW) = VW * h_U / 2
and
Area(UYW) = a + b + c = WY * h_U / 2
Hence
Area(UVW) / Area(UYW) = Area(UVW) / (a + b + c) = (VW * h_U / 2) / (WY * h_U / 2) = VW / WY
so
Area(UVW) / Area(UYW) = VW / WY = (a*b + b^2) / (a*b + b^2 - a*c)
Area(UVW) / Area(UYW) = Area(UVW) / (a + b + c) = (a*b + b^2) / (a*b + b^2 - a*c)
Finally, we obtain the formula:
Area(UVW) = (a + b + c) * (a*b + b^2) / (a*b + b^2 - a*c)
Area(UVW) = b * (a + b) * (a + b + c) / (b*(a + b) - a*c)

Calculating the right number of bits in a bloom filter

I'm trying to make a configurable bloom filter. In the constructor you set the predicted necessary capacity of the filter (n), the desired error rate (p), and a list of hash functions (of size k).
According to Wikipedia, the following relation holds (m being the number of bits):
p = (1 - k * n / m) ** k
Since I get p, n and k as parameters, I need to solve for m; I get the following:
m = k * n / (1 - p ** (1 / k))
However, there are a few things that make me think I did something wrong. For starters, p ** (1 / k) will tend towards 1 for a large enough k, which means the whole fraction is ill defined (because you can conceivably divide by 0).
Another thing you may notice is that as p (the allowed maximum error rate) grows, so does m, which is totally backwards.
Where did I go wrong?
You did solve the equation correctly, however note that Wikipedia states:
The probability of all of them being 1, which would cause
the algorithm to erroneously claim that the element is in
the set, is often given as:
p ~= (1 - (1 - 1 / m) ** (k * n)) ** k ~= (1 - Exp(-k * n / m)) ** k
This is very different from what you've stated:
p = (1 - k * n / m) ** k
So what you really want to start with is
p = (1 - (1 - 1 / m) ** (k * n)) ** k
I worked this out to be
(1 - 1 / m) ** (k * n) = 1 - p ** (1 / k)
1 - 1 / m = (1 - p ** (1 / k)) ** (1 / (k * n))
m - 1 = m * (1 - p ** (1 / k)) ** (1 / (k * n))
m - m * (1 - p ** (1 / k)) ** (1 / (k * n)) = 1
m * (1 - (1 - p ** (1 / k)) ** (1 / (k * n))) = 1
m = 1 / (1 - (1 - p ** (1 / k)) ** (1 / (k * n)))

Resources