## Q7. A dice is not necessarily fair, in which case the probabilities for the 6 sides are different.
## We will look at a way to simulate unfair dice rolls in R.
## First simulate the rolling of some unfair dice and return the logical values
## if the sum is larger than a threshold value (this is an event).
## Then calculate the proportion of them with sum larger than the threshold using the logicals above.
## Your function should have 4 parameters: a threshold value, how many dice, events you have and
## possibilities for 6 unfair sides, and it should return a possibility.
### Your code here
## Q8.
## Draw independently from 1 dice with probability 2/7 for a six and 1/7 for others 30 times,
## computing the fraction of those trials whose sum is at least 4;
## Draw independently from 1 dice with probability 2/7 for a six and 1/7 for others 100 times,
## computing the fraction of those trials whose sum is at least 4;
## Draw independently from 2 dice with probability 3/14 for a five or a six, and 1/7 for others 100 times,
## computing the fraction of those trials whose sum is at least 7;
## What is your expectation for each trail? Are they different with the trials above? Why?
### Your code here
### Your answer here
#************************************************************************************
# Part 3: Calculation of pie (15 pts.)
#************************************************************************************
## Q1. First, create a dataframe called polygon_data
## that contains the points necessary to build a polygon.
## The first column includes "0" and 1000 values evenly distributed in [0,1], called x_val;
## The second column includes "0" and 1000 values that are sqrt(1-x^2), called y_val.
### Your code here
## Then, run the code below to create the shaded area for the quarter circle with ggplot2 package.
library(ggplot2) #Load ggplot2
plot_pi <- ggplot() +
geom_polygon(data=polygon_data,aes(x=x_val,y=y_val),alpha=0.1) + theme_bw()
par(mfrow=c(1,1))
plot_pi
## Q2. Now, let’s randomly put dots on the unit square (i.e. square with side length of 1).
## Create a dataframe called dot_data that contains 25 random points by declaring dot positions
## with 2 random uniformly distributed values for x and y.
## Hint: The dimension of dot_data is (25,2)
### Your code here
## Q3. Then define them “in†or “out†depending on whether they are within the circle area or not.
## add a column called in_or_out in dot_data to define if the point is in the polygon,
## indicate being "in" with 1 and "out" with 0.
## Hint: calculate in/out with the EU distance from origin.
## Print the head of dot_data.
### Your code here
## Run the code below to see the distribution of your points.
plot_pi + geom_point(data=dot_data,aes(x=x_val,y=y_val,color=in_or_out)) +
theme(legend.position="none")
## Q4. Simulate size of unit circle (pi) by dots.
## Hints: The ratio of the number of dots within the circle area to the total number of dots
## will give us the approximate ratio of the quarter unit circle to the unit square. Note that we are
## only plotting one quadrant here, assuming dots distribution are the same for all 4 quadrants.
### Your code here
## Q5. You may find your simulated value of pi is not very satisfactory.
## Give a solution to make your simulated value closer to the true value of pi.
## Show code and result.
### Your code here
Get Free Quote!
431 Experts Online