How to generate power-law random numbers ?
29 ビュー (過去 30 日間)
古いコメントを表示
Hi, I'm confused in generating power law random numbers. Now I'm using the following code for generating power law random numbers:
k = rand;
power_law = k^(1/-(alpha+1)); where 0 < alpha < 2.
I don't know if I'm following the correct way or not. Kindly help me.
1 件のコメント
Igor Gitelman
2023 年 2 月 28 日
Use the CDF method:
% addpath('..\Basic functions\')
fun=@(x) x.^(-2);
range_bottom=10;
range_top=10^4;
data_length=10^5;
[data] = create_population_1(fun,range_bottom,range_top,data_length);
edges = 10.^(1:0.1:5);
% [bins,f_sim] = histlog(data,edges);
[f_sim,edges] = histcounts(data,edges,'Normalization','countdensity');
figure(2)
stairs(edges(1:(end-1)),f_sim)
ax=gca;
ax.XScale='log';
ax.YScale='log';
title('The density Histogram in double log scale')
function [data] = create_population_1(fun,range_bottom,range_top,data_length)
%addpath(fullfile('..\Basic functions\'))
% random number preparation
R=rand(1,data_length);
R_ind=1;
% fun=@(x) x.^-2;
% range_bottom=10;
% range_top=10^4;
% Normalisation
q = integral(fun,range_bottom,range_top);
fun=@(x) fun(x)/q;
% range
range=logspace(log10(range_bottom),log10(range_top),1e4);
range_length=length(range);
CDF=zeros(1,range_length);
for i=2:range_length
CDF(i)=CDF(i-1)+integral(fun,range(i-1),range(i));
end
figure(1)
stairs(range,CDF)
ax=gca;
ax.XScale='log';
title('The CDF')
% creation of the data
data=zeros(1,data_length);
for i=1:data_length
RN=R(R_ind); % random numer
R_ind=R_ind+1;
k=find(RN<CDF);
data(i) = (range(k(1))+range(k(1)-1))/2;
end
end
採用された回答
Torsten
2016 年 7 月 11 日
What is the probability density function for the power law distribution you use ?
p(x)=(alpha-1)*x^(-alpha)
?
In this case, use
k=rand ;
power_law = (1-k)^(1/(-alpha+1))
Best wishes
Torsten.
7 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!