Difference between revisions of "Aufgaben:Exercise 2.7: C Programs "z1" and "z2""

From LNTwww
m (Text replacement - "„" to """)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  
{{quiz-Header|Buchseite=Stochastische Signaltheorie/Erzeugung von diskreten Zufallsgrößen
+
{{quiz-Header|Buchseite=Theory_of_Stochastic_Signals/Generation_of_Discrete_Random_Variables
 
}}
 
}}
  
[[File:P_ID122__Sto_A_2_7.png|right|frame|C-Programme zur Erzeugung <br>diskreter Zufallsgrößen]]
+
[[File:P_ID122__Sto_A_2_7.png|right|frame|C programs for generating <br>discrete random variables]]
Die beiden hier angegebenen C-Programme eignen sich zur Erzeugung diskreter Zufallsgr&ouml;&szlig;en:
+
The two C programs given here are suitable for generating discrete random variables:
  
* Die Funktion&nbsp; $z1$&nbsp; erzeugt eine&nbsp; $M$&ndash;stufige Zufallsgr&ouml;&szlig;e mit dem Wertevorrat&nbsp; $\{0, 1$, ... , $M-1\}$.&nbsp; Die dazugehörigen Wahrscheinlichkeiten werden im Array&nbsp; $\text{p_array}$&nbsp; mit der Eigenschaft "Float" &uuml;bergeben.&nbsp; Die Funktion&nbsp; $\text{random()}$&nbsp; liefert gleichverteilte Float&ndash;Zufallsgr&ouml;&szlig;en zwischen&nbsp; $0$&nbsp; und&nbsp; $1$.
+
* The function&nbsp; $z1$&nbsp; generates an&nbsp; $M$&ndash;level random variable with the value set&nbsp; $\{0, 1$, ... , $M-1\}$.&nbsp; The associated probabilities are passed in the array&nbsp; $\text{p_array}$&nbsp; with property&nbsp; "Float"&nbsp; The function&nbsp; $\text{random()}$&nbsp; returns equally distributed float&ndash;random variables&nbsp; between&nbsp; $0$&nbsp; and&nbsp; $1$.
  
*Eine zweite Funktion&nbsp; $z2$&nbsp; (Quelltext siehe unten)&nbsp; liefert eine spezielle Wahrscheinlichkeitsverteilung, die durch die beiden Parameter&nbsp; $I$&nbsp; und&nbsp; $p$&nbsp; festgelegt ist.&nbsp; Dieses geschieht unter Verwendung der Funktion&nbsp; $z1$.
+
*A second function&nbsp; $z2$&nbsp; (source code see below)&nbsp; returns a special probability distribution specified by the two parameters&nbsp; $I$&nbsp; and&nbsp; $p$.&nbsp; This is done using the function&nbsp; $z1$.
  
  
  
  
 
+
Hints:
 
+
*The exercise belongs to the chapter&nbsp; [[Theory_of_Stochastic_Signals/Generation_of_Discrete_Random_Variables|Generation of Discrete Random Variables]].
 
+
*In particular, reference is made to the page&nbsp;  [[Theory_of_Stochastic_Signals/Generation_of_Discrete_Random_Variables#Generation_of_multilevel_random_variables|Generation of multilevel random variables]].
''Hinweise:''
 
*Die Aufgabe gehört zum  Kapitel&nbsp; [[Theory_of_Stochastic_Signals/Erzeugung_von_diskreten_Zufallsgrößen|Erzeugung von diskreten Zufallsgrößen]].
 
*Insbesondere wird auf die Seite&nbsp;  [[Theory_of_Stochastic_Signals/Erzeugung_von_diskreten_Zufallsgrößen#Erzeugung_mehrstufiger_Zufallsgr.C3.B6.C3.9Fen|Erzeugung mehrstufiger Zufallsgrößen]] Bezug&nbsp; genommen.
 
 
   
 
   
  
  
  
===Fragebogen===
+
===Questions===
  
 
<quiz display=simple>
 
<quiz display=simple>
{Es gelte&nbsp; $M=4$&nbsp; und&nbsp; $\text{p_array} = \big[0.2, \ 0.3, \ 0.4, \ 0.1 \big]$.  
+
{It is valid:&nbsp; $M=4$,&nbsp; &nbsp; $\text{p_array} = \big[0.2, \ 0.3, \ 0.4, \ 0.1 \big]$.  
<br>Welches Ergebnis liefert die Funktion&nbsp; $z1$, wenn die Randomfunktion den&nbsp; Wert $x = 0.75$&nbsp; zur&uuml;ckgibt?
+
<br>What result does the function&nbsp; $z1$&nbsp; return if the random function returns the&nbsp; value&nbsp; $x = 0.75$&nbsp; ?
 
|type="{}"}
 
|type="{}"}
$z1 \ = \ $ { 2 }
+
$z1 \ = \ $ { 2 }
  
{Welche der folgenden Aussagen sind bez&uuml;glich&nbsp; $z1$&nbsp; zutreffend?
+
{Which of the following statements are true regarding&nbsp; $z1$&nbsp;?
 
|type="[]"}
 
|type="[]"}
- Man könnte auf die Zuweisung&nbsp; $\text{x = random()}$&nbsp; in Zeile 5 verzichten und in Zeile 8 direkt mit&nbsp; $\text{random()}$&nbsp; vergleichen.
+
- You could omit the assignment&nbsp; $\text{x = random()}$&nbsp; in line 5 and compare directly with&nbsp; $\text{random()}$&nbsp; in line 8.
+ Sind alle übergebenen Wahrscheinlichkeiten gleich, so g&auml;be es schnellere Programmrealisierungen als&nbsp; $z1$.
+
+ If all probabilities passed  to the function are equal,&nbsp; there would be faster program realizations than&nbsp; $z1$.
+ Der R&uuml;ckgabewert&nbsp; $\text{random() = 0.2}$&nbsp; f&uuml;hrt zum Ergebnis&nbsp; $z1= 1$.
+
+ The return value&nbsp; $\text{random() = 0.2}$&nbsp; leads to the result&nbsp; $z1= 1$.
  
  
{Welche der folgenden Aussagen sind bez&uuml;glich&nbsp; $z2$&nbsp; zutreffend?
+
{Which of the following statements are true regarding&nbsp; $z2$&nbsp;?
 
|type="[]"}
 
|type="[]"}
+ Das Programm erzeugt eine <i>binomialverteilte</i>&nbsp; Zufallsgr&ouml;&szlig;e.
+
+ The program generates a&nbsp; "binomially distributed random variable".
- Das Programm erzeugt eine <i>poissonverteilte</i>&nbsp; Zufallsgr&ouml;&szlig;e.
+
- The program generates a&nbsp; "Poisson distributed random variable".
+ Mit&nbsp; $I = 4$&nbsp; sind f&uuml;r&nbsp; $z2$&nbsp; die Werte&nbsp; $0, \ 1, \ 2, \ 3, \ 4$&nbsp; m&ouml;glich.
+
+ With&nbsp; $I = 4$&nbsp; the values&nbsp; $0, \ 1, \ 2, \ 3, \ 4$&nbsp; are possible for&nbsp; $z2$.
+ Das Einbinden der mathematischen Bibliothek "'''math.h'''" ist erforderlich, da in&nbsp; $z2$&nbsp; die Funktion "'''pow'''"  (Potenzieren) verwendet wird.
+
+ The inclusion of the mathematical library&nbsp; "'''math.h'''"&nbsp; is required because in&nbsp; $z2$&nbsp; the function&nbsp; "'''pow'''"&nbsp; ("exponentiate")&nbsp; is used  in&nbsp; $z2$.
  
  
{Welcher Wert steht  in &nbsp;$\text{p_array[2]}$&nbsp; beim Aufruf mit&nbsp; $I = 4$&nbsp; und&nbsp; $p = 0.25$?
+
{What value has &nbsp;$\text{p_array[2]}$&nbsp; when the function&nbsp; $z2$&nbsp; is called with the parameters &nbsp; $I = 4$&nbsp; and&nbsp; $p = 0.25$?
 
|type="{}"}
 
|type="{}"}
$\text{p_array[2]} \ = \ $ { 0.211 3% }
+
$\text{p_array[2]} \ = \ $ { 0.211 3% }
  
  
Line 54: Line 51:
 
</quiz>
 
</quiz>
  
===Musterlösung===
+
===Solution===
 
{{ML-Kopf}}
 
{{ML-Kopf}}
'''(1)'''&nbsp; Nach dem ersten Schleifendurchlauf&nbsp; $(m = 0)$&nbsp; ist die Variable&nbsp; $\text{summe = 0.2}$, beim n&auml;chsten&nbsp; $(m = 1)$&nbsp; gilt&nbsp; $\text{summe = 0.2 + 0.3 = 0.5}$.  
+
'''(1)'''&nbsp; After the first iteration of the loop&nbsp; $(m = 0)$&nbsp; the variable&nbsp; $\text{sum = 0.2}$,&nbsp; at the next iteration&nbsp; $(m = 1)$&nbsp; holds&nbsp; $\text{sum = 0.2 + 0.3 = 0.5}$.  
*In beiden F&auml;llen ist somit die Variable&nbsp; $\text{summe} < x = 0.75$.&nbsp;  
+
*In both cases:&nbsp; $\text{sum}$&nbsp; is less than&nbsp; $x = 0.75$.&nbsp;  
*Erst bei&nbsp; $m = 2$&nbsp; ist die R&uuml;cksprungbedingung erf&uuml;llt: &nbsp; $0.9 > x$.&nbsp; Somit ist&nbsp; $\underline{z1 = 2}$.
+
*Only at&nbsp; $m = 2$&nbsp; the return  condition is satisfied: &nbsp; $0.9 > x$.&nbsp; Thus&nbsp; $\underline{z1 = 2}$.
  
  
  
'''(2)'''&nbsp; Richtig sind die <u>Lösungsvorschläge 2 und 3</u>:
+
'''(2)'''&nbsp; Correct are the&nbsp; <u>solutions 2 and 3</u>:
*W&uuml;rde man auf die Hilfsvariable&nbsp; $x$&nbsp; verzichten und in Zeile 8 dafür&nbsp; $\text{summe > random()}$&nbsp; schreiben, so w&uuml;rde bei jedem Schleifendurchgang ein neuer Zufallswert erzeugt und&nbsp; $z1$&nbsp; h&auml;tte dann nicht die gew&uuml;nschten Eigenschaften.
+
*If one were to dispense with the auxiliary variable&nbsp; $x$&nbsp; and write in line 8 instead&nbsp; "$\text{sum > random()}$"&nbsp; a new random value would be generated on each iteration of the loop,&nbsp; and&nbsp; $z1$&nbsp; would then not have the desired properties.
*$z1$&nbsp; arbeitet gemäß dem Schaubild auf der Seite „Erzeugung mehrstufiger Zufallsgrößen“ im Theorieteil.&nbsp; Dort findet man eine deutlich schnellere Implementierung f&uuml;r den Fall gleicher Wahrscheinlichkeiten&nbsp; $(1/M)$.
+
*$z1$&nbsp; works according to the diagram on the page "Generation of multilevel random variables" in the theory section.&nbsp; There you find a faster implementation for the case of equal probabilities&nbsp; $(1/M)$.
*Im ersten Durchlauf&nbsp; $(m = 0)$&nbsp; ist in diesem Fall die R&uuml;cksprungbedingung aufgrund der Kleiner/Gleich&ndash;Abfrage nicht erf&uuml;llt;&nbsp; der Ausgabewert ist tatsächlich&nbsp; $z1 = 1$.
+
*In this case:&nbsp; In the first run&nbsp; $(m = 0)$&nbsp; the return condition is not satisfied due to the&nbsp; "Lesser/Equal"&nbsp; query;&nbsp; the output value is actually&nbsp; $z1 = 1$.
  
  
  
'''(3)'''&nbsp; Richtig sind die <u>Lösungsvorschläge 1, 3 und 4</u>:
+
'''(3)'''&nbsp; Correct are the&nbsp; <u>solutions 1,&nbsp; 3,&nbsp; and&nbsp; 4</u>:
*Es ergibt sich eine binomialverteilte Zufallsgr&ouml;&szlig;e, und zwar mit dem Wertevorrat&nbsp; $\{0, 1, 2, 3, 4\}$.  
+
*It results in a binomially distributed random variable,&nbsp; with&nbsp; $z2 \in \{0, 1, 2, 3, 4\}$.  
*F&uuml;r die Berechnung der Wahrscheinlichkeit&nbsp; ${\rm Pr}(z2 = 0) = (1 -p)^{I}$&nbsp; ben&ouml;tigt man hier die mathematische Bibliothek.  
+
*For the calculation of the probability&nbsp; ${\rm Pr}(z2 = 0) = (1 -p)^{I}$&nbsp; one needs the mathematical library.  
*Das Potenzieren k&ouml;nnte aber auch durch&nbsp; $I$&ndash;fache Multiplikation realisiert werden.  
+
*But exponentiation could also be realized by by multiplying &nbsp; $I$&nbsp; times.  
  
  
  
'''(4)'''&nbsp; Aufgrund der Zeile 6 beinhaltet das Feldelement&nbsp; $\text{p_array[0]}$&nbsp; vor der Programmschleife&nbsp; $(i = 0)$&nbsp; den Wert&nbsp; $(1 -p)^{I}$.&nbsp;  
+
'''(4)'''&nbsp; Because of line 6,&nbsp; before the program loop&nbsp; $(i = 0)$&nbsp;the field element&nbsp; $\text{p_array[0]}=(1 -p)^{I}$.&nbsp;  
*Im ersten Schleifendurchlauf&nbsp; $(i = 1)$&nbsp; wird folgender Wert eingetragen:
+
*In the first iteration&nbsp; $(i = 1)$&nbsp; the following value is entered:
:$$\text{p_array[1]}=\frac{ p\cdot I}{ 1- p}\cdot\text{p_array[0]}=   I\cdot p\cdot(1- p)^{ I- 1}={\rm Pr}(z2= 1) .$$
+
:$$\text{p_array[1]}=\frac{ p\cdot I}{ 1- p}\cdot\text{p_array[0]}= I\cdot p\cdot(1- p)^{ I- 1}={\rm Pr}(z2= 1) .$$
  
*Im zweiten Schleifendurchlauf&nbsp; $(i = 2)$&nbsp; wird die Wahrscheinlichkeit für das Ergebnis  "$z2=2$" berechnet:
+
*In the second iteration&nbsp; $(i = 2)$&nbsp; the probability for the result&nbsp; "$z2=2$"&nbsp; is calculated:
:$$\text{p_array[2]}=\frac{p\cdot (I- 1)}{ 2\cdot ( 1- p)}\cdot\text{p_array[1]}= \left({ I \atop { 2}}\right)\cdot p^{\rm 2}\cdot( 1- p)^{\rm 2}={\rm Pr}( z2 = 2) .$$
+
:$$\text{p_array[2]}=\frac{p\cdot (I- 1)}{ 2\cdot ( 1- p)}\cdot\text{p_array[1]}= \left({ I \atop { 2}}\right)\cdot p^{\rm 2}\cdot( 1- p)^{\rm 2}={\rm Pr}( z2 = 2) .$$
  
*Für&nbsp; $I= 4$&nbsp; und&nbsp; $p = 0.25$&nbsp; erhält man folgenden Zahlenwert &nbsp; &rArr; &nbsp; "$4$&nbsp; über &nbsp;$2$" &nbsp; $=6$:
+
*For&nbsp; $I= 4$&nbsp; and&nbsp; $p = 0.25$&nbsp; we get the following numerical value &nbsp; &rArr; &nbsp; "$4$&nbsp; over &nbsp;$2$" &nbsp; $=6$:
 
:$$\text{p_array[2]}={\rm Pr}( z 2=2)=6\cdot\frac{1}{16}\cdot\frac{9}{16}
 
:$$\text{p_array[2]}={\rm Pr}( z 2=2)=6\cdot\frac{1}{16}\cdot\frac{9}{16}
 
\hspace{0.15cm}\underline{=0.211}.$$
 
\hspace{0.15cm}\underline{=0.211}.$$
Line 90: Line 87:
  
  
[[Category:Theory of Stochastic Signals: Exercises|^2.5 Erzeugung diskreter Zufallsgrößen^]]
+
[[Category:Theory of Stochastic Signals: Exercises|^2.5 Generation of Discrete Random Variables^]]

Latest revision as of 15:33, 29 December 2021

C programs for generating
discrete random variables

The two C programs given here are suitable for generating discrete random variables:

  • The function  $z1$  generates an  $M$–level random variable with the value set  $\{0, 1$, ... , $M-1\}$.  The associated probabilities are passed in the array  $\text{p_array}$  with property  "Float"  The function  $\text{random()}$  returns equally distributed float–random variables  between  $0$  and  $1$.
  • A second function  $z2$  (source code see below)  returns a special probability distribution specified by the two parameters  $I$  and  $p$.  This is done using the function  $z1$.



Hints:



Questions

1

It is valid:  $M=4$,    $\text{p_array} = \big[0.2, \ 0.3, \ 0.4, \ 0.1 \big]$.
What result does the function  $z1$  return if the random function returns the  value  $x = 0.75$  ?

$z1 \ = \ $

2

Which of the following statements are true regarding  $z1$ ?

You could omit the assignment  $\text{x = random()}$  in line 5 and compare directly with  $\text{random()}$  in line 8.
If all probabilities passed to the function are equal,  there would be faster program realizations than  $z1$.
The return value  $\text{random() = 0.2}$  leads to the result  $z1= 1$.

3

Which of the following statements are true regarding  $z2$ ?

The program generates a  "binomially distributed random variable".
The program generates a  "Poisson distributed random variable".
With  $I = 4$  the values  $0, \ 1, \ 2, \ 3, \ 4$  are possible for  $z2$.
The inclusion of the mathematical library  "math.h"  is required because in  $z2$  the function  "pow"  ("exponentiate")  is used in  $z2$.

4

What value has  $\text{p_array[2]}$  when the function  $z2$  is called with the parameters   $I = 4$  and  $p = 0.25$?

$\text{p_array[2]} \ = \ $


Solution

(1)  After the first iteration of the loop  $(m = 0)$  the variable  $\text{sum = 0.2}$,  at the next iteration  $(m = 1)$  holds  $\text{sum = 0.2 + 0.3 = 0.5}$.

  • In both cases:  $\text{sum}$  is less than  $x = 0.75$. 
  • Only at  $m = 2$  the return condition is satisfied:   $0.9 > x$.  Thus  $\underline{z1 = 2}$.


(2)  Correct are the  solutions 2 and 3:

  • If one were to dispense with the auxiliary variable  $x$  and write in line 8 instead  "$\text{sum > random()}$"  a new random value would be generated on each iteration of the loop,  and  $z1$  would then not have the desired properties.
  • $z1$  works according to the diagram on the page "Generation of multilevel random variables" in the theory section.  There you find a faster implementation for the case of equal probabilities  $(1/M)$.
  • In this case:  In the first run  $(m = 0)$  the return condition is not satisfied due to the  "Lesser/Equal"  query;  the output value is actually  $z1 = 1$.


(3)  Correct are the  solutions 1,  3,  and  4:

  • It results in a binomially distributed random variable,  with  $z2 \in \{0, 1, 2, 3, 4\}$.
  • For the calculation of the probability  ${\rm Pr}(z2 = 0) = (1 -p)^{I}$  one needs the mathematical library.
  • But exponentiation could also be realized by by multiplying   $I$  times.


(4)  Because of line 6,  before the program loop  $(i = 0)$ the field element  $\text{p_array[0]}=(1 -p)^{I}$. 

  • In the first iteration  $(i = 1)$  the following value is entered:
$$\text{p_array[1]}=\frac{ p\cdot I}{ 1- p}\cdot\text{p_array[0]}= I\cdot p\cdot(1- p)^{ I- 1}={\rm Pr}(z2= 1) .$$
  • In the second iteration  $(i = 2)$  the probability for the result  "$z2=2$"  is calculated:
$$\text{p_array[2]}=\frac{p\cdot (I- 1)}{ 2\cdot ( 1- p)}\cdot\text{p_array[1]}= \left({ I \atop { 2}}\right)\cdot p^{\rm 2}\cdot( 1- p)^{\rm 2}={\rm Pr}( z2 = 2) .$$
  • For  $I= 4$  and  $p = 0.25$  we get the following numerical value   ⇒   "$4$  over  $2$"   $=6$:
$$\text{p_array[2]}={\rm Pr}( z 2=2)=6\cdot\frac{1}{16}\cdot\frac{9}{16} \hspace{0.15cm}\underline{=0.211}.$$