楼主: whx229
5007 12

[问答] 反函数法产生随机变量程序求助 [推广有奖]

  • 0关注
  • 0粉丝

小学生

35%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
39 点
帖子
4
精华
0
在线时间
8 小时
注册时间
2010-9-17
最后登录
2018-9-19

楼主
whx229 发表于 2014-10-16 05:05:45 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
如下所述:
cdf 的反函数分别是x=(2u)^1/2 u是小于1/2的, x=2-(2-2u)^1/2  u是大于等于1/2的
我的方法是先产生n个[0,1]均匀分布,再判断每一个u的大小,然后带到不同式子里,最后产生这个CDF的随机样本
程序如下
simfx=function(n)
{
output=0
x=0
t=0
u=runif(n)
for(i in 1:n)
while(t<n)
{
if(u[i]<(1/2))
{
x=(2*u)^(1/2)
t=t+1
output[t]=x
}
else((1/2)<=u[i])
{
x=2-(2-2*u)^(1/ 2)
t=t+1
output[t]=x
}
t
}
output
}


程序如上,不知道 为什么总是运行不了,求高人解答,实在太感谢了。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:随机变量 反函数 function output while function 反函数 程序 样本

沙发
fortr_cc 发表于 2014-10-16 06:03:32
your code has a few problems,  try the following and see if it does what you need (i only fixed the code, you'll need to make sure the statistics is right, thanks)

simfx=function(n) {
  output=array(0,dim=n)
  u=runif(n)
  for(i in 1:n) {
     if(u[i]< 0.5) {
         x=(2*u[i])^(1/2)
         output[i]=x
     }
     else {
         x=2-(2-2*u[i])^(1/2)
         output[i]=x
     }
  }
  output
}


已有 1 人评分论坛币 收起 理由
admin_kefu + 30 热心帮助其他会员

总评分: 论坛币 + 30   查看全部评分

藤椅
fortr_cc 发表于 2014-10-16 06:06:49
fortr_cc 发表于 2014-10-16 06:03
your code has a few problems,  try the following and see if it does what you need (i only fixed the  ...
you don't need the x, can just do output=(2*u)^(1/2) for the 1st branch, and output=2-(2-2*u)^(1/2) for the 2nd.

板凳
whx229 发表于 2014-10-16 07:04:17
fortr_cc 发表于 2014-10-16 06:06
you don't need the x, can just do output=(2*u)^(1/2) for the 1st branch, and output=2-(2-2*u)^(1/2 ...
I fixed it as your suggestions, but it still was not working.
anyway, thanks
错误于output[t] = 2 - (2 - 2 * u)^(1/2) : 类别为'closure'的下标不对
此外: 警告信息:
1: In output = (2 * u)^(1/2) : 被替换的项目不是替换值长度的倍数

报纸
nuomin 发表于 2014-10-16 09:11:59
试试这一句
  1. ifelse(runif(100)<0.5,sqrt(2*x),2-sqrt(2-2*x))
复制代码
已有 1 人评分论坛币 收起 理由
admin_kefu + 10 热心帮助其他会员

总评分: 论坛币 + 10   查看全部评分

地板
fortr_cc 发表于 2014-10-16 13:10:30
whx229 发表于 2014-10-16 07:04
I fixed it as your suggestions, but it still was not working.
anyway, thanks
错误于output[t] = 2 ...
sorry i forgot the u, i thought you noticed the u in my longer code,  you only need to remove the x in the longer code.   (without u you'll get an error)

this is certainly the basic way of writing it out,  for someone versed in R, he could likely write it in just one line of code
已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

7
fortr_cc 发表于 2014-10-16 13:13:04
fortr_cc 发表于 2014-10-16 13:10
sorry i forgot the u, i thought you noticed the u in my longer code,  you only need to remove the  ...
now i see why, the system automatically removed [ i ] ,  it should be u [ i ], not u,  the error you got is because of not using the index [ i ] for u  (see the code above and remove x)  

8
fortr_cc 发表于 2014-10-16 13:23:35
nuomin 发表于 2014-10-16 09:11
试试这一句
your answer is very neat, it only needs a minor modification to get what was asked for,

simfx=function(n) {
  u=runif(n);  output=ifelse(u<0.5, sqrt(2*u), 2-sqrt(2-2*u))
}

9
nuomin 发表于 2014-10-16 18:37:25
fortr_cc 发表于 2014-10-16 13:23
your answer is very neat, it only needs a minor modification to get what was asked for,

simfx= ...
函数不用显式的定义了,传递的参数就是虚拟函数

10
fortr_cc 发表于 2014-10-18 07:32:16
nuomin 发表于 2014-10-16 18:37
函数不用显式的定义了,传递的参数就是虚拟函数
not quite understand what you mean, i think he wanted sth that can be controlled by an input n, sth like

> v1=simfx(5); v1
[1] 0.5605143 1.3219990 0.5743634 0.9877892 1.6568320
> v2=simfx(10); v2
[1] 0.6671224 1.0667072 0.2414595 0.4811383 0.6274158 0.7628753 1.0127439 1.7258184 1.8567844 1.3401155

the line you provided forced n=100, also, it got an error since the x was likely undefined and not related to the runif(100).   thanks.   

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 17:57