Skip to content

Generate data - function

4 messages · Petr Savicky, Val

#
On Tue, Jan 31, 2012 at 01:59:13PM -0500, Val wrote:
Hi.

The following is a simpler solution, where we can start
with a function f(x), which is a multiple of the required
density, so it specifies its shape.

  #an example, use a function approximating your graphs
  f <- function(x) { 0.7 - 1.25*((x - 1)^2 - 0.4)^2 }

  #plot function f(x)
  x <- seq(0, 2, length=51)
  y <- f(x)
  plot(x, y, type="l")

  #plot an approximate distribution function
  xDistr <- x
  yDistr <- cumsum(y)/sum(y)
  plot(xDistr, yDistr, type="l")

  #generate random sample
  library(splines)
  invDistr <- interpSpline(yDistr, xDistr)
  z <- predict(invDistr, runif(100000))$y

  #plot the empirical distribution function
  lines(sort(z), seq(0, 1, length=length(z)), col=2)

  #for a more accurate comparison subtract the line with slope 1/2
  plot(xDistr, yDistr - xDistr/2, type="l")
  lines(sort(z), seq(0, 1, length=length(z)) - sort(z)/2, col=2)

Hope this helps.

Petr.
#
On Wed, Feb 01, 2012 at 11:01:21AM -0500, Val wrote:
I used a polynomial as f(x), since this is simple.
However, for a better tuning of the shape, a spline
may be better.
For these conditions, it may be better to shift the
function by 24 to the left, so the mean will be 0
and the second moment will be 25. Then try to find
a spline with a shape, which you require, and with
first moment 0 and second moment 25 computed as follows.

  # example spline
  library(splines)
  xOrig  <- 2*(-5:5)
  yOrig <- c(0, 3, 3.9, 4, 3.9, 3.9, 3.9, 4, 3.9, 3, 0)
  ispl <- interpSpline(xOrig, yOrig)

  # see the shape
  x <- seq(-10, 10, length=10001)
  y <- predict(ispl, x)$y
  stopifnot(y >= 0)
  plot(x, y, type="l")

  # compute the moments
  moment1 <- sum(x*y)/sum(y)
  moment2 <- sum(x^2*y)/sum(y)

Here, we get

  moment1
  [1] -2.484611e-17

  moment2
  [1] 25.88646 

I think that a trial and error method may be sufficient
for adjusting the original points to get [0, 25]
within a small error.

Another option is to create several candidate splines
and consider their mixture. The moments are linear
functions of the mixture parameters, so the mixture
parameters may be obtained as a solution of a system
of linear equations. If we have three splines such
that the triangle between the corresponding points
[moment1, moment2] contains the point [0, 25], then
the mixture parameters will be nonnegative and the mixture
well defined.

Petr.