Advertisement
Guest User

Untitled

a guest
May 20th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1.  
  2.  
  3. library("ggplot2")
  4.  
  5. eq = function(x){x^(-x)}
  6. area = integrate(eq, 0, 4)
  7. # print(area[1])
  8. c = 1/as.numeric(area3[1])
  9. print(c)
  10.  
  11. density = function(x){c * (x^(-x))}
  12. curve(density, from=-1, to=5, xlab="x", ylab="y",
  13. xlim = c(-1, 6),
  14. ylim = c(0, 3),
  15. col = "coral",
  16. col.lab="darkslategray",
  17. col.main = "darkslategray",
  18. col.axis = "darkslategray",
  19. bty = "n")
  20.  
  21.  
  22.  
  23.  
  24.  
  25. #f = function(t){integrate(density, lower = -Inf, upper = Inf, t)}
  26. F <- function(t){
  27. if(t < 0){
  28. return(0)
  29. }
  30. else if(t >=0 && t < 4){
  31. return(as.numeric(integrate(density, lower = 0, upper = t)[1]))
  32. }
  33. else {
  34. return(1)
  35. }
  36. }
  37.  
  38.  
  39. ggplot(data.frame(x=c(-1, 5)), aes(x=x)) +
  40. stat_function(fun=Vectorize(F), geom="line", color = "coral") +
  41. xlab("x") +
  42. ylab("y")
  43.  
  44. # Finv <- function(s){function(x){uniroot(F(x) - s, lower = 0, upper = 1)}}
  45. # Finv <- function (s) uniroot((function (x) F(x) - s), lower = 0, upper = 1)[1]
  46. # inverse <- function (f, lower = 0, upper = 1) {
  47. # function (s) uniroot((function (x) f(x) - s), lower = lower, upper = upper,extendInt = "yes")[1]
  48. # }
  49. Finv <- Vectorize(function (s)uniroot((function (x) F(x) - s), lower = 0, upper = 4)[1])
  50. curve(Finv, 0,1)
  51.  
  52.  
  53. # Finv <- inverse(F, 0,1)
  54. # Finv <- function(s){
  55. # if(s<0){
  56. # return(0)
  57. # }
  58. # else if(s >=0 && s < 4){
  59. # # return(uniroot(F, c(0,1)))
  60. # # return(uniroot(F,c(0,100),extendInt = "yes")$f.root)
  61. # }
  62. # else{
  63. # return(1)
  64. # }}
  65.  
  66.  
  67. # print(Finv(0.9))
  68. # ggplot(data.frame(x=c(0, 1)), aes(x=x)) +
  69. # stat_function(fun=Finv, geom="line", color = "cornflowerblue") +
  70. # xlab("x") +
  71. # ylab("y")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement