-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,36 @@ | ||
const count = (data: number[] | string[], stair = 0): {[key: string | number]:number} => { | ||
let count : {[key: string | number]:number} = {}; | ||
data.forEach((d)=>{ | ||
if (stair > 0) { | ||
if (typeof d === 'string'){ | ||
console.log("stair is not defined for string data"); | ||
return {}; | ||
} | ||
else d = Math.floor(d / stair); | ||
} | ||
if (count[d] === undefined) count[d] = 0; | ||
count[d] += 1; | ||
}); | ||
return count; | ||
} | ||
|
||
// opts := 0 -> no normalization | ||
// otherwise -> normalize | ||
export const gini_simpson = (data: number[] | string[], stair = 0, opts = 0): number => { | ||
const n = data.length; | ||
let cnt = count(data, stair); | ||
let sum = 0; | ||
sum = Object.values(cnt).reduce((a,b) => a+b*(b-1)); | ||
sum = 1 - sum / (n*(n-1)); | ||
if (opts > 0) sum = sum / (1 - 1/opts); | ||
return sum; | ||
} | ||
|
||
export const shannon = (data: number[] | string[], stair = 0): number => { | ||
const n = data.length; | ||
let cnt = count(data, stair); | ||
let sum = 0; | ||
sum = Object.values(cnt).reduce((a,b) => a+b*Math.log(b)); | ||
sum = -sum / n; | ||
return sum; | ||
} |