Skip to content

Commit

Permalink
Merge pull request #1238
Browse files Browse the repository at this point in the history
* [#187531276] Feature: CODAP provides randomBinomial and randomNormal

* chore: code review tweaks

* * Adding tests for randomNormal and randomBinomial
  • Loading branch information
bfinzer authored May 3, 2024
1 parent ea7a7ec commit e302c6c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
20 changes: 20 additions & 0 deletions v3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"transformIgnorePatterns": [
"/comments/ESM-only (https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) modules that should not be transformed by ts-jest",
"/node_modules/(?!(color-parse|d3|d3-(.+)|decode-uri-component|delaunator|escape-string-regexp|filter-obj|internmap|mime|nanoid|query-string|@?react-leaflet|robust-predicates|split-on-first)/)"
"/node_modules/(?!(d3|d3-(.+)|decode-uri-component|delaunator|escape-string-regexp|filter-obj|internmap|mime|nanoid|query-string|random|@?react-leaflet|robust-predicates|split-on-first)/)"
],
"setupFilesAfterEnv": [
"<rootDir>/src/test/setupTests.ts"
Expand Down Expand Up @@ -198,6 +198,7 @@
"pixi.js": "~7.3.3",
"pluralize": "^8.0.0",
"query-string": "^8.2.0",
"random": "^4.1.0",
"react": "^18.2.0",
"react-colorful": "^5.6.1",
"react-data-grid": "7.0.0-beta.42",
Expand Down
21 changes: 21 additions & 0 deletions v3/src/models/formula/functions/other-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ describe("random", () => {
expect(result).toBeLessThanOrEqual(20)
})
})

describe("randomNormal", () => {
it("returns random numbers between with a mean of 10 and a standard deviation of 5", () => {
const fn = math.compile("randomNormal(10, 5)")
const numbers = Array.from({ length: 1000 }, () => fn.evaluate()),
mean = numbers.reduce((a, b) => a + b) / numbers.length,
stdDev = Math.sqrt(numbers.reduce((a, b) => a + (b - mean) ** 2) / numbers.length)
expect(mean).toBeGreaterThanOrEqual(9.5)
expect(mean).toBeLessThanOrEqual(10.5)
expect(stdDev).toBeGreaterThanOrEqual(4)
expect(stdDev).toBeLessThanOrEqual(6)
})
})

describe("randomBinomial", () => {
it("returns random integers between 0 and 5", () => {
const fn = math.compile("randomBinomial(5, 0.5)")
const integers = Array.from({ length: 100 }, () => fn.evaluate())
expect(integers.every((n) => Math.round(n) === n && n >= 0 && n <= 5)).toBeTruthy()
})
})
34 changes: 30 additions & 4 deletions v3/src/models/formula/functions/other-functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { pickRandom, random } from "mathjs"
import { pickRandom } from "mathjs"
import { Random } from "random"
import { FValue } from "../formula-types"

const randomGen = new Random()

export const otherFunctions = {
// if(expression, value_if_true, value_if_false)
if: {
Expand All @@ -21,7 +24,30 @@ export const otherFunctions = {
random: {
numOfRequiredArguments: 0,
isRandomFunction: true,
// Nothing to do here, mathjs.random() has exactly the same signature as CODAP V2 random() function.
evaluate: (...args: FValue[]) => random(...args as number[])
}
// Nothing to do here, Random.float() has exactly the same signature as CODAP V2 random() function.
evaluate: (...args: FValue[]) => randomGen.float(...args as number[])
},

// randomNormal(mean, standard_deviation) Returns a random number drawn from a normal distribution which, by default,
// has a mean of 0 and a standard deviation of 1.
randomNormal: {
numOfRequiredArguments: 0,
isRandomFunction: true,
// Nothing to do here, Random.normal() has exactly the same signature as CODAP V2 randomNormal() function.
evaluate: (...args: FValue[]) => {
return randomGen.normal(...args as number[])()
}
},

// randomBinomial(n, p) Returns a random integer drawn from a binomial distribution with n independent draws
// (or experiments) each with probability p of success. Defaults are n = 1, p = 0.5.
randomBinomial: {
numOfRequiredArguments: 0,
isRandomFunction: true,
// Nothing to do here, Random.binomial() has exactly the same signature as CODAP V2 randomBinomial() function.
evaluate: (...args: FValue[]) => {
return randomGen.binomial(...args as number[])()
}
},

}

0 comments on commit e302c6c

Please sign in to comment.