@lazy-random/df-algorithm
Version:
1 lines • 6.04 kB
Source Map (JSON)
{"version":3,"file":"index.umd.development.cjs","sources":["../src/irwin-hall.ts","../src/bates.ts","../src/bernoulli.ts","../src/binomial.ts","../src/exponential.ts","../src/geometric.ts","../src/normal.ts","../src/log-normal.ts","../src/pareto.ts"],"sourcesContent":["import { fixZero } from 'num-is-zero';\nimport { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract';\n\n/**\n * https://zh.wikipedia.org/wiki/%E6%AD%90%E6%96%87%E2%80%93%E8%B3%80%E7%88%BE%E5%88%86%E4%BD%88\n * https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution\n *\n * @param random\n * @param {number} n - Number of uniform samples to average (n >= 1)\n * @return {function}\n */\nexport function dfIrwinHall(random: IRNGLike, n: number = 1)\n{\n\texpect(n).integer.gte(0)\n\tn = fixZero(n)\n\n\tif (n === 0)\n\t{\n\t\treturn () => 0\n\t}\n\n\treturn (): number =>\n\t{\n\t\tlet i = n\n\t\tlet sum = 0\n\n\t\twhile (i--)\n\t\t{\n\t\t\tsum += random.next()\n\t\t}\n\n\t\treturn sum\n\t}\n}\n\n","import { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract';\nimport { dfIrwinHall } from './irwin-hall'\n\nexport function dfBates(random: IRNGLike, n: number = 1)\n{\n\t//ow(n, ow.number.integer.positive)\n\texpect(n).integer.gt(0)\n\n\tconst irwinHall = dfIrwinHall(random, n)\n\n\treturn () =>\n\t{\n\t\treturn irwinHall() / n\n\t}\n}\n","import { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract';\n\nexport function dfBernoulli(random: IRNGLike, p = 0.5)\n{\n\t//ow(p, ow.number.gte(0).lte(1))\n\texpect(p).number.gte(0).lte(1)\n\n\treturn () =>\n\t{\n\t\treturn Math.floor(random.next() + p)\n\t}\n}\n\n","import { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract';\n\nexport function dfBinomial(random: IRNGLike, n: number = 1, p: number = 0.5)\n{\n\texpect(n).integer.gt(0)\n\texpect(p).number.gte(0).lte(1)\n\n\treturn () =>\n\t{\n\t\tlet i = n\n\t\tlet x = 0\n\n\t\twhile (i--)\n\t\t{\n\t\t\tif (random.next() < p)\n\t\t\t{\n\t\t\t\tx++\n\t\t\t}\n\t\t}\n\n\t\treturn x\n\t}\n}\n\n","import { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract';\n\nexport function dfExponential(random: IRNGLike, lambda = 1)\n{\n\t//ow(lambda, ow.number.positive)\n\texpect(lambda).number.gt(0)\n\n\treturn () =>\n\t{\n\t\treturn -Math.log(1 - random.next()) / lambda\n\t}\n}\n\n","import { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract';\n\nexport function dfGeometric(random: IRNGLike, p = 0.5)\n{\n\t//ow(p, ow.number.gt(0).lte(1))\n\texpect(p).number.gt(0).lte(1)\n\n\tconst invLogP = 1.0 / Math.log(1.0 - p)\n\n\treturn () =>\n\t{\n\t\treturn Math.floor(1 + Math.log(random.next()) * invLogP)\n\t}\n}\n\n","import { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract'\n\nexport function dfNormal(random: IRNGLike, mu = 0, sigma = 1)\n{\n\t//ow(mu, ow.number)\n\t//ow(sigma, ow.number)\n\n\texpect(mu).number();\n\texpect(sigma).number();\n\n\treturn () =>\n\t{\n\t\tlet x: number, y: number, r: number\n\n\t\tdo\n\t\t{\n\t\t\tx = random.next() * 2 - 1\n\t\t\ty = random.next() * 2 - 1\n\t\t\tr = x * x + y * y\n\t\t}\n\t\twhile (!r || r > 1)\n\n\t\treturn mu + sigma * y * Math.sqrt(-2 * Math.log(r) / r)\n\t}\n}\n\n","import { dfNormal } from './normal'\n\nexport function dfLogNormal(...args: Parameters<typeof dfNormal>)\n{\n\tconst normal = dfNormal(...args)\n\n\treturn () =>\n\t{\n\t\treturn Math.exp(normal())\n\t}\n}\n\n","import { expect } from '@lazy-random/expect';\nimport { IRNGLike } from '@lazy-random/rng-abstract';\n\nexport function dfPareto(random: IRNGLike, alpha: number = 1)\n{\n\t//ow(alpha, ow.number.gt(0))\n\texpect(alpha).gt(0);\n\tconst invAlpha = 1.0 / alpha\n\n\treturn () =>\n\t{\n\t\treturn 1.0 / Math.pow(1.0 - random.next(), invAlpha)\n\t}\n}\n\n"],"names":["n","sum","i","random","next","expect","integer","gt","irwinHall","dfIrwinHall","p","number","gte","lte","x","lambda","Math","log","floor","invLogP","mu","sigma","y","r","exp","normal","alpha","invAlpha","pow"],"mappings":";;;;;;;CAAA;CACA;;;;;;;;;CAcC,EAAA,IAAAA,CAAY,KAAA,CAAA,EAAA;CAIX,IAAA,OAAA,MAAA,CAAA,CAAA;;;aAQOA,CAAA,CAAA;CAEN,IAAA,IAAAC,GAAA,GAAA,CAAA,CAAA;CAGD,IAAA,OAAUC,CAAA,EAAA,EAAA;OAEXD,GAAA,IAAAE,MAAA,CAAAC,IAAA,EAAA,CAAA;;;;;;;CClCD;CAIAC,EAAAA,aAAA,CAAAL,CAAA,CAAAM,CAAAA,OAAA,CAAAC,EAAA,CAAA,CAAA,CAAA,CAAA;SAGWC,SAAU,GAAAC,WAAG,CAAAN,MAAA,EAAAH,CAAA,CAAA,CAAA;CAIvB,EAAA,OAAA,MAAA;CAEC,IAAA,OAAAQ,SAAA,EAAA,GAAAR,CAAA,CAAA;CAED,GAAA,CAAA;;;;;CCfDK,EAAAA,aAAA,CAAAK,CAAA,CAAA,CAAAC,MAAA,CAAAC,GAAA,IAAAC,GAAA,CAAA,CAAA,CAAA,CAAA;GAMC;kCAI0B,KAAAH,CAAA,CAAA,CAAA;CAC1B,GAAA,CAAA;;;;;CCXDL,EAAAA,aAAA,CAAAK,CAAA,CAAA,CAAAC,MAAA,CAAAC,GAAA,IAAAC,GAAA,CAAA,CAAA,CAAA,CAAA;GAMC;;;CASE,IAAA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