Skip to content
Prev 283915 / 398502 Next

Generate data - function

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.