Random Number Generation with Parameters
Show older comments
Dear All,
Kindly ask for your assistant, I am trying to generate a time series with approx 500 data with the following parameters
1)values between min=0 max=1500
2) 0-100 range 50% of total data, 101-200 25% of total data etc
3) x/x+1 data should be +/-10% at 20% of data, x/x+1 should be between +/-10% - +/-20 at 15% of total data etc
Any ideas will be highly appreciated
7 Comments
John D'Errico
on 17 Aug 2024
Edited: John D'Errico
on 17 Aug 2024
What does this mean:
3) x/x+1 data should be +/-10% at 20% of data, x/x+1 should be between +/-10% - +/-20 at 15% of total data etc
does it say something about the ratio of consecutive terms? And what exactly does it say? I'm sorry, but that line is highly confusing.
Demosthenis Kasastogiannis
on 17 Aug 2024
Edited: Demosthenis Kasastogiannis
on 17 Aug 2024
William Rose
on 1 Sep 2024
This script esitmates the parameters for a Markov model of the system. The script converts the engne power sequence to an equal length sequence of 14 levels, from 1 to 14. Each level is considered a state. It converts the 14x14 matrix of probabilities for each state going to each other state (including the same state) at each time point. Some of the probabilities are zero, bececause there are no transitions from certain levels to certain other levels in the original sequence. The script also computes the mean lifetime (in samples) of each state, although this is not needed for the next stage of simulation, and the lifetimes can be computed directly from the probability matrix. The transition probability matrix and the mean lifetimes are saved in a .mat file.
You can use the transition probability matrix to contruct a 500 point long sequence.
The script and the mat fle created by the script are attached.
[Edit: Add comments and code at the end to convert the "levels" signal (V2) to a signal (U2) with engine power in the original signal units.]
Load the original sequence, convert it to levels, and display the level signal and its histogram.
load('Data_time_power_values'); % load original sequence
U=Data(:,2); % column 2 = engine power
U=U(U~=0); % remove zeros from U
N1=length(U); % sequence length, after removing zeros
% Define sequence V1 as sequence U, quantized into integer-valued levels
V1=ceil(U/100); % V1=level at every time
Ns=length(unique(V1)); % number of states
figure;
subplot(211), plot(1:N1,V1); title('V1'); ylabel('Level')
subplot(212), histogram(V1);
title('V1 Histogram'); ylabel('Counts'); xlabel('Level')
Now simulate the system. Start by loading the Markov model transition probability matrix, computed earlier.
load('Data_time_power_values_MM.mat'); % load transProb
Each row of transProb is like a probability density: the values on each row sum to 1. It will be useful for simulation to have a matrix in which each row is like a cumulative distribution, i.e. the last value on each row is 1.
B=zeros(Ns); % allocate matrix
for i=1:Ns
B(i,:)=cumsum(transProb(i,:)); % cum.dist.function
end
Use B to simulate. Start at level 9, since that is most probable state, as shown by histogram above.
N2=500; % desired sequence length
V2=zeros(N2,1); % allocate vector for sequence of levels
V2(1)=9; % initial value: level 9
for i=2:N2
x=rand; % uniform random in (0,1)
j=V2(i-1); % level of previous state is the row of B to use
V2(i)=find(x<B(j,:),1);
end
Let's plot the sequence and its histogram
figure
subplot(211); plot(1:N2,V2,'-r'); title('V2'); ylabel('Level')
subplot(212); histogram(V2);
title('V2 Histogram'); ylabel('Counts'); xlabel('Level')
The histogram of V2 is similar to the hstogram of the original signal. It is not identical, due to randomness and due to the fact that V2 has only 500 elements. For example, since the original sequence had only 7 samples at level 14, out of 30000, it is not surprising that a sequence with length 500 has no samples with level 14. The probabilities of transitions between each level and each other level are also similar for the original sequence (V1) and the new sequence (V2), but not identical, for the same reasons.
Finally, convert V2 (units of levels) to a signal with engine power in the same units as the original signal. SInce level 1=0-99, and level2=100-199, etc., we convert level 1 to 50, level 2 to 150, etc.
U2=100*V2-50; % convert levels to engine power
figure; plot(1:N2,U2,'-b');
xlabel('Samples'); ylabel('Engine Power'); title('U2')
OK
Demosthenis Kasastogiannis
on 2 Sep 2024
William Rose
on 3 Sep 2024
@Demosthenis Kasastogiannis, you're welcome. You can accept the answer if it suits you. Good luck with your work.
William Rose
on 3 Sep 2024
@Demosthenis Kasastogiannis, thank you for your endorsement.
Accepted Answer
More Answers (1)
Mathy
on 17 Aug 2024
Hi Demosthenis,
I understand that you want to generate random numbers based on a few rules/parameters.
The first and second conditions can be met using the `randi` function. For instance, you can generate 50% of the total data size (i.e., 500 numbers) within the range of 0-100. The next 20% of the data can be in the range of 101-200, while the remaining numbers can fall within the range of 201-1500. For example, to generate the first 100 numbers in the range of 0-100, you can use:
r = randi([0 100],1,100)
However, the third condition is unclear and somewhat confusing. It would be helpful if you could provide more details or elaborate further on it.
Hope this helps!
1 Comment
Demosthenis Kasastogiannis
on 17 Aug 2024
Edited: Demosthenis Kasastogiannis
on 17 Aug 2024
Categories
Find more on Measurements and Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!










