Skip to content

Commit

Permalink
Added Erlang distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviomacedo committed May 13, 2023
1 parent b9ba2db commit 6fff768
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ minutes, in this case:
## Using other distributions

Although the Markov process is the most commonly used to model queueing systems,
your particular use case may be better modeled by some process. For example, in
many applications, batch jobs are triggered periodically by some scheduler
like [cron]. The service time may still be exponentially distributed, but the
inter-arrival time is now deterministic. To model these situations, use the
lower-level API provided by the _general_ process:
your particular use case may be better modeled by some other process. For
example, in many applications, batch jobs are triggered periodically by some
scheduler like [cron]. The service time may still be exponentially distributed,
but the inter-arrival time is now deterministic. To model these situations, use
the lower-level API provided by the `general` process:

```ts
const simulator = BatchSimulator.general(stack);
Expand All @@ -225,9 +225,10 @@ const report = simulator.simulate([{
}]);
```

If you need to use some more probability distribution that is not available in
the `Distribution` class, you can bring your own, by implementing the
`IDistribution` interface.
In addition to deterministic and exponential distributions, the simulator also
offers an implementation of the [Erlang distribution]. But If you need to use
some probability distribution that is not available in the `Distribution` class,
you can bring your own, by implementing the `IDistribution` interface.

## Unsupported features (yet)

Expand All @@ -248,4 +249,6 @@ Some of the AWS Batch features are not being simulated by this library:

[mmc]: https://www.wikiwand.com/en/M/M/c_queue

[cron]: https://en.wikipedia.org/wiki/Cron
[cron]: https://en.wikipedia.org/wiki/Cron

[Erlang distribution]: https://en.wikipedia.org/wiki/Erlang_distribution
26 changes: 25 additions & 1 deletion src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface IDistribution {

export class Distribution {
static deterministic(value: number): IDistribution {
if (value <= 0) {
throw new Error(`The deterministic time must be positive. Got ${value}`);
}

return new class implements IDistribution {
nextTime(): number {
return value;
Expand All @@ -34,9 +38,29 @@ export class Distribution {
}

static exponential(lambda: number): IDistribution {
if (lambda <= 0) {
throw new Error(`The parameter of the exponential distribution must be positive. Got ${lambda}`);
}

return this.erlang(1, lambda);
}

static erlang(k: number, lambda: number): IDistribution {
if (lambda <= 0) {
throw new Error(`The rate parameter (λ) of the Erlang distribution must be positive. Got ${lambda}`);
}

if (!(Number.isInteger(k) && k > 0)) {
throw new Error(`The shape parameter (k) of the Erlang distribution must be a positive integer. Got ${k}`);
}

return new class implements IDistribution {
nextTime(): number {
return -Math.log(Math.random()) / lambda;
let sum = 0;
for (let i = 0; i < k; i++) {
sum += Math.log(Math.random());
}
return -sum / lambda;
}
};
}
Expand Down

0 comments on commit 6fff768

Please sign in to comment.