从频率分布生成值
Generate values from a frequency distribution
我目前正在分析一个 16 位二进制字符串——类似于 0010001010110100。我有大约 30 个这样的字符串。我在 Matlab 中编写了一个简单的程序,计算所有 30 个字符串的每个位中 1 的数量。
因此,例如:
1 30
2 15
3 1
4 10
等等
我想生成更多大致遵循上述频率分布的字符串 (100s)。是否有 Matlab(或 Python 或 R)命令可以做到这一点?
我正在寻找的是这样的东西:http://www.prenhall.com/weiss_dswin/html/simulate.htm
在 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
我目前正在分析一个 16 位二进制字符串——类似于 0010001010110100。我有大约 30 个这样的字符串。我在 Matlab 中编写了一个简单的程序,计算所有 30 个字符串的每个位中 1 的数量。
因此,例如:
1 30
2 15
3 1
4 10
等等
我想生成更多大致遵循上述频率分布的字符串 (100s)。是否有 Matlab(或 Python 或 R)命令可以做到这一点?
我正在寻找的是这样的东西:http://www.prenhall.com/weiss_dswin/html/simulate.htm
在 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