3-function factory.txt - HW3 Function factory In this exercise we will write a function computing the k^{th order statistics and a function computing

3-function factory.txt - HW3 Function factory In this...

This preview shows page 1 - 2 out of 2 pages.

#' HW3: Function factory #' #' In this exercise, we will write a function computing the k^{th} order statistics and a function computing the density of a mixture model. #' Order Statistics Reference: . #' #' 1. Implement a function `kth_order(vec, k)` that takes as input a vector and returns its k^{th} order statistics. If `k` is not an index of the vector, the function should return `NA`. #' Use the `sort()` and `seq_along()` function to create this function. ## Do not modify this line! kth_order<-function(vec,k){ vec1<-sort(vec) seq_along(vec) return(vec1[k]) } #' 2. Implement a function `order_factory(k)` that takes as input an index `k` and returns a function that compute the k^{th} order statistics of a vector `vec` using `kth_order`. Don't forget to use `force()`. ## Do not modify this line! order_factory<-function(k){ force(k) function(x){ kth_order(x,k) } } #' 3. Create a function `my_mixture(x, c1, mu1, sigma1, c1, mu1, sigma1)` that evaluates the probability density function `p(x) = c1 x N(x; mu1, sigma1) + c2 x
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

Unformatted text preview: N(x; mu2, sigma2)` at `x`. The sigma here is the standard deviation. #' You can assume that 0 &lt;= c1 &lt;= 1, 0 &lt;= c2 &lt;= 1, c1 + c2 = 1, sigma1 &gt; 0, sigma2 &gt; 0. ## Do not modify this line! my_mixture&lt;-function(x, c1, mu1, sigma1, c2, mu2, sigma2){ return(c1*dnorm(x,mean=mu1,sd=sigma1)+c2*dnorm(x,mean=mu2,sd=sigma2)) } #' 4. Implement a function `mixture_factory(c1, mu1, sigma1, c2, mu2, sigma2)`. This function makes a function that evaluates the probability density function `p(x) = c1 x N(x; mu1, sigma1) + c2 x N(x; mu2, sigma2)`. The sigma here is the standard deviation. #' You can assume that 0 &lt;= c1 &lt;= 1, 0 &lt;= c2 &lt;= 1, c1 + c2 = 1, sigma1 &gt; 0, sigma2 &gt; 0. Use the functions `force` and `dnorm`. ## Do not modify this line! mixture_factory&lt;-function(c1, mu1, sigma1, c2, mu2, sigma2){ force(c1) force(mu1) force(sigma1) force(c2) force(mu2) force(sigma2) function(x){ return(c1*dnorm(x,mean=mu1,sd=sigma1)+c2*dnorm(x,mean=mu2,sd=sigma2)) } }...
View Full Document

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors