[R] how can generate h(u)=min{a,log(u)} for 0<u<1 in R ?
Daniel Malter
daniel at umd.edu
Tue Dec 22 16:55:19 CET 2009
Okay, since you did not provide the information I asked for, this is the
best guess of what I think you want to do (see commented code below).
#draw a random u*
u.star=runif(1)
#draw a random w
w=runif(1)
#Interval boundaries as defined by u
u=c(0.1,0.2,0.3,0.6,0.9)
#All Us smaller than u*
min.int[u.star>min.int]
#Maximum u smaller than u*
b=max(min.int[u.star>min.int])
#The function gi(u)
gi=function(x,y){min(x,-log(y))*(y>0&y<1)}
# define a
a=1
#evaluate the fraction between h at u* and h at max(u)<u.star
#if the fraction is smaller than w, return u*
#otherwise return NA
rand=ifelse(gi(a,u.star)/gi(a,b)<w,u.star,NA)
print(u.star);print(rand)
HTH
Daniel
-------------------------
cuncta stricte discussurus
-------------------------
-----Original Message-----
From: khazaei at ceremade.dauphine.fr [mailto:khazaei at ceremade.dauphine.fr]
Sent: Tuesday, December 22, 2009 9:13 AM
To: Daniel Malter
Subject: RE: [R] how can generate h(u)=min{a,log(u)} for 0<u<1 in R ?
Ok. For example let for itration i=4 whe have u1=.1,u2=.2,u3=.3,u4=.6
and u5=.8
then
gi(u)=h(u1) when 0.1<u<0.2
=h(u2) when 0.2<u<0.3
=h(u3) when 0.3<u<0.6
=h(u4) when 0.6<u<0.8
where h(ui)=min{a=1,-log(ui)}*ind.(0<ui<1)
now I want to take u* from gi(u). if h(u*)/h(uj)<w where w come from
U(0,1) then I accept u* as a random variable this is my aim.
> Before I start guessing what you want to do, I rather ask you to provide a
> numerical example.
>
> Please take 5 values of u or so, and illustrate what the functions should
> do. That is, provide the values of u, provide a, provide h(u), provide the
> value the indicator variable ind takes (and why), provide gi(u), and
> finally
> provide what/how you want to sample from which function.
>
> Right now, I am only guessing what indices the sum in gi(u) is supposed to
> sum over and when ind takes one or zero. Also I do not understand what you
> mean by "take a u FROM gi," since gi is a function of u. Please be as
> specific as possible. However, if you have the gi(u) and just want to
> sample
> from them, you can sample an arbitrary number with or without replacement
> with the "sample" function.
>
> Daniel
>
> -------------------------
> cuncta stricte discussurus
> -------------------------
> -----Original Message-----
> From: khazaei at ceremade.dauphine.fr [mailto:khazaei at ceremade.dauphine.fr]
> Sent: Tuesday, December 22, 2009 7:15 AM
> To: Daniel Malter
> Subject: RE: [R] how can generate h(u)=min{a,log(u)} for 0<u<1 in R ?
>
> Hi,
> thank you for your explain.
> let me please to state my question precisely:
> this is my problrm exactly:
>
> I want to take a u from gi such that
>
> gi(u)=sum(h(uj)*ind(uj<u<u(j+1))) for j=1,...,i
>
> where 0<uj<uj+1<...<u(i+1)<1 ,
>
> h(uj)=min{a,-log(uj)} 0<uj<1 and a is positive constant.
>
> could you please help me to do it?
>
>
>> Hi,
>>
>> if "a" is a constant, and "u" is a vector of random variables of
>> arbitrary
>> length, each of which is uniformly distributed between 0 and 1, then
>> h(u)
>> is
>> a random sample of the function f.
>>
>> So, if you adjust the code to the desired "a" and to the desired length
>> "n,"
>> then you get a random sample of h(u) of length n. If that is not what
>> you
>> want to do, you need to state your question more precisely.
>>
>> Daniel
>>
>> -------------------------
>> cuncta stricte discussurus
>> -------------------------
>>
>> -----Original Message-----
>> From: khazaei at ceremade.dauphine.fr [mailto:khazaei at ceremade.dauphine.fr]
>> Sent: Monday, December 21, 2009 3:33 PM
>> To: Daniel Malter
>> Subject: RE: [R] how can generate h(u)=min{a,log(u)} for 0<u<1 in R ?
>>
>> Hello Dear
>> thank you for your answer, but I need to generate a sample from the
>> function h(u) not compute the h(u).
>> best
>> khazaei
>>
>>
>>> Hi, see the example below. There must be a way to do this with apply or
>>> tapply, but it always returned an error "incorrect number of
>>> dimensions.
>>> At
>>> least the code below works
>>>
>>> n=100
>>> a=rnorm(n)
>>> u=runif(n)
>>>
>>> f=function(x){min(x[,1],log(x[,2]))}
>>>
>>> x=data.frame(a,u)
>>>
>>> apply(x,1,f) #does not work
>>>
>>> #this works
>>> y=NULL
>>> for(i in 1:n) y[i]=f(x[i,])
>>>
>>> HTH,
>>> Daniel
>>>
>>> -------------------------
>>> cuncta stricte discussurus
>>> -------------------------
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org
>>> [mailto:r-help-bounces at r-project.org]
>>> On
>>> Behalf Of khazaei at ceremade.dauphine.fr
>>> Sent: Monday, December 21, 2009 5:17 AM
>>> To: r-help at r-project.org
>>> Subject: [R] how can generate h(u)=min{a,log(u)} for 0<u<1 in R ?
>>>
>>> Hello,
>>>
>>> How can generate a sample from h(u)=min{a,log(u)} for 0<u<1 in
>>> R,please?
>>>
>>> thank you
>>> khazaei
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>>
>>
>
>
>
More information about the R-help
mailing list