简体   繁体   English

从两个变量创建和采样联合分布

[英]Create and sample joint distribution from two variables

I have a question that's been bothering me.我有一个问题一直困扰着我。

I have a pandas dataframe with two columns corresponding to a and b coefficients which depend on each row (they are not independent):我有一个 pandas dataframe 有两列对应于ab系数,这取决于每一行(它们不是独立的):

            a          b
0   13.967158   2.370449
1   12.375649   2.199846
2   12.005615   2.268646
3   12.030142   1.542835
4   12.119529   1.570510
... ... ...
63  12.215212   1.677631
64  12.221597   1.483855
65  12.758342   2.311847
66  11.712199   2.505323
67  12.393513   1.402272

These can be plotted as:这些可以绘制为:

联合分配

From this, I need to generate random samples from the joint distribution of a and b variables, but I'm not sure how to do it.由此,我需要从ab变量的联合分布中生成随机样本,但我不知道该怎么做。 I tried generating random samples from a normal distribution for each one of these variables, using np.random.normal(mean_variable, sd_variable, 1000) .我尝试使用np.random.normal(mean_variable, sd_variable, 1000)从正态分布中为每个变量生成随机样本。 However, after creating these values, I'm not sure how to join them.但是,在创建这些值之后,我不确定如何加入它们。 Any ideas in this regard would be very useful.在这方面的任何想法都会非常有用。 Regards问候

You need the function that samples a multivariate normal distribution.您需要对多元正态分布进行采样的 function。 This function requires a 1D array of the means and a 2D array of covariances, both of which are readily calculated from your dataframe:这个 function 需要一个一维数组的平均值和一个二维的协方差数组,这两者都可以从您的 dataframe 中轻松计算出来:

numpy.random.multivariate_normal(df.mean(), df.cov())
#array([11.69993186,  1.64400885])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM