0

enter image description here

I want to simulate demand values that follows different distributions (ex above: starts of linear> exponential>invlog>etc) I'm a bit confused by the notion of probability distributions but thought I could use rnorm, rexp, rlogis, etc. Is there any way I could do so?

I think it may be this but in R: Generating smoothed randoms that follow a distribution

  • How will you be given the distribution? From sample data? A function? – G5W Jun 07 '17 at 17:43
  • 2
    Looks like you need to dive into mixture distributions. See: https://stackoverflow.com/questions/23480529/r-function-to-generate-a-mixture-distribution – Alex Knorre Jun 07 '17 at 17:47
  • I am creating a game where the distribution is based on a player's performance. So if they are doing well, the demand will be triggered to follow the exponential curve and if they are performing poorly, the demand will be triggered to follow an inverse logarithmic curve, etc – flightless13wings Jun 07 '17 at 17:48
  • Could you please give one example of the type of distribution curve that you might use? – G5W Jun 07 '17 at 17:56
  • Scenario: I give player demand signals that steadily increase in a linear pattern. After 5 times of matching my demand signals, I give the player demand that goes up exponentially. After sort of matching my demand 5 times, I give the player demand that tapers down following a negative logarithm. Depending on other variables, I will adjust mean and sd accordingly. I won't be mixing distributions as they are triggered at different moments. Thanks! – flightless13wings Jun 07 '17 at 18:01

1 Answers1

1

Simulating random values from commonly-used probability distributions in R is fairly trivial using rnorm(), rexp(), etc, if you know what distribution you want to use, as well as its parameters. For example, rnorm(10, mean=5, sd=2) returns 10 draws from a normal distribution with mean 5 and sd 2.

rnorm(10, mean = 5, sd = 2)
##  [1] 5.373151 7.970897 6.933788 5.455081 6.346129 5.767204 3.847219 7.477896 5.860069 6.154341

## or here's a histogram of 10000 draws...
hist(rnorm(10000, 5, 2))

You might be interested in an exponential distribution - check out hist(rexp(10000, rate=1)) to get the idea.

The easiest solution will be to investigate what probability distribution(s) you're interested in and their implementation in R.

It is still possible to return random draws from some custom function, and there are a few techniques out there for doing it - but it might get messy. Here's a VERY rough implementation of drawing randomly from probabilities defined by the region of x^3 - 3x^2 + 4 between zero and 3.

## first a vector of random uniform draws from the region
unifdraws <- runif(10000, 0, 3)

## assign a probability of "keeping" draws based on scaled probability
pkeep <- (unifdraws^3 - 3*unifdraws^2 + 4)/4

## randomly keep observations based on this probability
keep <- rbinom(10000, size=1, p=pkeep)
draws <- unifdraws[keep==1]

## and there it is!
hist(draws)

## of course, it's less than 10000 now, because we rejected some
length(draws)
## [1] 4364
Matt Tyers
  • 2,125
  • 1
  • 14
  • 23