Question: a.Try to create random sequence of coin tosses. Create vector of ones (representing heads) and zeros (tails), of length 41. Be as random as you

a.Try to create "random" sequence of coin tosses. Create vector of ones (representing heads) and zeros (tails), of length 41. Be as random as you can. Do before you read the rest of the question. (Or,getsomeoneelsetodoit.)Printyoursequence.

My fake sequence:00011001010111110101001001000011010110101

b.Can we devise a test to distinguish between fake tosses and true tosses of a coin? One method is based on the observation that people tend to fluctuate between heads and tails more frequently than random. Write code to count the number of times nfthe sequence fluctuates between heads andtails

(that is, the number of times "head follows tails" or "tails follows head"). For example, for the sequence THHTT, the number of fluctuations is 2.

The sequence of flips fluctuates nf = 25 times.

c.How many times would we expect the sequence of tosses to fluctuate if it were truly random? Write looptoexecutethefollowingsteps2105times:

createasetof41tosses,

calculatethenumberoffluctuationsfortheset,usingthemethodfromthepreviousquestion,

storethenumberoffluctuationsinavector.

When you havecompleted2 105trials,youwillhaveavectoroflength2 105. Plot the histogram offluctuations

d.

You will note that the distribution peaks at 20. In a string of 41 tosses, there are 40 times when the sequence could fluctuate, and a random sequence will fluctuate 50% of the time, on average. Given yourvalueofnf(frompartb,thenumberoffluctuationsinyoursequence),itisD=nf 20 fluctuations awayfrom the average.Forexample, for a sequence with nf= 25, it is D = 25 20 = 5 fluctuations away fromaverage.

Inyoursetof2105trials,whatfractionareDormorefluctuationsawayfromaverage?In otherwords,addtogetherthefractionthathave nf20+Dandthefractionthathave

nf 20 D. Print your answer as a percentage.

15.5155 of trials have fluctuations that are >= 5 away from average.

e.We can use this knowledge to create rigged coin toss game. The player will guess the outcome of the next coin toss, and will accordingly gain or lose money. The coin tosses won't be random: we'll use previous guesses to try to outwit the player. Include here a script that performs the following (set eval = F so that it prints out but does notrun).

Initialise money = 10, to give the player $10 startingmoney.

Loop over attempts to guess the cointoss.

Foreachattempt,readacharacterfromkeyboardinput.

Ifhort,recordthisguessasyour_current_guess,whichwillequaloneorzero. Keeparecordof upto41guessesinavectorcalledguesses,withthemostrecentguessalwaysatguesses[1].For any other keyboard entry, break from theloop.

Keeptrackofthenumberofrecordedguessesnguesses.

Given the list guesses, calculate the probability of a fluctuation (pf) the number offluctuations in guesses divided by the number of recorded guesses inguesses.

If the number of recorded guesses is small (less than 5, say), set pf =0.7.

Usetheprobabilitypftopredictthenextguessasfollows:

If thistoss is equal to your_current_guess, add $1. If thistossis not equal to

your_current_guess, take away $1.

Ifzeromoneyremains,endthegame.Outputthenumberofguessestheplayermade.

Be sure to give appropriate feedback to the player after each guess: their guess, the coin toss, You win!or

You lose!, and how much money they have. Play your game a few times to test it.

a.Setavariableequaltonumber(asaninteger).Runthefollowingcodeto seed the random numbergenerator.

set.seed(no)

b.Createarandomvectorwith32elementsusingthefunctionrunif().Printyourvectorasbelow(its entrieswillbedifferenttotheonebelow).

My vector:

[1]0.4440.1410.3630.9330.4540.3270.8020.5320.6330.2000.480

[12]0.5210.3540.9840.3470.8480.8690.4560.5920.2790.8810.997

[23] 0.609 0.872 0.532 0.541 0.501 0.400 0.950 0.541 0.927 0.631

c.Print the following elements of your vector. Be sure to format the result in the same way as shown below, including the labels Minimum value,etc.

Minimumvalue:0.1405872. Maximum value:0.9969375

Averagevalue:0.5918109. Median value:0.5363848

d.Choosearandomnumber(calledn)between5and9usingsample().Printthenthsmallestandnth

largest value in the vector.

The8thsmallestvalue:0.4002958 The8thlargestvalue:0.8691926

e.Reshapeyourvectorintoa4by8matrix.Calculatethefollowingcharacteristicsofyourmatrix.

Column6hasthelargestsumofallthecolumns. Row 2 has the smallest sum of all the rows.

f.Create new matrix, which is the same as the previous matrix, except that any element m[i,j]that isgreaterthan0.3isreplacedwithm[i,j]-1.Printthesumofthematrix.

Thesumofthenewmatrixis -10.06205

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mathematics Questions!