Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating normal distribution data within range 0 and 1

Tags:

r

I am working on my project about the income distribution... I would like to generate random data for testing the theory. Let say I have N=5 countries and each country has n=1000 population and i want to generate random income (NORMAL DISTRIBUTION) for each person in each population with the constraint of income is between 0 and 1 and at same mean and DIFFERENT standard deviation for all countries. I used the function rnorm(n, meanx, sd) to do it. I know that UNIFORM DISTRIBUTION (runif(n,min, max) has some arguments for setting min, max, but no rnorm. Since rnorm doesn't provide the argument for setting min and max value. I have to write a piece of code to check the set of random data to see whether they satisfy my constraints of [0,1] or not.

I successfully generated income data for n=100. However, if i increase n = k times of 100, for eg. n=200, 300 ......1000. My programme is hanging. I can see why the programs is hanging, since it just generate data randomly without constraints of min, max. Therefore, when I do with larger n, the probabilities that i will generate successfully is less than with n=100. And the loop just running again : generate data, failed check.

Technically speaking, to fix this problem, I think of breaking n=1000 into small batches, let say b=100. Since rnorm successfully generate with 100 samples in range [0,1] and it is NORMAL DISTRIBUTION, it will work well if i run the loop of 10 times of 100samples separately for each batch of 100 samples. And then, I will collect all data of 10 * 100 samples into one data of 1000 for my later analysis. However, mathematically speakign, I am NOT SURE whether the constrain of NORMAL DISTRIBUTION for n=1000 is still satisfied or not by doing this way. I attached here my code. Hopefully my explanation is clear to you. All of your opinions will be very useful to my work. Thanks a lot.

 # Update: 
# plot histogram
# create the random data with same mean, different standard deviation and x in range [0,1]

# Generate the output file 
# Generate data for K countries
#---------------------------------------------
# Configurable variables
number_of_populations = 5
n=100  #number of residents (*** input the number whish is k times of 100)
meanx = 0.7
sd_constant = 0.1 # sd = sd_constant + j/50

min=0 #min income
max=1 #max income

#---------------------------------------------
batch =100  # divide the large number of residents into small batch of 100

x= matrix(
  0,                           # the data elements 
  nrow=n,                       # number of rows 
  ncol=number_of_populations,   # number of columns 
  byrow = TRUE)                 # fill matrix by rows 

x_temp = rep(0,n)
# generate income data randomly for each country
for (j in 1:number_of_populations){
  # 1. Generate uniform distribution
  #x[,j] <- runif(n,min, max)
  # 2. Generate Normal distribution
  sd = sd_constant+j/50

  repeat
  {
{
  x_temp <- rnorm(n, meanx, sd)
  is_inside = TRUE
  for (i in 1:n){
    if (x_temp[i]<min || x_temp[i] >max) {
      is_inside = FALSE
      break
    }
  }
}   
if(is_inside==TRUE) {break}
  } #end repeat

  x[,j] <- x_temp

}


# write in csv
# each column stores different income of its residents
working_dir= "D:\\dataset\\"
setwd(working_dir)

file_output = "random_income.csv"
sink(file_output)

write.table(x,file=file_output,sep=",", col.names = F, row.names = F)
sink()
file.show(file_output) #show the file in directory

#plot histogram of x for each population
#par(mfrow=c(3,3), oma=c(0,0,0,0,0))
attach(mtcars)
par(mfrow=c(1,5)) 
for (j in 1:number_of_populations)
{
  #plot(X[,i],y,'xlab'=i)
  hist(x[,j],main="Normal",'xlab'=j)
}
like image 550
Thuy Nguyen Hong Avatar asked Nov 03 '13 22:11

Thuy Nguyen Hong


People also ask

How do you generate a random number from a normal distribution?

Using the inverse function is how we will get our set of normally distributed random values. We will use the RAND() function to generate a random value between 0 and 1 on our Y-axis and then get the inverse of it with the NORM. INV function which will result in our random normal value on the X-axis.

How do I create a normal distribution in Numpy?

random. normal(loc = 0.0, scale = 1.0, size = None) : creates an array of specified shape and fills it with random values which is actually a part of Normal(Gaussian)Distribution. This is Distribution is also known as Bell Curve because of its characteristics shape.


2 Answers

Here's a sensible simple way...

sampnorm01 <- function(n) qnorm(runif(n,min=pnorm(0),max=pnorm(1)))

Test it out:

mysamp <- sampnorm01(1e5)
hist(mysamp)

Thanks to @PatrickPerry, here is a generalized truncated normal, again using the inverse CDF method. It allows for different parameters on the normal and different truncation bounds.

rtnorm <- function(n, mean = 0, sd = 1, min = 0, max = 1) {
    bounds <- pnorm(c(min, max), mean, sd)
    u <- runif(n, bounds[1], bounds[2])
    qnorm(u, mean, sd)
}

Test it out:

mysamp <- rtnorm(1e5, .7, .2)
hist(mysamp)
like image 142
Frank Avatar answered Oct 17 '22 22:10

Frank


You can normalize the data:

x = rnorm(100)

# normalize
min.x = min(x)
max.x = max(x)

x.norm = (x - min.x)/(max.x - min.x)
print(x.norm)
like image 31
Fernando Avatar answered Oct 17 '22 21:10

Fernando