Telechargé par abdi chakib (ghost0gamer)

tp1 codage

publicité
TP1:simulation de
la theorie de
l’information
2023
TP1:simulation de la theorie de
l’information
1-objectivf du tp:
Il s’agit ici de simuler les differents parametres qui caracterisent la theorie de l’intformation a
savoir, le contenu information.l’entropie (contenu informatif moyen). L’entropie conjoint et
conditionnelle ainsi l’information mutuelle.
2-travail théorique:
P(x1)=0.3 ,P(x2)=0.25 ,P(x3)=0.2 ,P(x4)=0.12 ,P(x5)=0.08 ,P(x6)=0.05
1/calcule de le contenu informatif I(x) de cette source x:
𝐼(𝑋) =− 𝑙𝑜𝑔(𝑃(𝑋))
Pour X=1,P(X=1); 𝐼(𝑋1) =− 𝑙𝑜𝑔(0. 3) = 1. 73
Pour X=2,P(X=2); 𝐼(𝑋1) =− 𝑙𝑜𝑔(0. 25) = 2
Pour X=3,P(X=3); 𝐼(𝑋1) =− 𝑙𝑜𝑔(0. 2) = 2. 32
Pour X=4,P(X=4); 𝐼(𝑋1) =− 𝑙𝑜𝑔(0. 12) = 3. 05
Pour X=5,P(X=5); 𝐼(𝑋1) =− 𝑙𝑜𝑔(0. 08) = 3. 64
Pour X=6,P(X=6); 𝐼(𝑋1) =− 𝑙𝑜𝑔(0. 05) = 4. 32
2/calculer l’entropie H(X) de cette source:
𝐻(𝑋) =− [𝑋1𝑙𝑜𝑔2(𝑋1) + 𝑋2𝑙𝑜𝑔2(𝑋2) + 𝑋3𝑙𝑜𝑔2(𝑋3) + 𝑋4𝑙𝑜𝑔2(𝑋4) + 𝑋5𝑙𝑜𝑔2(𝑋5) + 𝑋6𝑙𝑜𝑔2(𝑋6)]
𝐻(𝑋) =− [0. 3𝑙𝑜𝑔2(0. 3) + 0. 25𝑙𝑜𝑔2(0. 25) + 0. 2𝑙𝑜𝑔2(0. 2) + 0. 12𝑙𝑜𝑔2(0. 12) + 0. 3𝑙𝑜𝑔2(0. 3) + 0. 3𝑙𝑜𝑔2(0. 3
𝐻(𝑋) = 2. 36
0 ≤ 𝐻(𝑋) ≤ 𝑙𝑜𝑔2(𝑚)
0 ≤ 2. 36 ≤ 2. 58
3/calcule la probabilite de sortie P(Y) et l’entropie H(Y):
𝑃(𝑌) = 𝑃(𝑋)𝑃(𝑌/𝑋) = [0. 05 0. 05] [0. 9 0. 1 ; 0. 2 0. 8]
𝑃(𝑌) = [0. 55 0. 45]
𝐻(𝑌) =− ∑ 𝑃(𝑌)𝑙𝑜𝑔2(𝑃(𝑌))
𝐻(𝑌) =− [0. 55𝑙𝑜𝑔2(0. 55) + 0. 45𝑙𝑜𝑔2(0. 45)]
𝐻(𝑌) = 0. 99 𝑏𝑖𝑡𝑠/𝑠𝑦𝑚𝑏𝑜𝑙𝑒
4/calcule de la probabilite conjointe P(X,Y) et l’entropie conjointe H(Y/X):
𝑃(𝑋, 𝑌) = 𝑃(𝑋)𝑑𝑃(𝑌/𝑋)
𝑃(𝑋, 𝑌) = [0. 5 0 ; 0 0. 5][0. 9 0. 1 ; 0. 2 0. 8]
𝑃(𝑋, 𝑌) = [0. 45 0. 05 ; 0. 1 0. 4]
𝐻(𝑌, 𝑋) = ∑∑ 𝑃(𝑋, 𝑌)𝑙𝑜𝑔2(𝑃(𝑋))
𝐻(𝑌, 𝑋) =− [0. 45𝑙𝑜𝑔2(0. 45) + 0. 05𝑙𝑜𝑔2(0. 05) + 0. 1𝑙𝑜𝑔2(0. 1) + 0. 4𝑙𝑜𝑔1(0. 4)]
𝐻(𝑌, 𝑋) = 1. 59 𝑏𝑖𝑡𝑠/𝑠𝑦𝑚𝑏𝑜𝑙𝑒
5/calcule l’entropie conditionnelle H(Y/X):
𝐻(𝑌/𝑋) =− ∑∑ 𝑃(𝑋, 𝑌)𝑙𝑜𝑔2(𝑝(𝑌/𝑋))
𝐻(𝑌/𝑋) =− [0. 45𝑙𝑜𝑔2(0. 9) + 0. 05𝑙𝑜𝑔2(0. 1) + 0. 1𝑙𝑜𝑔2(0. 2) + 0. 4𝑙𝑜𝑔2(0. 8)]
𝐻(𝑌/𝑋) = 0. 59 𝑏𝑖𝑡𝑠/𝑠𝑦𝑚𝑏𝑜𝑙𝑒
6/calcule l’information mutulle I(x;Y):
𝐼(𝑋; 𝑌) = 𝐻(𝑌) − 𝐻(𝑌/𝑋)
𝐼(𝑋; 𝑌) = 0. 99 − 0. 59
𝐼(𝑋; 𝑌) = 0. 4 𝑏𝑖𝑡𝑠/𝑠𝑦𝑚𝑏𝑜𝑙𝑒
2-implémentation sur matlab:
1/
clc; % Efface la fenêtre de commande
clear all; % Supprime toutes les variables de l'espace de travail
close all; % Ferme toutes les fenêtres de figures ouvertes
p=[0.3 0.25 0.2 0.12 0.08 0.05]; % Définit un vecteur de probabilités
discrètes
disp('P(X)='); % Affiche une chaîne de caractères
disp(p); % Affiche le vecteur de probabilités p
i=length(p); % Définit la longueur du vecteur p et la stocke dans la
variable i
I=(-log2(p)); % Calcule le contenu informatif de chaque symbole du vecteur
p
disp('Contenu informatif de l''entrée:'); % Affiche une chaîne de
caractères
disp('I(X)='); % Affiche une chaîne de caractères
disp(I); % Affiche le contenu informatif de chaque symbole du vecteur p
sum=0; % Initialise la variable sum à zéro
for n=1:i % Pour chaque élément dans le vecteur p
HX=sum+(p(n)*log2(1/p(n))); % Calcule l'entropie de l'entrée
sum=HX; % Stocke l'entropie dans la variable sum pour la sommation des
éléments suivants
end
disp('entropie de l''entrée:'); % Affiche une chaîne de caractères
disp(HX); % Affiche l'entropie de l'entrée
PYbyPX=[0.9 0.1; 0.2 0.8]; % Définit une matrice de transition pour une
source binaire
px=[0.5 0.5]; % Définit la distribution de probabilité marginale de la
source binaire
py=px*PYbyPX; % Calcule la distribution de probabilité marginale de la
sortie
disp('la probabilite de sortie:'); % Affiche une chaîne de caractères
disp('p(y)='); % Affiche une chaîne de caractères
disp(py); % Affiche la distribution de probabilité marginale de la sortie
sum=0; % Initialise la variable sum à zéro
for n=1:length(py) % Pour chaque élément dans le vecteur py
hy=sum+(py(n)*log2(1/py(n))); % Calcule l'entropie de la sortie
sum=hy; % Stocke l'entropie dans la variable sum pour la sommation des
éléments suivants
end
disp('entropie de sortie:'); % Affiche une chaîne de caractères
disp(hy); % Affiche l'entropie de la sortie
pxy=diag(px)*PYbyPX; % Calcule la distribution de probabilité conjointe de
l'entrée et de la sortie
disp('la probabilitie conjointe:'); % Affiche une chaîne de caractères
disp('p(x,y)='); % Affiche une chaîne de caractères
disp(pxy); % Affiche la distribution de probabilité conjointe de l'entrée
et de la sortie
sum=0; % Initialise la variable sum à zéro
for n=1:length(pxy)% Pour chaque élément dans le vecteur pxy
for m=1:length(PYbyPX)% Pour chaque élément dans le vecteur PYbyPX
hyx=sum+(pxy(n,m)*log2(1/PYbyPX(n,m)));% Calcule entropie
conditionelle
sum=hyx;
end
end
disp('entropie conditionelle:');
disp('h(y/x)=');% Affiche h(y/x)
disp(hyx);% Affiche valeur entropie conditionelle
ixy=hy-hyx;% Calcule 'linformation mutuelle
disp('linformation mutuelle:');% Affiche linformation mutuelle
disp(ixy);%affiche valeur linformation mutuelle
les resultats
P(X)=
0.3000
👍
0.2500
0.2000
0.1200
0.0800
0.0500
Contenu informatif de l'entrée:
I(X)=
1.7370 2.0000 2.3219 3.0589
3.6439
4.3219
entropie de l'entree:
2.3601
la probabilite de sortie:
p(y)=
0.5500 0.4500
entropie de sortie:
0.9928
la probabilitie conjointe:
p(x,y)=
0.4500 0.0500
0.1000 0.4000
entropie conditionelle:
h(y/x)=
0.5955
linformation mutuelle:
0.3973
Les résultats montrent que l'entrée a une entropie de 2.36 bits, ce qui est inférieur au contenu
informatif de chaque symbole du vecteur d'entrée. Cela indique que certaines entrées sont plus
probables que d'autres, ce qui réduit l'incertitude associée à l'entrée. La sortie a une entropie de
0.99 bits, ce qui est inférieur à l'entropie de l'entrée, ce qui signifie que l'information a été
réduite lors de la transmission de l'entrée à la sortie. La probabilité de sortie montre que la
sortie est plus probable lorsque l'entrée est 0.5, ce qui est également la probabilité marginale de
l'entrée. L'entropie conditionnelle montre la quantité d'incertitude dans la sortie lorsque l'entrée
est connue, ce qui est inférieur à l'entropie de la sortie, ce qui signifie que l'information contenue
dans l'entrée aide à réduire l'incertitude de la sortie. Enfin, l'information mutuelle mesure la
quantité d'information partagée entre l'entrée et la sortie, ce qui est de 0.3973 bits, ce qui est
une mesure de l'efficacité de la transmission de l'information.
2/P(X)=
Columns 1 through 6
0.2000
0.1500
0.0600
0.1000
0.4000
0.0500
3.3219
1.3219
4.3219
Columns 7 through 8
0.0300
0.0100
Contenu informatif de l'entrée:
I(X)=
Columns 1 through 6
2.3219
2.7370
4.0589
Columns 7 through 8
5.0589
6.6439
entropie de l'entree:
2.4137
la probabilite de sortie:
p(y)=
0.5500 0.4500
entropie de sortie:
0.9928
la probabilitie conjointe:
p(x,y)=
0.4500 0.0500
0.1000 0.4000
entropie conditionelle:
h(y/x)=
0.5955
linformation mutuelle:
0.3973
La principale différence entre les deux ensembles de données est la distribution de probabilité
de la variable aléatoire X. Dans le premier ensemble de données, la probabilité est plus
uniforme, tandis que dans le second ensemble de données, la probabilité est plus concentrée
sur quelques valeurs de X. Cela se reflète dans la valeur de l'entropie de l'entrée, qui est plus
élevée dans le premier ensemble de données que dans le second.ainsi le contenue informatif
est dffirent que le premiere script Cependant, la probabilité de sortie, l'entropie de sortie, la
probabilité conjointe, l'entropie conditionnelle et l'information mutuelle sont les mêmes dans les
deux ensembles de données.
explication de code:
Ce code MATLAB permet de calculer plusieurs mesures d'information pour une source
discrète et une source binaire.
Tout d'abord, le code commence par initialiser l'environnement MATLAB en effaçant la fenêtre
de commande, en supprimant toutes les variables de l'espace de travail et en fermant toutes
les figures ouvertes.
Ensuite, le code définit un vecteur de probabilités discrètes pour la source discrète et une
matrice de transition pour la source binaire. Il calcule ensuite le contenu informatif et l'entropie
de l'entrée pour la source discrète, ainsi que la distribution de probabilité marginale, l'entropie
et l'information mutuelle pour la source binaire.
Pour calculer l'entropie de l'entrée, le code utilise une boucle pour parcourir chaque symbole
dans le vecteur de probabilités et calcule l'entropie de l'entrée en utilisant la formule de
Shannon. Pour la source binaire, le code utilise également une boucle pour parcourir chaque
symbole dans la distribution de probabilité marginale et calcule l'entropie de la sortie en
utilisant la formule de Shannon.
Le code calcule ensuite la distribution de probabilité conjointe de l'entrée et de la sortie, ainsi
que l'entropie conditionnelle et l'information mutuelle. Pour calculer l'entropie conditionnelle, le
code utilise deux boucles imbriquées pour parcourir chaque symbole dans la distribution de
probabilité conjointe et la matrice de transition, puis calcule l'entropie conditionnelle en
utilisant la formule de Shannon.
Enfin, le code affiche les résultats pour chaque mesure d'information calculée.
python version
1/# Calculate the information content I(X) with Python
import math
# Define the probability of the event
p = [0.3, 0.25, 0.2, 0.12, 0.08, 0.05]
# Calculate the information content
info_content = 0
for i in range(len(p)):
info_content = -math.log2(p[i])
# Print the result
print("Information content I(p(",p[i],"))=", info_content)
# Calculate Entropy H(X) with Python
import numpy as np
# Define the probability distribution
p = np.array([0.3, 0.25, 0.2, 0.12, 0.08, 0.05])
# Calculate the entropy H (X)
Hx = -np.sum(np.nan_to_num(p * np.log2(p)))
# Print the result
print("Entropy H(X):\n", Hx)
# Calculate the output probability P(Y) of channel
import numpy as np
# Define the channel's transition probabilities
Pc = np.array([[0.9, 0.1], [0.2, 0.8]])
# Define the input distribution
p_x = np.array([0.5, 0.5])
# Calculate the probability of each output symbol
p_y = np.sum(Pc * p_x.reshape(-1, 1), axis=0)
# Print the result
print("Output probability P(Y):\n", p_y)
# Calculate Entropy H(Y)
import numpy as np
# Calculate the entropy H(Y)
Hy = -np.sum(np.nan_to_num(p_y * np.log2(p_y)))
# Print the result
print("Entropy H(Y):\n", Hy)
# Calculate the joint probability P(X,Y)
import numpy as np
# Define the conditional probability matrix
Pc = np.array([[0.9, 0.1], [0.2, 0.8]])
# Define the marginal probability distribution of X
p_x = np.array([0.5, 0.5])
# Construct the diagonal matrix of p_x
d_x = np.diag(p_x)
# Calculate the joint probability distribution of X and Y
p_xy = d_x @ Pc
# Print the result
print("Joint probability P(X,Y):\n", p_xy)
# Calculate the joint entropy H(X,Y)
import numpy as np
# Define the joint probability distribution
p_xy = np.array([[0.45, 0.05], [0.1, 0.4]])
# Calculate the joint entropy H(X,Y)
Hxy = -np.sum(np.nan_to_num(p_xy * np.log2(p_xy)))
# Print the result
print("Joint entropy H(X,Y):\n",Hxy)
# Calculate the conditional Entropy H(Y/X)
import numpy as np
# Define the joint probability distribution of X and Y
p_xy = np.array([[0.45, 0.05], [0.1, 0.4]])
# Calculate the marginal probability distribution of X
p_x = np.sum(p_xy, axis=1)
# Calculate the conditional probability distribution of Y given X
P_y_given_x = p_xy / p_x.reshape(-1, 1)
# Calculate the conditional entropy of Y given X
Hyx = -np.sum(np.nan_to_num(p_xy * np.log2(P_y_given_x)))
# Print the result
print("Conditional entropy H(Y/X):\n", Hyx)
#Calculate the mutual Information I(X;Y)
import numpy as np
# Define the joint probability distribution of X and Y
p_xy = np.array([[0.45, 0.05], [0.1, 0.4]])
# Calculate the marginal probability distributions of X and Y
p_x = np.sum(p_xy, axis=1)
p_y = np.sum(p_xy, axis=0)
# Calculate the conditional probability distributions of Y given X
P_y_given_x = p_xy / p_x.reshape(-1, 1)
# Calculate the entropy of Y
H_y = -np.sum(np.nan_to_num(p_y * np.log2(p_y)))
# Calculate the entropy of the conditional probability distributions
H_y_given_x = -np.sum(np.nan_to_num(p_xy * np.log2(P_y_given_x)))
# Calculate the mutual information
I_xy = H_y - H_y_given_x
# Print the result
print("Mutual information I(X;Y):\n", I_xy)
Information content I(p( 0.3 ))= 1.736965594166206
Information content I(p( 0.25 ))= 2.0
Information content I(p( 0.2 ))= 2.321928094887362
Information content I(p( 0.12 ))= 3.0588936890535687
Information content I(p( 0.08 ))= 3.643856189774725
Information content I(p( 0.05 ))= 4.321928094887363
Entropy H(X):
2.3601474398401088
Output probability P(Y):
[0.55 0.45]
Entropy H(Y):
0.9927744539878083
Joint probability P(X,Y):
[[0.45 0.05]
[0.1 0.4 ]]
Joint entropy H(X,Y):
1.5954618442383217
Conditional entropy H(Y/X):
0.5954618442383217
Mutual information I(X;Y):
0.39731260974948657
The results show that the input has an entropy of 2.36 bits, which is less than the information
content of each symbol in the input vector. This indicates that some inputs are more likely than
others, which reduces the uncertainty associated with the input. The output has an entropy of
0.99 bits, which is less than the entropy of the input, which means that the information has been
reduced in the transmission from the input to the output. The output probability shows that the
output is more likely when the input is 0.5, which is also the marginal probability of the input.
The conditional entropy shows the amount of uncertainty in the output when the input is known,
which is less than the entropy of the output, meaning that the information in the input helps
reduce the uncertainty in the output. Finally, the mutual information measures the amount of
information shared between the input and the output, which is 0.3973 bits, which is a measure
of the efficiency of information transmission.
2/
# Calculate the information content I(X) with Python
import math
# Define the probability of the event
p = [0.2 , 0.15 , 0.06 , 0.1 , 0.4 , 0.05 , 0.03 , 0.01]
# Calculate the information content
info_content = 0
for i in range(len(p)):
info_content = -math.log2(p[i])
# Print the result
print("Information content I(p(",p[i],"))=", info_content)
# Calculate Entropy H(X) with Python
import numpy as np
# Define the probability distribution
p = np.array([0.3, 0.25, 0.2, 0.12, 0.08, 0.05])
# Calculate the entropy H (X)
Hx = -np.sum(np.nan_to_num(p * np.log2(p)))
# Print the result
print("Entropy H(X):\n", Hx)
# Calculate the output probability P(Y) of channel
import numpy as np
# Define the channel's transition probabilities
Pc = np.array([[0.9, 0.1], [0.2, 0.8]])
# Define the input distribution
p_x = np.array([0.5, 0.5])
# Calculate the probability of each output symbol
p_y = np.sum(Pc * p_x.reshape(-1, 1), axis=0)
# Print the result
print("Output probability P(Y):\n", p_y)
# Calculate Entropy H(Y)
import numpy as np
# Calculate the entropy H(Y)
Hy = -np.sum(np.nan_to_num(p_y * np.log2(p_y)))
# Print the result
print("Entropy H(Y):\n", Hy)
# Calculate the joint probability P(X,Y)
import numpy as np
# Define the conditional probability matrix
Pc = np.array([[0.9, 0.1], [0.2, 0.8]])
# Define the marginal probability distribution of X
p_x = np.array([0.5, 0.5])
# Construct the diagonal matrix of p_x
d_x = np.diag(p_x)
# Calculate the joint probability distribution of X and Y
p_xy = d_x @ Pc
# Print the result
print("Joint probability P(X,Y):\n", p_xy)
# Calculate the joint entropy H(X,Y)
import numpy as np
# Define the joint probability distribution
p_xy = np.array([[0.45, 0.05], [0.1, 0.4]])
# Calculate the joint entropy H(X,Y)
Hxy = -np.sum(np.nan_to_num(p_xy * np.log2(p_xy)))
# Print the result
print("Joint entropy H(X,Y):\n",Hxy)
# Calculate the conditional Entropy H(Y/X)
import numpy as np
# Define the joint probability distribution of X and Y
p_xy = np.array([[0.45, 0.05], [0.1, 0.4]])
# Calculate the marginal probability distribution of X
p_x = np.sum(p_xy, axis=1)
# Calculate the conditional probability distribution of Y given X
P_y_given_x = p_xy / p_x.reshape(-1, 1)
# Calculate the conditional entropy of Y given X
Hyx = -np.sum(np.nan_to_num(p_xy * np.log2(P_y_given_x)))
# Print the result
print("Conditional entropy H(Y/X):\n", Hyx)
#Calculate the mutual Information I(X;Y)
import numpy as np
# Define the joint probability distribution of X and Y
p_xy = np.array([[0.45, 0.05], [0.1, 0.4]])
# Calculate the marginal probability distributions of X and Y
p_x = np.sum(p_xy, axis=1)
p_y = np.sum(p_xy, axis=0)
# Calculate the conditional probability distributions of Y given X
P_y_given_x = p_xy / p_x.reshape(-1, 1)
# Calculate the entropy of Y
H_y = -np.sum(np.nan_to_num(p_y * np.log2(p_y)))
# Calculate the entropy of the conditional probability distributions
H_y_given_x = -np.sum(np.nan_to_num(p_xy * np.log2(P_y_given_x)))
# Calculate the mutual information
I_xy = H_y - H_y_given_x
# Print the result
print("Mutual information
I(X;Y):\n", I_xy)
Information content I(p( 0.2 ))= 2.321928094887362
Information content I(p( 0.15 ))= 2.736965594166206
Information content I(p( 0.06 ))= 4.058893689053568
Information content I(p( 0.1 ))= 3.321928094887362
Information content I(p( 0.4 ))= 1.3219280948873622
Information content I(p( 0.05 ))= 4.321928094887363
Information content I(p( 0.03 ))= 5.058893689053568
Information content I(p( 0.01 ))= 6.643856189774724
Entropy H(X):
2.3601474398401088
Output probability P(Y):
[0.55 0.45]
Entropy H(Y):
0.9927744539878083
Joint probability P(X,Y):
[[0.45 0.05]
[0.1 0.4 ]]
Joint entropy H(X,Y):
1.5954618442383217
Conditional entropy H(Y/X):
0.5954618442383217
Mutual information I(X;Y):
0.39731260974948657
The main difference between the two data sets is the probability distribution of the random
variable X. In the first data set, the probability is more uniform, while in the second data set, the
probability is more concentrated on a few values of X. This is reflected in the value of the input
entropy, which is higher in the first data set than in the second.Thus the informative content is
dffirent than the first script However, the output probability, output entropy, joint probability,
conditional entropy and mutual information are the same in both data sets.
Téléchargement