-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beta distribution bug fix, distributions added (#11)
* 2.8.0 - Compertz distribution added, tests added * 2.8.1 - Beta distribution bug fixed Beta distribution for alpha=1 or beta=1 returned NaN every time. Fixed it by changing prng.random(n) logic. * 2.8.2 - Delaporte distribution added
- Loading branch information
1 parent
13f24ce
commit 5789ab3
Showing
25 changed files
with
808 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// @flow | ||
/** | ||
* Compertz Distribution | ||
* This is continuous distribution | ||
* https://en.wikipedia.org/wiki/Gompertz_distribution | ||
* @param nu: number - shape > 0 | ||
* @param b: number - scale > 0 | ||
* @returns Compertz Distributed value | ||
* Created by Alexey S. Kiselev | ||
*/ | ||
|
||
import prng from '../prng/prngProxy'; | ||
import type { MethodError, RandomArray } from '../types'; | ||
import type { IDistribution } from '../interfaces'; | ||
|
||
class Compertz implements IDistribution { | ||
nu: number; | ||
b: number; | ||
|
||
constructor(nu: number, b: number): void { | ||
this.nu = Number(nu); | ||
this.b = Number(b); | ||
} | ||
|
||
/** | ||
* Generates a random number | ||
* @returns a Compertz distributed number | ||
*/ | ||
_random(u: number): number { | ||
return Math.log(1 - Math.log(1 - u) / this.nu) / this.b; | ||
} | ||
|
||
random(): number { | ||
return this._random(prng.random()); | ||
} | ||
|
||
next(): number { | ||
return this._random(prng.next()); | ||
} | ||
|
||
/** | ||
* Generates Compertz distributed numbers | ||
* @param n: number - Number of elements in resulting array, n > 0 | ||
* @returns Array<number> - Compertz distributed numbers | ||
*/ | ||
distribution(n: number): RandomArray { | ||
let compertzArray: RandomArray = [], | ||
random: RandomArray = (prng.random(n): any); | ||
for(let i: number = 0; i < n; i += 1){ | ||
compertzArray[i] = this._random(random[i]); | ||
} | ||
return compertzArray; | ||
} | ||
|
||
/** | ||
* Error handling | ||
* @returns {boolean} | ||
*/ | ||
isError(): MethodError { | ||
if(!this.nu || this.nu <= 0) { | ||
return {error: 'Compertz distribution: you should point parameter "nu" (shape) with positive numerical value'}; | ||
} | ||
if(!this.b || this.b <= 0) { | ||
return {error: 'Compertz distribution: you should point parameter "b" (scale) with positive numerical value'}; | ||
} | ||
return { error: false }; | ||
} | ||
|
||
/** | ||
* Refresh method | ||
* @param newNu: number - new parameter "bu" | ||
* @param newB: number - new parameter "b" | ||
* This method does not return values | ||
*/ | ||
refresh(newNu: number, newB: number): void { | ||
this.nu = Number(newNu); | ||
this.b = Number(newB); | ||
} | ||
|
||
/** | ||
* Class .toString method | ||
* @returns {string} | ||
*/ | ||
toString(): string { | ||
let info = [ | ||
'Compertz Distribution', | ||
`Usage: unirand.compertz(${this.nu}, ${this.b}).random()` | ||
]; | ||
return info.join('\n'); | ||
} | ||
|
||
/** | ||
* Median value | ||
* Information only | ||
* For calculating real median value use analyzer | ||
*/ | ||
get median(): number { | ||
return this._random(0.5); | ||
} | ||
|
||
/** | ||
* All parameters of distribution in one object | ||
* Information only | ||
*/ | ||
get parameters(): {} { | ||
return { | ||
median: this.median | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Compertz; |
Oops, something went wrong.
5789ab3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello,
Is there any reason you added the line to packages.json:
The error I'm not getting when I add unirand to my project is:
error unirand@2.8.2: The engine "node" is incompatible with this module. Expected version "^8.12". Got "14.5.0"
5789ab3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for Your feedback. Issue has been fixed here.
5789ab3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much! It is working now. Also while I'm here, thank you for this great project!