I am struggling with the Monte Carlo integral problem in R.
y= x^2+cos(x); for x= [0,2]
I am supposed to use HitMiss <- function(T,S,method="halton") to solve this problem.
T is the number of trails run per sample size in S.
S is number of sample points
The function should return T*|S| matrix, |S| is length of S.
Please help me out and give me some clue on solving this problem.
Really appreciate it!!!
A few hints:
(1) Have you seen this package? There are some Halton methods: https://cran.r-project.org/web/packages/randtoolbox/randtoolbox.pdf
(2) Do you want to solve it only with the Halton method? For this easy example an easy solution would be this one:
set.seed(1)
N = 10000
f = function(x) x^2 + cos(x)
points(runif(N, 0, 2), runif(N, 0, 4), pch = 20)
curve(f(x), 0,2, ylim=c(0, 4), col='white', lwd = 2, add=TRUE)
sum(f(runif(N, 0, 2)) > runif(N, 0, 4))/N * 2*4
#[1] 3.5592
Related
I have following simulated data of following 2 variables. I created the density plot as follows,
set.seed(1)
x1=density(rnorm(100,0.5,3))
x2=density(rnorm(100,1,3))
plot(x1)
lines(x2)
Is there any function that can use to find the common area for these 2 graphs using R ?
Do i need to perform an integration for intersecting points ?
Thank you
If you set the sequence both densities use for x values to be identical, you can use pmin on the y values. (Call str(x1) to see how they're stored.) For instance, to see how it works:
set.seed(1)
x1 <- density(rnorm(100,0.5,3), from = -10, to = 10, n = 501)
x2 <- density(rnorm(100,1,3), from = -10, to = 10, n = 501)
plot(x2, main = 'Density intersection')
lines(x1)
polygon(x1$x, pmin(x1$y, x2$y), 20, col = 'dodgerblue')
Taking the integral means just multiplying each pmin times the increment in the x sequence and summing the lot:
sum(pmin(x1$y, x2$y) * diff(x1$x[1:2]))
#> [1] 0.896468
I have asked another question, which was closed as Too Broad. Now, I will try to specify.
Again, I would like to simulate a 1-dimensional point process in R. So far, I've only been working on 2-dimensional simulations and would need a bit of help.
My goal is a simulation like in the picture
But, I only need the real line with the random points on it.
I use spatstat and have already found out that I can generate random points on a 1-dim Line with:
rpoisppOnLines(lambda, L, lmax = NULL, ..., nsim=1, drop=TRUE)
Now, I would like to produce the real line, preferably with matching labeling.
Does anyone have an idea?
Here is some crude code on getting samples from a point process.
library(spatstat)
lambda = 5
L = psp(0, 0, 3, 0, owin(c(0, 3), c(-1, 1)))
pp = rpoisppOnLines(lambda, L, lmax = NULL, nsim=1, drop=TRUE)
plot(pp$x, pp$y, pch = 4, lwd = 2, cex = 2)
abline(0, 0)
You could make your plot fancy with ggplot2
You could use a simple linear network to represent the one dimensional line
segment you want to simulate on. This also makes it possible to fit models
(lppm), estimate the intensity non-parametrically (density.lpp), estimate
the K-function (linearK), and a bunch of other things:
library(spatstat)
x_start <- 0
x_end <- 3
endpoints <- ppp(x=c(x_start, x_end), y=c(0,0), window = owin(c(x_start, x_end), c(-.1,.1)))
L <- linnet(endpoints, edges = matrix(c(1,2),ncol = 2))
X <- rpoislpp(lambda = 5, L = L)
However, this tool is designed for points on a complicated network and not
just the real line, so the plotting method is not really adapted to this
setting, and might not produce exactly what you want (too much white space):
plot(X, pch = 4, lwd = 2, main = "")
axis(1)
You can extract the coordinates of the point pattern using coords and then
use the plotting method from the other answer from there:
co <- coords(X)
co$x
#> [1] 1.3306861 2.5550691 1.7776248 2.9486675 1.8571362 2.5020587 1.4843001
#> [8] 0.4371669 0.8478670
Created on 2018-12-18 by the reprex package (v0.2.1)
I'm trying to find shape1 and shape2 in dbeta() such that the answer from dbeta() for the two input values: .6 and .8 become 3.
I'm using the below optim() but don't get exact result, that is I expect getting values for shape1 and shape2 that when used with .6 and .8 give 3 and 3, but they don't, why?
f <- function(x) {
y <- c(3, 3) - dbeta(c(.6, .8), shape1 = x[1], shape2 = x[2])
}
AA = optim(c(1, 1), function(x) sum(f(x)^2), control = list(reltol = (.Machine$double.eps)))
parms = unname(AA$par)
dbeta(c(.6, .8), parms[1], parms[2]) # Here I expect to get `3` for `.6` and `.8` but I don't.
I had a brief look at this. I don't think there's any problem with the fit: here's a picture of the likelihood surface:
library(emdbook)
cc <- curve3d(g(c(x,y)),xlim=c(1,20),ylim=c(1,20),
sys3d="none")
pp <- which(cc$z==min(cc$z),arr.ind=TRUE)
png("betasurf.png")
with(cc,image(x,y,z))
points(parms[1],parms[2],pch=16)
points(cc$x[pp[1]],cc$y[pp[2]],pch=1)
dev.off()
Filled circle is the fitted value, open circle is the minimum of the grid; I think the difference is just numerical fuzz (I zoomed in a few times to make sure). In any case, there's no evidence of anything weird like multiple optima.
I believe the issue is just that you've set up a set of pair of points that can't be simultaneously matched by any Beta distribution; optim() is giving you the best possible fit ...
png("betatmp.png")
curve(dbeta(x,parms[1],parms[2]),from=0,to=1)
points(c(0.6,0.8),c(3,3),pch=16)
dev.off()
I need to get the x- and y-coordinates of points along a Bezier curve in R. I thought this would work:
x <- c(0, 0, 1, 1)
y <- c(0, 1, 1, 0)
bg <- bezierGrob(x, y)
trace <- bezierPoints(bg)
But after running that trace$x and trace$y are a bunch of measurements in inches well outside the range of (0,1). The man page for bezierPoints says:
Rather than drawing an Xspline (or Bezier curve), this function returns the points that would be used to draw the series of line segments for the Xspline.
Am I running into some grid weirdness? Or am I trying to use the wrong solution to this problem?
Looks like the bezier package, not grid, is the way to go. This works:
t <- seq(0, 1, length=100)
p <- matrix(c(0,0, 0,1, 1,1, 1,0), nrow=4, ncol=2, byrow=TRUE)
bp <- bezier(t=t, p=p)
I'm simulating some data in R, plotting x against Y (a rate) and I want x to be increasing linearly up to a point and then level off. That is, Y is a function of x between say, 0.1 and 5, but constant from 5.01 to 10. Is there a simple command which allows for varying x's? I'm sure my lecturer told me about one but I can't remember it...
Any help or thoughts would be greatly appreciated!
You could use ifelse:
> f <- function(x) ifelse(x < 5, x**2, 25)
> x <- seq(1, 10, .1)
> plot(x, f(x), type='l')