Question

In: Math

Write an R function max.streak(p) that gives the length of the maximum "streak" of either all...

Write an R function max.streak(p) that gives the length of the maximum "streak" of either all heads or all tails in 100 flips of a (possibly biased) coin whose probabilty of showing heads is pp.

Use your function to determine the expected length (rounded to the nearest integer) of the maximum streak seen in 100 flips of a coin when the probability of seeing "heads" is 0.700.70.

As a check of your work, note that the expected length of the maximum streak seen in 100 flips of a fair coin should be very close to 7.

Solutions

Expert Solution

R function with appropriate comments -

max.streak <- function(p) {
x = sample(c(0,1), 100, replace = TRUE, prob = c(1-p, p)) #Generate 100 samples of 100 flips
count.head = 0 #Initialize Head Count to 0
count.tail = 0 #Initialize Tail Count to 0
max.head.streak = 0 #Initialize Max streak of Head to 0
max.tail.streak = 0 #Initialize Max streak of Tail to 0
for(i in 1:100) {
if(x[i] == 0) { # IF Tail occurs
if(max.head.streak < count.head) { #If max streak of heads is less than current streak
max.head.streak = count.head #replace the max streak of heads by the currect streak
}
count.head = 0 #Initialize Head Count to 0 as tail occurs
count.tail = count.tail + 1 #Increment the count of tail
}
if(x[i] == 1) { # IF Head occurs
count.head = count.head + 1 #Increment the count of head
if(max.tail.streak < count.tail) { #If max streak of tails is less than current streak
max.tail.streak = count.tail #replace the max streak of heads by the currect streak
}
count.tail = 0 #Initialize Tail Count to 0 as tail occurs
}
}
return(max(max.head.streak, max.tail.streak))
}
max.streak(0.7)

On running the code, I got the output of maximum streak as 11. The output will change for different runs.


Related Solutions

Write a function name randintlist which takes three parameters: 1.minimum 2.maximum 3.length The randintlist function should...
Write a function name randintlist which takes three parameters: 1.minimum 2.maximum 3.length The randintlist function should return a list of random int values (using random.randint) where the minimum and maximum values of those random numbers are the argument values for the first two parameters, and the length of the list(count of random integers ) is given by the argument value of the third parameter For the list of random int values returned by randintlist no two values should be equal...
Find the absolute maximum and​ minimum, if either​ exists, for the function on the indicated interval....
Find the absolute maximum and​ minimum, if either​ exists, for the function on the indicated interval. f(x)=x^4+4x^3-7 (A) [-2,2] (B) [-4,0] (C) [-2,1]
Find the absolute maximum and minimum, if either exists, for the function on the indicated interval....
Find the absolute maximum and minimum, if either exists, for the function on the indicated interval. f(x)=x^3-6x^2+9x+5 (A) [-3,6] (B)[-3,3] (C)[2,6] (A) Find the absolute maximum. Select the correct choice below​ and, if​ necessary, fill in the answer boxes to complete your choice.
PLEASE WRITE IN R CODE. Has to output on R software. (1) The stem length of...
PLEASE WRITE IN R CODE. Has to output on R software. (1) The stem length of soybeans from an experiment are: 20.2, 22.9, 23.3, 20.0, 19.4, 22.0, 22.1, 22.0, 21.9, 21.5, 20.9 a. Create a histogram to visualize the data b. Test "t.test" whether the population mean is different from 22 c. Obtain a 2 sided 98% confidence interval on the true mean using "t.test". d. The researcher, by using "t.test" on a sample size of 11 was assuming that...
Your task is to write a paragraph (maximum length of one page double spaced) that analyzes...
Your task is to write a paragraph (maximum length of one page double spaced) that analyzes one of the following environmental ethical issue. You choose which issue to write about; in this assignment, reflect deeply on a topic that matters to you. You must argue your position with ethical theory and also show how your position is stronger than the opposing position supported by another theory. Option A: Should animals be used in medical research? In your response, consider which...
Your task is to write a paragraph (maximum length of one page double spaced) that analyzes...
Your task is to write a paragraph (maximum length of one page double spaced) that analyzes one specific bioethical issue. You choose which bioethical issue to write about; in this Assignment, reflect deeply on a topic that matters to you. If you need help selecting a topic, consult with your instructor. Complete your position paragraph. You should write about a bioethical issue that strikes you as interesting, important, and relevant. For example, if, when reading the assigned chapter in the...
Your task is to write a paragraph (maximum length of one page double spaced) that analyzes...
Your task is to write a paragraph (maximum length of one page double spaced) that analyzes one specific bioethical issue. You choose which bioethical issue to write about; in this assignment, reflect deeply on a topic that matters to you. If you need help selecting a topic, consult with your instructor. You should write about a bioethical issue that strikes you as interesting, important, and relevant. For example, if, when reading the assigned chapter in the text, you recalled a...
1. Consider the function f: R→R, where R represents the set of all real numbers and...
1. Consider the function f: R→R, where R represents the set of all real numbers and for every x ϵ R, f(x) = x3. Which of the following statements is true? a. f is onto but not one-to-one. b. f is one-to-one but not onto. c. f is neither one-to-one nor onto. d. f is one-to-one and onto. 2. Consider the function g: Z→ {0, 1, 2, 3, 4, 5}, where Z represents the set of all integers and for...
Hello, i have this excersice in R studio Write a function in R that generates simulations...
Hello, i have this excersice in R studio Write a function in R that generates simulations of a Poisson random variable as follows: define I = [λ], and use p_i + 1 = λp_i / (i + 1) to determine F recursively. Generate a random number U, determine if X≤I by comparing if U≤F (I). If X≤I searches downwards starting at I, otherwise it searches upwards starting from I + 1. Compare the time it takes for the two algorithms...
Environmental Ethics Position Paragraph Your task is to write a paragraph (maximum length of one page...
Environmental Ethics Position Paragraph Your task is to write a paragraph (maximum length of one page double spaced) that analyzes one of the following environmental ethical issue. You choose which issue to write about; in this assignment, reflect deeply on a topic that matters to you. You must argue your position with ethical theory and also show how your position is stronger than the opposing position supported by another theory. Option A: Should animals be used in medical research? In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT