Skip to content

Commit 6f22f9b

Browse files
committed
update: CDF
1 parent 9f41d71 commit 6f22f9b

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

src/data/data.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5151,9 +5151,9 @@
51515151
"key": "altDilution",
51525152
"value": 30
51535153
},
5154-
"lifeStd": {
5155-
"key": "lifeStd",
5156-
"value": 12
5154+
"lifeBeta": {
5155+
"key": "lifeBeta",
5156+
"value": 3.8
51575157
},
51585158
"year": {
51595159
"key": "year",

src/data/data.xlsx

-29 Bytes
Binary file not shown.

src/data/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { data, version } from '@/data/data.json'
22
import { database } from '@/database'
3-
import { zoneRandom, cdf } from '@/utils'
3+
import { zoneRandom, weibullCDF } from '@/utils'
44

55
export { version }
66

@@ -335,7 +335,7 @@ export function percentBefore(
335335
sex: Sex
336336
) {
337337
const x = life + age.value
338-
const mean = country.life[sex.value]
339-
const std = getConfig('lifeStd')
340-
return (cdf(x, mean, std) * 100).toFixed(2).replace(/\.00$/, '')
338+
const average = country.life[sex.value]
339+
const beta = getConfig('lifeBeta')
340+
return (weibullCDF(x, beta, average) * 100).toFixed(2).replace(/\.00$/, '')
341341
}

src/utils.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ export function yearsLater(year: number) {
3737
return formatDate(date)
3838
}
3939

40-
export function pdf(x: number, mean: number, std: number): number {
41-
return (
42-
Math.exp(-((x - mean) ** 2) / (2 * std ** 2)) /
43-
(std * Math.sqrt(2 * Math.PI))
44-
)
45-
}
46-
export function cdf(x: number, mean: number, std: number): number {
40+
export function normalCDF(x: number, mean: number, std: number): number {
4741
const z = (x - mean) / std
4842
return 0.5 * (1 + erf(z / Math.sqrt(2)))
4943
}
@@ -67,3 +61,18 @@ function erf(x: number): number {
6761

6862
return sign * y
6963
}
64+
65+
export function weibullCDF(x: number, k: number, scale: number): number {
66+
if (x < 0) {
67+
return 0
68+
}
69+
return 1 - Math.exp(-Math.pow(x / scale, k))
70+
}
71+
72+
export function logNormalCDF(x: number, mean: number, std: number): number {
73+
if (x <= 0) {
74+
return 0
75+
}
76+
const z = (Math.log(x) - mean) / std
77+
return 0.5 * (1 + erf(z / Math.sqrt(2)))
78+
}

0 commit comments

Comments
 (0)