Change from random Distribution to Normal/Poisson Distribution

Hello there ,
I have this code which distrbuted values of C and f_i as randome distrbutuons.
I need to change this distrbutuins to :
1- Normal Distrbutions
2- Poission Distrbutions
but any time I tried those it gave me errors.
s_input = struct('V_POSITION_X_INTERVAL',[0 1],... %(km)
'V_POSITION_Y_INTERVAL',[0 1],... %(km)
'V_SPEED_INTERVAL',[0.01 0.1],... %(km/s) originally set to [0.2 2.2]?
'V_PAUSE_INTERVAL',[0 1],... % pause time (s) originally [1 4]
'V_WALK_INTERVAL',[1 5],... % walk time (s)
'V_DIRECTION_INTERVAL',[-180 180],... % (degrees)
'SIMULATION_TIME',20,... %(s) % originally 20
'NB_NODES', 200); % originally 300
global BaseX BaseY
BaseX = 0.5; %(km)
BaseY = 0.5; %(km)
num_users = s_input.NB_NODES;
s_mobility = Generate_Mobility(s_input);
% timeStep is used for interpolating positions of nodes
% within the positions specified in the s_mobility data generated above
timeStep = 0.5; % (s) % originally 1; shouldn't really affect anything
% except to increase number of node time-positions
% generate and display an animated version of the nodes
% using the timeStep to interpolate position data available
% in the s_mobility position data
test_Animate(s_mobility,s_input,timeStep);
%%
% ======================== %
% PARAMETERS %
% ======================== %
% Loss function parameters
alpha = 128.1;
beta = 37.6;
% Computational Latency:
C_i = 5000; % (eventually want C to be) Between C_i and 3*C_i
C_t=69;
% D_i = 1; % Between 5000-10000 bytes
D_i=500; % modified by wdc 01/09/2021 for use below in calc of D
z)
f_i = randi([1500000000, 3000000000], num_users, 1); % give each user her own f value
%Y = mu + sqrt(2)*sigma*erfinv(2*X-1);
eta = 1/10; % Got value from notes (was 1/1000)
Delta = 0.1;
L = 4; % From notes but it should came From hessian
ggamma = 2; % From notes but it should came From hessian
% Uploading Latency:
global B P_i N_0 S
B = 11000000; % bandwidth is 10 MHz = 10,000,000 HZ; trying .1 MHz?
P_i = 10; % dBm; Transmission power
% No idea what the N_0 is supposed to be here. Is ?noise? = N_0?
% Is the following really in bytes and not bits?
S = 1000000;
N_0 = 10^((-104-30)/10); % noise=-104dbm. Noise
% transmit data size, trying 1 Mbit = 1,000,000 bits
% Define Tau
% Tau = 0.00009563
Tau = 1; % originally 0.00009563
Selected = s_mobility;
% Create a vector of all desired time values/steps, in the form
% [0, timeStep, 2*timeStep, ..., SIMULATION_TIME]
v_t = 0:timeStep:s_input.SIMULATION_TIME;
for nodeIndex = 1:Selected.NB_NODES
Selected.VS_NODE2(nodeIndex).v_t = v_t;
Selected.VS_NODE2(nodeIndex).v_x = interp1(Selected.VS_NODE(nodeIndex).V_TIME,Selected.VS_NODE(nodeIndex).V_POSITION_X,v_t);
Selected.VS_NODE2(nodeIndex).v_y = interp1(Selected.VS_NODE(nodeIndex).V_TIME,Selected.VS_NODE(nodeIndex).V_POSITION_Y,v_t);
end
for N = 1:Selected.NB_NODES
% this tlen should actually be the same each time, being the
% number of (x,y) positions created in the previous for loop above
% for each node
tlen = length(Selected.VS_NODE2(N).v_t);
for t = 1:tlen
Selected.VS_NODE2(N).DISTANCE(t) = disFn(Selected.VS_NODE2(N).v_x(t),Selected.VS_NODE2(N).v_y(t));
Selected.VS_NODE2(N).LOSS(t) = lossFn(alpha,beta,Selected.VS_NODE2(N).DISTANCE(t));
Selected.VS_NODE2(N).ULAT(t) = UlatFn(Selected.VS_NODE2(N).LOSS(t));
end
end
C_i = 5000; % (eventually want C to be) Between C_i and 3*C_i
C_t=69;
C = C_i + (C_t*C_i)*rand(Selected.NB_NODES,1);
f_i = randi([1500000000, 3000000000], num_users, 1); % give each user her own f value
function distance=disFn(x,y)
global BaseX BaseY
% distance=sqrt(sum((x-BaseX).^2)+sum((y-BaseY)^2));
% cleaned this up a bit -- was working but the sums were not necessary
distance=sqrt((x-BaseX).^2 + (y-BaseY)^2);
end
function loss=lossFn(alpha,beta,d)
loss=alpha+beta*log10(d);
end
function Ulatency=UlatFn(loss)
global B P_i N_0 S
h2=10.^(-loss/10);
Ulatency = S/(B*log2(1+P_i.*h2./N_0)) ;
end
function RateLat=RatelatFn(loss)
global B P_i N_0
h2=10.^(loss/10);
RateLat = B*log2(1+P_i.*h2./N_0);
end
Any suggestions willl be helpful.

 Risposta accettata

Well you probably solved it by now, assuming you looked up random in the help, but for what it's worth, here is a solution:
randomNumbers = random('poisson', 5, 1, 100000);
histogram(randomNumbers);
grid on;
fontSize = 15;
title('Poisson Distribution', 'FontSize', fontSize);
xlabel('Value', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);

30 Commenti

By the way, for a Poisson parameter of 69, it's going to look very very close to a normal distribution:
% Generate 100 thousand numbers drawn from a Poisson distribution
randomNumbers = random('poisson', 69, 1, 100000);
histogram(randomNumbers); % Plot their distribution.
grid on;
fontSize = 15;
title('Poisson Distribution', 'FontSize', fontSize);
xlabel('Value', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
That's just the way that the Poisson distribution works. The more you move away from a parameter around 5 or 10, the more Gaussian the distribution is going to appear.
Could you please modify my code because it's giving me an error.
Your code does not define Selected.NB_NODES or num_users so we cannot test it to see what error you are getting.
You did not declare the needed variables so it throws errors saying that it does not know what those variables are. To fix, assign something to them:
% Declare needed variables.
Selected.NB_NODES = 100000;
num_users = 5;
C_i = 5000; % (eventually want C to be) Between C_i and 3*C_i
C_t=69;
C = C_i + (C_t*C_i)*rand(Selected.NB_NODES,1);
f_i = randi([1500000000, 3000000000], num_users, 1); % give each user her own f value
I updated the code above. Sorry might be some parts not related but hopfully it's clear. thanks in advance!
Generate_Mobility function is not defined, so we cannot test to see what error you are getting (and you have not posted the error message.)
I uploaded Generate_Mobility program as attached file. Sorry for inconvience issue.
disFn(), lossFn(), UlatFn() not defined.
I am not getting any error message when I execute on R2021a. It would help to post the error message you are getting.
Brave A
Brave A il 18 Ago 2021
Modificato: Brave A il 18 Ago 2021
how you modify the code please to show the poisson and noramal distrbituon for C and f_i ?
Nothing about code itself, I need to modify it to change the distrbution of C and f_i.
I did not modify the code you have posted. You indicated that your code was generating error messages, so I was looking to see if I could explain the error messages you have.
No I said I need to change the distrbutions and I provided the details. My code runing 100% nothing about it.
The errors when I tried to change the distrbutions of C and f_i to poisson and normal distrbutions.
I need to change C and f_i
C = C_i + (C_t*C_i)*rand(Selected.NB_NODES,1);
f_i = randi([1500000000, 3000000000], num_users, 1); % give each user her own f value
from random to poission version and normal version.
The errors when I tried to change the distrbutions of C and f_i to poisson and normal distrbutions.
Post that version that has the errors and let us see what we can find. And post a copy of the error message, in case it does not show up for us.
For example, hypothetically you might not have the Statistics and Machine Learning Toolbox so the problem might be in creating the random distribution: we would not know as you have not posted a copy of the message.
C = C_i + (C_t*C_i)*poissrnd (Selected.NB_NODES,1);
f_i = poissrnd ([1500000000, 3000000000], num_users, 1); % give each user her own f value
Error using poissrnd (line 28)
Size information is inconsistent.
C = C_i + (C_t*C_i)*normrnd (Selected.NB_NODES,1);
f_i = normrnd ([1500000000, 3000000000], num_users, 1); % give each user her own f value
Error: Error using ==
Matrix dimensions must agree.
So it's only those 2 lines.
I am not aware of any Mathworks function named Poisson .
You can make a distribution use makedist(), or you can create random numbers from a poisson distribution using random() like Image Analyst showed, or you can use the built-in poisson random generator, https://www.mathworks.com/help/stats/poissrnd.html
Brave A
Brave A il 18 Ago 2021
Modificato: Brave A il 18 Ago 2021
no I need same numbers of my code please. and I provided my attempt and errors I got. I read everything online not this what I am looking for. you asked to provide the whole code during the past 6 hours!
I don't think I am asking something not clear.
My questions is to help me to change those 2 lines that's it!
How to change it to poission distrbutions then?
I need to change C and f_i only on my code. I don't need to generate anything from online source.
It is 02:30 in the morning where I am, and I am working on solutions for other people as well. You were failing to post the code we needed to test with, and you were not posting the error messages. You should not be surprised that progress is slow.
f_i = randi([1500000000, 3000000000], num_users, 1); % give each user her own f value
That requests uniformly distributed random numbers between 1500000000 and 3000000000 inclusive. It requests that an array that is num_users rows and 1 column is generated.
f_i = normrnd ([1500000000, 3000000000], num_users, 1)
That requests that normally distributed random numbers are generated. The sigma is to be 1500000000 for the first column, and 3000000000 for the second column. The mu (mean) is requested to be num_users, which is a scalar. Because mu does not have multiple rows and is a scalar, implicit expansion is used and based upon the size of sigma and the size of mu, it says that a 1 x 2 vector must be output and that it shall be an error if the size information that follows mu does not match 1 x 2. The size that is requested is 1, and by special case of a scalar size, a square array 1 x 1 is requested. But 1 x 1 requested size does not match the size implied by sigma and mu, so an error will be generated.
If you need to generate normally distributed random numbers that are confined to the range 1500000000 to 3000000000 then you will need to use an appropriate (scalar) sigma and (scalar) mu, and you will need to use makedist() and you will need to truncate() the distribution, and then tell random() to generate from the truncated distribution. Remember that normally distributed random numbers will be continuous, not discrete; depending on your needs, you might need to round() the numbers.
normrnd (Selected.NB_NODES,1)
That requests that a single normally distributed random number is generated with sigma given by Selected.NB_Nodes and mu 1. If you wanted to generate Selected.NB_NODES x 1 normally distributed random numbers, you should either use randn() to generate with sigma = 1 mu = 0, or you should pass (scalar) sigma and (scalar) mu to normrnd, like
normrnd(sigma, mu, Selected.NB_NODES, 1)
again remember that these will be continuous numbers, not discrete numbers.
C = C_i + (C_t*C_i)*poissrnd (Selected.NB_NODES,1);
That requests that a 1 x 1 array of poisson distributed random numbers be generated with a lambda of Selected.NB_NODES. The result will be integral. If you want to generate NB_NODES x 1 random numbers you need to pass a (scalar) lambda, such as
C = C_i + (C_t*C_i)*poissrnd(lambda, Selected.NB_NODES,1);
Error using normrnd (line 33)
Size information is inconsistent.
Error in test_Execute_CARN_LEARN_FDD (line 61)
f_i = normrnd ([1500000000, 3000000000], num_users, 1);
sorry Iam not aware of different time zone. Thank you for your reply but I got this error.
Again you forgot to attach test_Execute_CARN_LEARN_FDD.m.
And why can't you just use random() like I showed you? What's wrong with that? Why is normrnd() okay but random() not okay?
f_i = normrnd ([1500000000, 3000000000], num_users, 1);
How to add mu and sigma in f_i above?
this is the error because any time I tried to add I got error.
f_i = normrnd(sigma, mu, num_users, 1);
However, if you need the results to be confined to the range [1500000000, 3000000000] then you need a different approach:
lb = 1500000000; ub = 3000000000;
mu = (ub+lb)/2;
normalized_sigma = 3; %prob would have been in bounds is 99.7%
sigma = (ub-lb)/normalized_sigma;
pd = makedist('Normal', 'mu', mu, 'sigma', sigma);
tpd = truncate(pd, lb, ub);
f_i = random(tpd, num_users, 1);
lambda=2; % i don't know what is the best value for lambda?
f_i = poissrnd(lambda, [1500000000,3000000000], num_users, 1);
Thank you both really appreciated, just last error I got this when I convert f_i to poission distrbutions.
You seem to be ignoring my posts, but I'll try one last time.
You need to know what lambda is from the context of your experiment. There is no one best lambda (Poisson parameter). That would be like saying "I want to generate a normal distribution tell me the best mean and standard deviation to use." I can't just say the best mean is 10 and best SD is 3. It's meaningless without knowing your experimental situation.
And simply look to the definition of poissrnd():
r = poissrnd(lambda,sz1,...,szN) generates an array of random numbers from the Poisson distribution with the scalar rate parameter lambda, where sz1,...,szN indicates the size of each dimension.
It says nothing about having/allowing a vector as argument 2. Arguments 2 onwards need to be scalar dimensions of the output array that you want returned.
lb = 1500000000; ub = 3000000000;
mu = (ub+lb)/2;
normalized_sigma = 3; %prob would have been in bounds is 99.7%
sigma = (ub-lb)/normalized_sigma;
pdn = makedist('Normal', 'mu', mu, 'sigma', sigma);
tpdn = truncate(pdn, lb, ub);
pdp = makedist('Poisson', 'lambda', mu);
tpdp = truncate(pdp, lb, ub);
f_in = random(tpdn, num_users, 1); %normal
f_ip = random(tpdp, num_users, 1); %poisson
In both cases, the distribution is arranged to peak half way between the upper bound and the lower bound. In both cases, the distribution is truncated in a way that only values between the lower bound and the upper bound can be generated.
@Image Analyst Thank you so much for your help, I read all replies and I got sense how is my result will be from your previous reply.
@Walter Roberson Thanks a lot Walter for all comments, I think I got some resluts. Thank you both, I really appriciated :)

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by