필터 지우기
필터 지우기

How to generate power-law random numbers ?

조회 수: 30 (최근 30일)
Dhanuja Elizabeth Thomas
Dhanuja Elizabeth Thomas 2016년 7월 11일
댓글: Igor Gitelman 2023년 2월 28일
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
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
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
Dhanuja Elizabeth Thomas
Dhanuja Elizabeth Thomas 2016년 7월 11일
Thank you !!
Dhanuja Elizabeth Thomas
Dhanuja Elizabeth Thomas 2016년 8월 18일
@Torsen: As specified above, I'm using k = rand and power_law = (1-k)^(1/(-alpha+1)) (where alpha = 1.5) for getting the random numbers from power law distribution.
Do I need to get a power law histogram when I'm using hist function on the generated power law random numbers, that is,
[f,x] = hist(power_law,100)
N = f/sum(f)
bar(x,N)
But I'm not getting a power law histogram , Kindly help me to solve the issue.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by