I want to generate 60 random numbers from a poisson distribution with a mean of M = 4 and a range of min = 2 max = 9. Does anybody know how to solve this (preferably in Matlab?).
Best!
If you have the Statistics toolbox, you can use poissrnd...
Related
I'm working with a dataset where the values of my variable of interest are hidden. I have the range (min max), mean, and sd of this variable and for each observation, I have information on which decile the value for observation lies in. Is there any way I can impute some values for this variable using the random number generator or rnormal() suite of commands in Stata? Something along the lines of:
set seed 1
gen imputed_var=rnormal(mean,sd,decile) if decile==1
Appreciate any help on this, thanks!
I am not familiar with Stata, but the following may get you in the right direction.
In general, to generate a random number in a certain decile:
Generate a random number in [(decile-1)/10, decile/10], where decile is the desired decile, from 1 through 10.
Find the quantile of the random number just generated.
Thus, in pseudocode, the following will achieve what you want (I'm not sure about the exact names of the corresponding functions in Stata, though, which is why it's pseudocode):
decile = 4 # 4th decile
# Generate a random number in the decile (here, [0.3, 0.4]).
v = runiform((decile-1)/10, decile/10)
# Convert the number to a normal random number
q = qnormal(v) # Quantile of the standard normal distribution
# Scale and shift the number to the desired mean
# and standard deviation
q = q * sd + mean
This is precisely the suggestion just made by #Peter O. I make the same assumption he did: that by a common abuse of terminology, "decile" is your shorthand for decile class, bin or interval. Historically, deciles are values corresponding to cumulative probabilities 0.1(0.1)0.9, not any bins those values delimit.
. clear
. set obs 100
number of observations (_N) was 0, now 100
. set seed 1506
. gen foo = invnormal(runiform(0, 0.1))
. su foo
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
foo | 100 -1.739382 .3795648 -3.073447 -1.285071
and (closer to your variable names)
gen wanted = invnormal(runiform(0.1 * (decile - 1), 0.1 * decile))
Hello
I would like create an array with numbers from different intervals.
For example, with the following code:
using Distributions
A = rand(Uniform(1,10),1,20)
"A" contains 20 numbers between 1 and 10.
I would like create "B" where "B" contains 20 numbers between 1 and 4, or between 6 and 10 but not between 4 and 6.
Is it possible ?
Thank you
I think for general usecase, you want to make sure that the new probability you're sampling from is still a uniform one, albeit spread across non-connecting ranges.
I hacked together a function that produces a new uniform distribution from multiple disconnected uniform distributions:
using Distributions
function general_uniform(distributions...)
all_dists = [distributions...]
sort!(all_dists, by = D -> minimum(D))
# make sure ranges are non overlapping
#assert all(map(maximum, all_dists)[1:end-1] .<= map(minimum, all_dists)[2:end])
dist_legths = map(D -> maximum(D) - minimum(D), all_dists)
ratios = dist_legths ./ sum(dist_legths)
return MixtureModel(all_dists, Categorical(ratios))
end
Then you can sample from this like this:
B = rand(general_uniform(Uniform(1,4), Uniform(6,10)),1,20)
This will give you a uniform distribution even if your ranges don't have the same length. For example:
general_uniform(Uniform(0,1), Uniform(1,10))
Will sample from range 0-1 with probability of 0.1 and from range 1-10 with probability of 0.9.
For example, the following gives a number around 5:
mean(rand(general_uniform(Uniform(0,9), Uniform(9,10)),1000))
Sure:
numbers = []
for i in 1 : 20
if rand() < 0.5
push!(numbers, rand(Uniform(1,4)))
else
push!(numbers, rand(Uniform(6,10)))
end
end
You can also do a mixture:
D = MixtureModel([Uniform(1,4), Uniform(6,10)], Categorical([0.5,0.5]))
rand(D, 1, 20)
Here you have to specify a probability distribution over which uniform distribution to select from, hence the Categorical. The code above samples from each uniform range with equal probability. You can adjust the weighting by changing the Categorical as you see fit.
Using a mixture model of two uniform distributions
rand(MixtureModel(Uniform[Uniform(1,4),Uniform(6,10)]),1,20)
edit :: this sampling is only correct if the size of the intervals is equal!
hth!
I'm creating a website in which 2 items out of a possible 117 are chosen and compared in different ways. I need a way to assign each of these matchups a unique number so they can be easily stored in a database and what not. I've seen pairing functions, but I cannot find one in which order doesn't matter. For example, I want the unique number for 2 and 17 to be the same as 17 and 2. Is there an equation that will satisfy this?
It depends on what programming language you are using.
In Java for example it would be quite easy, because same seed is producing the same random number sequence. So you could simple use the sum of both random numbers
Long seed = 2L + 17L;
Long seed2 = 17L+2L;
Random random = new Random(seed);
Random random2 = new Random(seed2);
Boolean b = (random.nextLong() == random2.nextLong()) //true
However, this would also return the same value for 1+18, 0+19 and so on - whatever sums up to 19.
So, to get really unique numbers "per pair" you would need to shift one of them. IE, with 117 entries, you could multiply the SMALLER (or larger) by 1000:
Long seed = 2L * 1000 + 17L;
....
Then you have a unique random number for 2,17 and 17,2 - but 19,0 or 0,19 would produce a DIFFERENT random number.
ps.: if it should ALWAYS return the same for 2,19 - the result is not really a random number, isn't it?
I know that the question dates from 2014, but I still wanted to add the following answer.
You could use the product of prime numbers to do this. For example, if your pair is (2,4), then you could use the product of the 2nd prime number (=3) and the 4th prime number (=7) as your id (= 3*7 = 21).
In order to do this though with your 117 possible combinations, you would need to pre-calculate all first 117 prime numbers and store them for example in an array or a hash table and then do something like (in JavaScript):
var primes = [2,3,5,7,...];
var a = 2;
var b = 17;
var id = primes[a-1]*primes[b-1];
Note that if you want to decode them as well, things are going to be more difficult since you would need to calculate the prime factorization of your id.
I have some problems to be implemented in malab. I have N (number of preambles) to be chosen by K (number of devices). How to randomly choose N by K number of devices, so it is possible one preambles chosen by more than one devices?
for example there are 10 unique preambles (N=10) to be randomly chosen by 50 devices (K=50), just in one time with no repetition and each K devices only can choose one from 10 unique preamble by random. I would like to know from those 10 unique preambles, how many are chosen by exactly one devices only and which preambles? and how many preambles are chosen by more than one devices and which preambles?
How to implement this scenario in matlab? I really need your response ASAP. thanks.
Assuming that your preambles (whatever that is in your application) are here:
preambles = [12 13 14];
K = 6;
then generate a vector of random indices into preambles, like this:
random_indices = 1 + floor(rand(1, K) * length(preambles));
which you can then plug into the preambles variable:
preambles(random_indices)
Try this one:
r = randperm(N*K); % generate a random permutation of the integers from 1 through N*K
r = mod(r(1:N:(K-1)*N+1), N); % the random selection results
[uniques,numUnique] = count_unique(r); % the selected preambles (uniques), and how many times they were selected (numUnique)
The count_unique function can be found here
How would you implement a function that is returning a random number from interval 1..1000
in the case there is a number N determining the chance of reaching higher numbers or lower numbers?
It should behave as follows:
e.g.
if N = 0 and we will generate many times the random number we will get a certain equilibrium (every number from interval 1..1000 has equal chance).
if N = 2321 (I call it positive factor) it will be very hard to achieve small number (often will be generated numbers > 900, sometimes numbers near 500 and rarely numbers < 100). The highest the positive factor the highest probability for high numbers
if N = -2321 (negative factor) this will be the opposite of positive factor
It's clear that the generated numbers will create for given N certain characteristic curve. Could you advise me how to achieve this goal and what curves can I create? What possibilities do I have here? How would you limit positive and negative factors etc.
thank you for help
If you generate a uniform random number, and then raise it to a power > 1, it will get smaller, but stay in the range [0, 1]. If you raise it to a power greater than 0 but less than 1, it will get larger, but stay in the range [0, 1].
So you can use the exponent to pick a power when generating your random numbers.
def biased_random(scale, bias):
return random.random() ** bias * scale
sum(biased_random(1000, 2.5) for x in range(100)) / 100
291.59652962214676 # average less than 500
max(biased_random(1000, 2.5) for x in range(100))
963.81166161355998 # but still occasionally generates large numbers
sum(biased_random(1000, .3) for x in range(100)) / 100
813.90199860117821 # average > 500
min(biased_random(1000, .3) for x in range(100))
265.25040459294883 # but still occasionally generates small numbers
This problem is severely underspecified. There are a million ways to solve it as it is mentioned.
Instead of arbitrary positive and negative values, try to think what is the meaning behind them. IMHO, beta distribution is the one you should consider. By selecting the parameters \alpha and \beta you should be appropriately modulate the behavior of your distribution.
See what shapes you can get with certain \alpha and \beta http://en.wikipedia.org/wiki/Beta_distribution#Shapes
http://en.wikipedia.org/wiki/File:Beta_distribution_pdf.svg
Lets for beginning decide that we will pick numbers from [0,1] because it makes stuff simpler.
n is number that represents distribution (0,2321 or -2321) as in example
We need solution only for n > 0, because if n < 0. You can take positive version of n and subtract from 1.
One simple idea for PDF in interval [0,1] is x^n. (or at least this kind of shape)
Calculating CDF is then integrating x^n and is x^(n+1)/(n+1)
Because CDF must be 1 at the end (in our case at 1) our final CDF is than x^(n+1) and is properly weighted
In order to generate this kind distribution from this, we must calaculate quantile function
Quantile function is just inverse of CDF and is in our case. x^(1/(n+1))
And that is it. Your QF is x^(1/(n+1))
To generate numbers from [0,1] you have to pick uniformly distributetd random from [0,1] (most common random function in programming languages)
and than power this whit (1/(n+1))
Only problem I see is that it can be problem to calculate 1-x^(1/(-n+1)) correctly, where n < 0 but i think that you can use log1p,
so it becomes exp(log1p(-x^(1/(-n+1))) if n<0
conclusion whit normalizations
if n>=0: (x^(1/(n/1000+1)))*1000
if n<0: exp(log1p(-(x^(1/(-(n/1000)+1)))))*1000
where x is uniformly distributed random value in interval [0,1]