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

We should all be familiar with the geometric shape known as a sphere. If centere

ID: 3871977 • Letter: W

Question

We should all be familiar with the geometric shape known as a sphere. If centered at the origin (0,0,0) then it can be described by the equation x^2 + y^2 + z^2 = r^2 , where x, y, z correspond to the three spatial coordinates and r > 0 is the radius. Likely, you have also encountered a formula V = 4/3r^3 for calculating the volume of a sphere. However, it is very common that one must calculate the volume of a sphere in dimensions much higher than three (it is then called a hypersphere). Thankfully, due to the simplicity of the hypersphere shape there exists an analytic solution to this problem Vd = Sdr d d where Sd = 2 d/2 (d/2) where d is the dimension and is the Gamma function (it is suggested, but not necessary to understand what the Gamma function is for this question, only that to calculate its value you will use the gamma function in R). Although the hypersphere volume is directly calculable, it is very common in the real world (e.g., for optimization or machine learning tasks) to encounter very high dimensional shapes that are very irregular and thus do not have a nice formula for their volume. In these cases a Monte Carlo sampling approach is often the most efficient way for estimating the volume. However, for simplicity this question will nonetheless focus on estimating the hypersphere volume of radius r = 2 under the assumption that we are unaware of the analytic solution. Additionally, you should be organizing your code into functions and leveraging existing sampling procedures such as replicate versus a very long script. The later technique will require much more of your time and is also likely to be more confusing and prone to both syntactic and semantic bugs.

(a) Create an R script that uses the hit-or-miss method discussed in lecture to estimate the volume of a hypersphere centered at the origin. Be sure to choose an appropriate formulation and bounding region. HINT: at minimum create a general purpose function for the hit-and-miss procedure, and another for determining whether a point exists within the hypersphere. Use the later as one of the parameters to the first (i.e., pass the function as a parameter to the hit-or-miss method). The hit-or-miss function should perform one execution of the method, not many trials.

(b) Use your script to estimate the hypersphere volume for dimensions D = {10, 50, 100, 500}. To test the sensitivity of results with respect to the number of samples you will repeat hit-or-miss with S = {1000, 10000, 50000, 100000} samples. Moreover, since sampling has an inherently random component you will need to re-execute each of the 4 × 4 = 16 experiments multiple times: you will replicate each experiment 30 times (so in total 16 experiments × 30 replications equals 480 executions of your hit-and-miss method). From the above experiments you will have generated for each D × S combination a list of 30 hypervolume estimates and can construct two data frames or matrices from these, each having |D| = 4 rows and |S| = 4 columns. The first data frame M will represent the mean volume and the second E will be the standard deviation. Then, M[1, 1] will correspond to the average hypervolume estimate for dimension D[1] = 10 using S[1] = 100 samples, and E[1, 1] will be the corresponding standard deviation. Append a fifth column to M that represents the exact volume for each of the four hypersphere dimensions as calculated by the aforementioned Vd formula. No column should be appended to E since there is no variance in the value of Vd for a specific d value.

(c) For each of the four dimensions in D create a line plot as follows (so, you will submit four plots). An example (with fictitious data) is shown in Figure 1.

• let the x-axis be the number of samples S and the y-axis be volume.

• plot a horizontal line in blue and line width 3 to represent the true volume.

• add to the plot four points, one for each estimated volume as stored in M, connect the points with a line with width 2 and color in red

• add to the plot the corresponding standard deviation bars. You can use the following function:

# Adds bars to plot (i.e., after running a plot())

# x,y: coordinates of plot to extend bars from

# upper,lower: size of bar

# length: width of the tips of the bar plot.bar <- function(x, y, upper, lower=upper, length=0.05,...){ if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper)) stop("vectors are not the same length") arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, lty=2, col="gray",...) }

Explanation / Answer

ANSWER::

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