From: Gonzalo on
Hi all,

I need to generate RN's from a double hyperbolic tangent function.

f(x) = p(1) .* (tanh((x - p(2)) ./ p(4))- tanh((x - p(3)) ./ p(5))

I can't do it using the inverse-transform method because it's not possible to solve for x. Does anybody know of a routine that works the Composition, or convolution or any other methods??

Thanks
From: Gonzalo on
Thanks Walter,

I found the coefficients with a nonlinear regression to be:

p = [4.4786 16.8696 28.0379 3.3347 1.9488];

Could you explain a little more how to solve numerically??

thanks a lot,

Gonzalo

Walter Roberson <roberson(a)hushmail.com> wrote in message <hu1h27$e6l$1(a)canopus.cc.umanitoba.ca>...
> Gonzalo wrote:
>
> > I need to generate RN's from a double hyperbolic tangent function.
> >
> > f(x) = p(1) .* (tanh((x - p(2)) ./ p(4))- tanh((x - p(3)) ./ p(5))
> >
> > I can't do it using the inverse-transform method because it's not
> > possible to solve for x. Does anybody know of a routine that works the
> > Composition, or convolution or any other methods??
>
> x can be solved for numerically given the other parameters. The key value to
> be solved for is,
>
> RootOf(2*_Z*p(5)-2*p(3)+2*p(2)-p(4)*ln((p(1)*exp(_Z)^2+p(1)+exp(_Z)^2-1)/(p(1)*
> exp(_Z)^2+p(1)-exp(_Z)^2+1)))
>
> where RootOf is a notation indicating that the value _Z should be found such
> that the expression evaluates to 0 at _Z .
>
> However, for some combinations of parameters, some of the x might be
> imaginary. For example, p(1)=1/2, p(2)=1/3, p(3)=1/5, p(4)=1/7 and p(5) from
> about 0.18 to about 0.54, whereas with p(1)=2, p(2)=3, p(3)=5, p(4)=7 then in
> my experiments I do not see any p(5) that would make the expression imaginary.
From: Roger Stafford on
"Gonzalo " <glpita(a)gmail.com> wrote in message <hu1c4b$5t7$1(a)fred.mathworks.com>...
> Hi all,
>
> I need to generate RN's from a double hyperbolic tangent function.
>
> f(x) = p(1) .* (tanh((x - p(2)) ./ p(4))- tanh((x - p(3)) ./ p(5))
>
> I can't do it using the inverse-transform method because it's not possible to solve for x. Does anybody know of a routine that works the Composition, or convolution or any other methods??
>
> Thanks

I am guessing that you mean for f(x) to be the probability density function for your random variable, though you didn't say so. Is that correct? If so, the integral of f(x) will be a linear combination of two log cosh terms, and for that reason the inverse of the cdf should be comparatively easy to obtain from fzero. If this fzero process isn't too slow for you, you could then generate the random variable using the rand function in connection with fzero in accordance with the standard method.

Of course your five p-values are subject to some constraints. The integral of f(x) must be unity, and there should be no x for which f(x) becomes negative. You would have to be sure of these conditions before attempting to generate a valid random variable.

Roger Stafford
From: Gonzalo on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <hu1l4k$ob7$1(a)fred.mathworks.com>...
> "Gonzalo " <glpita(a)gmail.com> wrote in message <hu1c4b$5t7$1(a)fred.mathworks.com>...
> > Hi all,
> >
> > I need to generate RN's from a double hyperbolic tangent function.
> >
> > f(x) = p(1) .* (tanh((x - p(2)) ./ p(4))- tanh((x - p(3)) ./ p(5))
> >
> > I can't do it using the inverse-transform method because it's not possible to solve for x. Does anybody know of a routine that works the Composition, or convolution or any other methods??
> >
> > Thanks
>
> I am guessing that you mean for f(x) to be the probability density function for your random variable, though you didn't say so. Is that correct? If so, the integral of f(x) will be a linear combination of two log cosh terms, and for that reason the inverse of the cdf should be comparatively easy to obtain from fzero. If this fzero process isn't too slow for you, you could then generate the random variable using the rand function in connection with fzero in accordance with the standard method.
>
> Of course your five p-values are subject to some constraints. The integral of f(x) must be unity, and there should be no x for which f(x) becomes negative. You would have to be sure of these conditions before attempting to generate a valid random variable.
>
> Roger Stafford

Roger,

I got the integral of f(x) and normalized the function so that Int(f(x)) = 1; the 2nd condition is also met thanks to the type of function chosen. Now, could please explain a little bit more on how to use fzero to generate the random numbers from f(x)??

thanks
From: Roger Stafford on
"Gonzalo " <glpita(a)gmail.com> wrote in message <hu1o0g$44h$1(a)fred.mathworks.com>...
> ......
> I got the integral of f(x) and normalized the function so that Int(f(x)) = 1; the 2nd condition is also met thanks to the type of function chosen. Now, could please explain a little bit more on how to use fzero to generate the random numbers from f(x)??
> ......

Gonzalo, you still haven't stated definitely that f(x) is to be the probability density function for your generated random variable. Is that true?

On the assumption that it is indeed true, I've looked a little more closely into the problem. To make f(x) a valid density function, one condition is certainly true and I strongly suspect another condition must also hold as well. Certainly you must have p(1) = 1/(2*(p3-p2)) in order to have an integral from minus infinity to plus infinity of one. That would be your normalization factor. The other strongly suspected condition is that p4 and p5 must be exactly equal. It can be shown rigorously that if you set these equal and also have p3>p2, then f(x) is always positive. Otherwise I am almost sure (99%) that somewhere along the x range a value can be found that makes f(x) negative, and I don't think you want that. If you lived up to both the above conditions, it would unfortunately leave you with only three independent parameters.

However there is a decided advantage for you in doing so. Applying this constraint would leave you with a cdf function whose inverse is directly computable using the inverse hyperbolic tangent function atanh. I can show you the details later. No call on fzero would be necessary. You could then make direct use of the rand function using this inverse cdf function.

Does this possibility interest you, given that I am only fairly certain about the above?

Roger Stafford