Consider the sine model of the von Mises distribution described in Section 2.2 of a recent paper. (You will need to read all of Section 2 to understand the notation.) Write a function named 'dvonmises' with signature 'function(x,mu,vu,kappa1,kappa2,lambda,log=FALSE,use.normalizing.constant=TRUE)' which evaluates the density (or, if 'log=TRUE', the natural logarithm of the density) of the sine model of the bivariate von Mises distribution for the supplied parameters at the angle pair given by x. In the case of 'use.normalizing.constant=FALSE', the result is given without the normalizing constant. Notice that the normalizing constant involves infinite sums, something that the computer cannot do. The computer can, however, approximate the infinite sum with a finite sum. Your implementation should seek to find a solution that balances numerical accuracy, efficiency, and speed. Put your function in a script named 'dvonmises.R'. Your script should have no other code.
Your task is to implement the function for rejection sampling in the script rejection-sampler.R. Do not change anything in this file except the body of the function. Your function should be completely generic (e.g., do not assume that it will be used to sample from any particular distribution, such as a beta or von Mises distribution). Please sure to test your function (but do not submit your testing code.)
Using your code from the previous parts, write a function named 'rvonmises' with signature 'function(n,mu,vu,kappa1,kappa2,lambda)' which generates 'n' independent and identically distributed realizations of the sine model von Mises distribution for the supplied parameters. The result of the function should be an 'n' by 2 matrix. Insights into the implementation are given in the first paragraph on page 11 of the paper, which references Mardia, et. al. (2007) and that paper's appendix. See, in particular, Theorem 2 of the appendix. (You should have access to the paper when within the University network and the appendix from anywhere.) Put your function in a script named 'rvonmises.R'. Again, your script should have not code other than the function definition.
Write a function named 'show.vonmises' with signature 'function(n,mu,vu,kappa1,kappa2,lambda,show.contours=TRUE)' which uses the previous parts to plot contours of the specified sine model density as well as 'n' realizations from that distribution. Note that plotting the contours can be disabled if 'show.contours=FALSE' and 'n' can be zero. Explore many different combinations of parameter settings to convince yourself that your code is correct.
The purpose of this part is to give you experiencing packaging your statistical software for use by others. Make an R packages named "vonmises" which provides and completely documents the 'rvonmises' and 'dvonmises' functions from past projects. Your package should only "export" those functions (i.e., not pollute the namespace with any other functions or variables). Ensure that your package builds without any error or warnings. Submit the release ".tar.gz" file resulting from the "R CMD build" command.
Commit the requested scripts in the appropriate directory in the repository.