Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the homework we said the surprise of a random occurrence with probability p(x

ID: 3888426 • Letter: I

Question

In the homework we said the surprise of a random occurrence with probability p(x) was defined as s(x)= -log2(p(x)), where p(x) is the probability of that event happening. A random variable is a collection of all possible outcomes for an event. For instance, for a coin flip the random variable can have value heads or tails, for weather it might be rainy or sunny, for a roulette wheel spin the collection of all possible slots the ball might land in (0, 00, and 1-36). The uncertainty or entropy of a random variable is the average surprise over all possible outcomes. For a random variable with only two outcomes the entropy is:


H(X) =p(x1)*s(x1)+p(x2)*s(x2) =p(x1)*(-log2(p(x1))+p(x2)*(-log2(p(x2))


where x1 and x2 are the two outcomes of the random variable x. For the coin flip x1 is heads and x2 is tails. Note that p(x1)+p(x2)=1 , meaning that one of the two thing must happen (heads or tails). You already know how to calculate surprise. Now write both pseudo code and a C++ program that calculates the entropy, H(X), for a random variable with two outcomes. The program should ask the probability of one event, and calculate the probability of the second event. Remember the sum of the probabilities is one. It then calculates the entropy , H(X) as defined in the formula above.

Explanation / Answer

#include<iostream.h>

#include<cmath.h>

int main()

{

//declaration;

double px1,px2,entropy,sx1,sx2;

cout<<"Enter the probablity of one event";

cin>>px1;

px2=1-px1;

//calculating x1 and sx2 by alreadyy defined log2 funcion in math.h

sx1=-log2(px1);

sx2=-log2(px2);

entropy=(px1*sx1)+(px2*sx2);

//printing entropy

cout<<"The entropy is : "<<entropy;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote