【问题标题】:Generate values from a frequency distribution从频率分布生成值
【发布时间】:2017-09-26 06:30:03
【问题描述】:

我目前正在分析一个 16 位二进制字符串 - 类似于 0010001010110100。我有大约 30 个这样的字符串。我在 Matlab 中编写了一个简单的程序,它计算所有 30 个字符串的每一位中 1 的数量。

所以,例如:

1 30

2 15

3 1

4 10

我想生成更多大致遵循上述频率分布的字符串(100 个)。是否有执行此操作的 Matlab(或 Python 或 R)命令?

我正在寻找的是这样的:http://www.prenhall.com/weiss_dswin/html/simulate.htm

【问题讨论】:

  • 如果您只有 30 个字符串,那么 2(我假设这是位位置)如何获得 45 个计数?
  • 这只是一个例子。
  • 我不明白你试图解释的逻辑。为什么不只显示您的真实数据以及您期望的输出?
  • 感谢您修复了令人困惑的示例数据。在 Python 3.6+ 中,您可以使用 random.choices

标签: python matlab frequency-distribution


【解决方案1】:

在 MATLAB 中:只需在 rand 上使用 <(或 lt,小于):

len = 16; % string length
% counts of 1s for each bit (just random integer here)
counts = randi([0 30],[1 len]); 
% probability for 1 in each bit
prob = counts./30;
% generate 100 random strings 
n = 100;
moreStrings = rand(100,len);
% for each bit check if number is less than the probability of the bit
moreStrings = bsxfun(@lt, moreStrings, prob); % lt(x,y) := x < y

在 Python 中:

import numpy as np

len = 16 # string length
# counts of 1's for each bit (just random integer here)
counts = np.random.randint(0, 30, (1,16)).astype(float)
# probability for 1 in each bit
prob = counts/30
# generate 100 random strings 
n = 100
moreStrings = np.random.rand(100,len)
# for each bit check if number is less than the probability of the bit
moreStrings = moreStrings < prob

【讨论】:

    猜你喜欢
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 2020-07-02
    • 2011-08-28
    • 1970-01-01
    • 2016-01-04
    • 2011-02-05
    相关资源
    最近更新 更多