14712 10

[编程问题求助] stata中怎么生成服从联合正态分布的随机变量 [推广有奖]

  • 1关注
  • 0粉丝

已卖:17份资源

本科生

99%

还不是VIP/贵宾

-

威望
0
论坛币
1395 个
通用积分
141.6372
学术水平
3 点
热心指数
9 点
信用等级
6 点
经验
158 点
帖子
48
精华
0
在线时间
175 小时
注册时间
2016-3-9
最后登录
2022-8-4

楼主
学法语去巴黎 发表于 2016-5-18 18:49:49 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
(u,v)服从联合正态分布,均值是0,方差是1,相关系数是0.8。想要在stata中随机生成u和v,怎么办?求各位赐教!
二维码

扫码加我 拉你入群

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

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

关键词:Stata 正态分布 tata 随机变量 相关系数 正态分布 联合

沙发
乐趣老梁 发表于 2016-5-18 23:10:48
gen data=rnormal(均值,标准差)
相关系数那是无法满足的。

藤椅
星期天的早晨 发表于 2016-5-18 23:27:58
不懂楼主的意思,一般在是均值和标准差确定的情况下,生成多少个满足这种情况的随机变量数吧?

板凳
学法语去巴黎 发表于 2016-5-20 21:16:14
乐趣老梁 发表于 2016-5-18 23:10
gen data=rnormal(均值,标准差)
相关系数那是无法满足的。
谢谢。但是这个只能生成一元随机变量,不是我想要的。

报纸
学法语去巴黎 发表于 2016-5-20 21:17:26
星期天的早晨 发表于 2016-5-18 23:27
不懂楼主的意思,一般在是均值和标准差确定的情况下,生成多少个满足这种情况的随机变量数吧?
您说的可能是一般情况,但是我们老师让做的是生成二维的随机变量。

地板
Captain-CUI 学生认证  发表于 2016-5-21 06:41:12 来自手机
学法语去巴黎 发表于 2016-5-20 21:17
您说的可能是一般情况,但是我们老师让做的是生成二维的随机变量。
看看cameron的书吧!

7
蓝色 发表于 2016-5-21 09:08:56
看stata的手册,找命令

Title

    [D] drawnorm -- Draw sample from multivariate normal distribution


Syntax

        drawnorm newvarlist [, options]

    options                 Description
    ---------------------------------------------------------------------------------------------
    Main
      clear                 replace the current dataset
      double                generate variable type as double; default is float
      n(#)                  # of observations to be generated; default is current number
      sds(vector)           standard deviations of generated variables
      corr(matrix|vector)   correlation matrix
      cov(matrix|vector)    covariance matrix
      cstorage(full)        correlation/covariance structure is stored as a symmetric k*k matrix
      cstorage(lower)       correlation/covariance structure is stored as a lower triangular
                              matrix
      cstorage(upper)       correlation/covariance structure is stored as an upper triangular
                              matrix
      forcepsd              force the covariance/correlation matrix to be positive semidefinite
      means(vector)         means of generated variables; default is means(0)

    Options
      seed(#)               seed for random-number generator
    ---------------------------------------------------------------------------------------------

Menu

    Data > Create or change data > Other variable-creation commands > Draw sample from normal
        distribution


Description

    drawnorm draws a sample from a multivariate normal distribution with desired means and
    covariance matrix.  The default is orthogonal data with mean 0 and variance 1.  The
    covariance matrix may be singular.  The values generated are a function of the current
    random-number seed or the number specified with set seed(); see [R] set seed.


Options

        +------+
    ----+ Main +---------------------------------------------------------------------------------

    clear specifies that the dataset in memory be replaced, even though the current dataset has
        not been saved on disk.

    double specifies that the new variables be stored as Stata doubles, meaning 8-byte reals. If
        double is not specified, variables are stored as floats, meaning 4-byte reals. See [D]
        data types.

    n(#) specifies the number of observations to be generated.  The default is the current number
        of observations.  If n(#) is not specified or is the same as the current number of
        observations, drawnorm adds the new variables to the existing dataset; otherwise,
        drawnorm replaces the data in memory.

    sds(vector) specifies the standard deviations of the generated variables.  sds() may not be
        specified with cov().

    corr(matrix|vector) specifies the correlation matrix.  If neither corr() nor cov() is
        specified, the default is orthogonal data.

    cov(matrix|vector) specifies the covariance matrix.  If neither cov() nor corr() is
        specified, the default is orthogonal data.

    cstorage(full|lower|upper) specifies the storage mode for the correlation or covariance
        structure in corr() or cov().  The following storage modes are supported:

        full specifies that the correlation or covariance structure is stored (recorded) as a
            symmetric k*k matrix.

        lower specifies that the correlation or covariance structure is recorded as a lower
            triangular matrix.  With k variables, the matrix should have k(k+1)/2 elements in the
            following order:

                C(11) C(21) C(22) C(31) C(32) C(33) ... C(k1) C(k2) ... C(kk)

        upper specifies that the correlation or covariance structure is recorded as an upper
            triangular matrix.  With k variables, the matrix should have k(k+1)/2 elements in the
            following order:

                C(11) C(12) C(13) ... C(1k) C(22) C(23) ... C(2k) ...  C(k-1k-1) C(k-1k) C(kk)

        Specifying cstorage(full) is optional if the matrix is square.  cstorage(lower) or
        cstorage(upper) is required for the vectorized storage methods.  See storage modes for
        examples.

    forcepsd modifies the matrix C to be positive semidefinite (psd), and so be a proper
        covariance matrix.  If C is not positive semidefinite, it will have negative eigenvalues.
        By setting negative eigenvalues to 0 and reconstructing, we obtain the least-squares
        positive-semidefinite approximation to C.  This approximation is a singular covariance
        matrix.

    means(vector) specifies the means of the generated variables.  The default is means(0).

        +---------+
    ----+ Options +------------------------------------------------------------------------------

    seed(#) specifies the initial value of the random-number seed used by the runiform()
        function.  The default is the current random-number seed.  Specifying seed(#) is the same
        as typing set seed # before issuing the drawnorm command.


Examples

    Generate 2000 independent observations (x,y); x with mean 2 and standard deviation .5; y with
    mean 3 and standard deviation 2.

        . matrix m = (2,3)
        . matrix sd = (.5,2)
        . drawnorm x y, n(2000) means(m) sds(sd)
        . summarize

    Draw a sample of 1000 observations from a bivariate standard normal distribution, with
    correlation 0.5.

        . clear
        . matrix C = (1, .5 \ .5, 1)
        . drawnorm x y, n(1000) corr(C)
        . summarize

    Equivalently,

        . clear
        . matrix C = (1, .5, 1)
        . drawnorm x y, n(1000) corr(C) cstorage(lower)
        . summarize


已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
Sunknownay + 2 + 2 + 2 热心帮助其他会员
夏目贵志 + 10 + 2 热心帮助其他会员

总评分: 经验 + 10  学术水平 + 2  热心指数 + 4  信用等级 + 2   查看全部评分

8
学法语去巴黎 发表于 2016-7-8 16:32:31
蓝色 发表于 2016-5-21 09:08
看stata的手册,找命令

Title
谢谢您的回答,我们老师也给了一种方法
gen u=invnorm(uniform())
gen v=0.8*u+invnorm(uniform())
类似这样

9
光之巨人88 发表于 2018-6-6 15:35:39
蓝色 发表于 2016-5-21 09:08
看stata的手册,找命令

Title
请问这种命令产生的随机变量一般会出现什么问题?除了非正定矩阵问题外,option cstorage() required with vectorized corr/cov matrix又表示什么问题? 我的命令是:mat C=(1,0.64,0.58/0.64,1,0/0.58,0,1)  drawnorm y x1 x2,m(2,3,3) sd(2.5,2.3,3) n(1500) corr(C)。结果显示上面提示信息,怎么解决这个问题呢?

10
风雨阁主人 学生认证  发表于 2018-10-24 16:06:54
蓝色 发表于 2016-5-21 09:08
看stata的手册,找命令

Title
大佬,如果我想从混合正态分布生成随机数怎么办呢?目前egenmore上说可以从有两个component的混合正态分布中生成,但是三个及以上的咋办呢?

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 15:02