Java代写:CS350 Simulation

用Java代写几个模拟器,模拟概率问题。

Requirement

Feel free to make assumptions, if you feel that such assumptions are justified or necessary. Please state your assumptions clearly. Unreasonable assumptions lead to unreasonable grades!

Simulation of coin tosses.

Write a Java method with the following function signature:

1
public static int singleSimulationOfGetNumberOfHeads(int tosses, double probability).

The above method takes in as input the number of tosses in a Bernoulli experiment and the probability of observing a heads on a single toss. The method returns back the number of heads observed when a single Bernoulli trial of 8 tossess is conducted. Note the number of heads returned back is not an expected value but the actual value (0 ≤ numberOf Heads ≤ 8) that is observed in that specific Bernoulli trial. Because this answer will vary (due to different number that gets generated via the uniform random number generator), you are asked call your method singleSimulationOfGetNumberOfHeads, 200 times for the same probability and record the results. We are providing you psuedocode of how your main function will look like that calls singleSimulationOfGetNumberOfHeads 200 times.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Psuedocode of your main function
p_heads = 0 // this is the probability of observing heads
numberOfTosses = 8 // number of experiments in the Bernoulli trial
while (p_heads < 1.01)
{
loop (run this loop 200 times)
{
// record the value
print p_heads, singleSimulationOfGetNumberOfHeads(numberOfTosses,p_heads)
}
// increment the p\_heads to a different value and then rerun the simulation 200 times
p_heads = p_heads + .05
}

Generate a graph (using Excel or any of your favourite graph plotting software) of the probability of observing heads on the X axis against the number of heads observed. Note: That for each probability of heads observed on the X axis, you will record 200 values on the Y axis (because you called your getNumberOfHeads method 200 times).

If you observe 7 heads show up out of 8 tosses. What would you expect on an average for the probability of the coin showing heads? No math is required for this question. I just expect you to see the graph generated earlier and answer your question based on the graph.

Explain by observing the graph you generated, what number of heads you roughly expect to see if the coin is a fair coin. No math is required for this question. I just expect you to see the graph generated earlier and answer your question based on the graph.

Question 1

The availability of a web server is rated at 98%. Answer the following questions

  • (a) What is the probability that in a sequence of 20 connections to the web server, no connections will be refused.
  • (b) What is the probability that in a sequence of 20 connections to the web server, exactly one connection will be refused.
  • (c) What is the probability that in a sequence of 20 connections to the web server, at most three connections will be refused.
  • (d) What is the probability that the web server will be able to successfully respond to 4 (or more) consecutive requests for connections.
  • (e) What is the average number of requests that will be successfully served before a connection is refused?
  • (f) What is the average number of requests that will be successfully served out of a set of 50 requests for connections?
  • (g) What is the probability that out of a very large number of requests at least 98.5% of the requests will be processed successfully?
  • (h) Which of the above questions refers to the reliability of the web server?

Question 2

The arrival of packets at an Ethernet adapter of a web server is described by a Poisson process with a rate of 100 packets per second. Answer the following questions

  • (a) Write down the formula for the probability distribution of the number of packets arriving to the adapter in 10 milliseconds.
  • (b) What is the probability that more than 1 packet will be received within a 10-millisecond period of time?
  • (c) Write down the formula for the probability distribution of the inter-arrival time of packets.
  • (d) What is the probability that two packets will be separated by more than 15 milliseconds?
  • (e) What is the standard deviation of the inter-arrival time of packets?

Question 3

Packets that arrive to the Ethernet adapter described in the above problem (3) are queued up in a buffer until processed by the Interrupt Service Routine (ISR). Assuming that the ISR service time per packet is exponential with an average of 9.6 milliseconds. Answer the following questions:

  • (a) What is the capacity of the Ethernet adapter?
  • (b) Write down the probability distribution of the number of packets at the Ethernet adapter (either queued or being transmitted).
  • (c) How many packets do you expect to find waiting in the buffer of the adapter at any point in time (on average)?
  • (d) What is the average waiting time in the buffer (i.e., how long is a packet buffered untilthe ISR starts processing it)?
  • (e) What is the slowdown caused by queuing at the Ethernet adapter?

Question 4

Write a Java function that returns a random value that is distributed according to an exponential distribution with a mean of T = 1 . Hint: Given that all you have through Java is a uniform random number generator, you will need to find the relationship that maps a uniform random variable that ranges from 0 to 1 to an exponential random variable with mean T = 1 . As explained in the lecture notes, pages 9 10, one way of establishing this relationship is to equate the cumulative distribution functions for a uniformly distributed random variable (say Y) and an exponentially distributed random variable (say X).
Answer the following questions:

  • (a) Write a program that calls the function you wrote to generate 100 exponentially-distributed random values using = 4. Generate and plot a CDF of the exponential distribution using the 100 generated values (for plotting the CDF, you may want to use software such as excel). Compare the CDF of the random variable that you have obtained empirically with the analytical CDF of the random variable. Do they match? Explain why or why not.
  • (b) Let the 100 random values you obtained above represent the inter-arrival times (in seconds) of queries submitted to a server. Plot a normalized histogram showing the relative frequency (on the Y axis) of the various number of queries observed over 0.1-second intervals (on the X axis). Write down the formula of the distribution that best characterizes the empirical results you got in this histogram.

Question 5

Write a function in Java - say Zrand() - that returns a random value that is distributed according to a standard normal distribution. Hint: Given that all you have through common Java libraries is a uniform random number generator, you will need to find the relationship that maps a uniform random variable that ranges from 0 to 1 to a standard normal random variable. One way of doing this is to use the Central Limit Theorem (CLT) to generate this random value using the uniform random number generator. Recall that according to CLT, the sum of N random variables (that follow any distribution with finite moments) approaches a Normal distribution, whose mean is N times the mean of the individual random variables, and whose variance is N times the variance of the individual random variables. [Note: You may assume that aggregating 30 or more samples is “enough” for the CLT to hold.]

  • (a) Write a program that calls the function you wrote to generate 100 standard-normal random values. Generate and plot a CDF of the standard normal distribution using the 100 generated values (for plotting the CDF, you may want to use software such as excel). Compare the CDF of the random variable that you have obtained empirically with the CDF given in the standard normal distribution table by looking up the probability that the random variable is less than or equal to 0, 1, 2, 3, and 4. Do they match? Explain why or why not.
  • (b) Write a program that calls the function you wrote in part (a) to generate a normally- distributed random variable with a mean = 72 and standard deviation = 16. Use your program to compute the probability that this random variable is between 66 and 80.

Compare this answer with the answer you get analytically. Show your work.