@dra2020/dra-analytics 4.1.7 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -23,7 +23,9 @@ them for several elections, one at a time, and then generate statistics for them
23
23
  This repo & package can be integrated into other tools.
24
24
  The libraries here are normally included like this:
25
25
 
26
- import { Compactness, Equal, Graph, Minority, Partisan, Splitting } from '@dra2020/dra-analytics';
26
+ ```TypeScript
27
+ import { Compactness, Equal, Graph, Minority, Partisan, Splitting } from '@dra2020/dra-analytics';
28
+ ```
27
29
 
28
30
  Then the various functions of the different sets are available under their appropriate symbol.
29
31
 
package/cli/partisan.ts CHANGED
@@ -161,10 +161,10 @@ export function printPartisanDetailsRow(xx: string, name: string, N: number, Vf:
161
161
  (s.bias.gamma) ? trim(s.bias.gamma) : s.bias.gamma,
162
162
 
163
163
  trim(s.bias.deviation), // Simple deviation from best # of seats
164
- // (s.experimental.lProp) ? trim(s.experimental.lProp) : s.experimental.lProp,
165
- (s.experimental.lPropAlt) ? trim(s.experimental.lPropAlt) : s.experimental.lPropAlt,
166
- // trim(s.impact.unearnedS),
167
- (s.experimental.lUE) ? trim(s.experimental.lUE) : s.experimental.lUE
168
- // (s.experimental.lSym) ? trim(s.experimental.lSym) : s.experimental.lSym,
164
+ // // (s.experimental.lProp) ? trim(s.experimental.lProp) : s.experimental.lProp,
165
+ // (s.experimental.lPropAlt) ? trim(s.experimental.lPropAlt) : s.experimental.lPropAlt,
166
+ // // trim(s.impact.unearnedS),
167
+ // (s.experimental.lUE) ? trim(s.experimental.lUE) : s.experimental.lUE
168
+ // // (s.experimental.lSym) ? trim(s.experimental.lSym) : s.experimental.lSym,
169
169
  );
170
170
  }
@@ -1844,11 +1844,11 @@ function makePartisanScorecard(Vf, VfArray, bLog = false) {
1844
1844
  // distribution than Rd.
1845
1845
  const cD = (0, responsiveness_1.estCompetitiveDistricts)(VfArray);
1846
1846
  const cDf = (0, responsiveness_1.estCompetitiveDistrictsShare)(cD, N);
1847
- // EXPERIMENTAL
1848
- const lSym = (0, bias_1.estLocalAsymmetry)(Vf, dSVpoints, rSVpoints);
1849
- const lProp = (0, bias_1.estLocalDisproportionality)(Vf, dSVpoints);
1850
- const lPropAlt = (0, bias_1.estLocalDisproportionalityAlt)(Vf, N, dSVpoints);
1851
- const lUE = (0, bias_1.estLocalUnearnedSeats)(Vf, N, dSVpoints);
1847
+ // // EXPERIMENTAL
1848
+ // const lSym = estLocalAsymmetry(Vf, dSVpoints, rSVpoints);
1849
+ // const lProp = estLocalDisproportionality(Vf, dSVpoints);
1850
+ // const lPropAlt = estLocalDisproportionalityAlt(Vf, N, dSVpoints);
1851
+ // const lUE = estLocalUnearnedSeats(Vf, N, dSVpoints);
1852
1852
  const biasMeasurements = {
1853
1853
  bestS: bestS,
1854
1854
  bestSf: bestSf,
@@ -1887,12 +1887,13 @@ function makePartisanScorecard(Vf, VfArray, bLog = false) {
1887
1887
  const RWins = VfArray.filter(x => x <= 0.5); // Ties credited to R's
1888
1888
  const averageDVf = (DWins.length > 0) ? U.avgArray(DWins) : undefined;
1889
1889
  const averageRVf = (RWins.length > 0) ? U.avgArray(RWins) : undefined;
1890
- const experimentalMetrics = {
1891
- lSym: lSym,
1892
- lProp: lProp,
1893
- lPropAlt: lPropAlt,
1894
- lUE: lUE
1895
- };
1890
+ // // EXPERIMENTAL
1891
+ // const experimentalMetrics: T.Experimental = {
1892
+ // lSym: lSym,
1893
+ // lProp: lProp,
1894
+ // lPropAlt: lPropAlt,
1895
+ // lUE: lUE
1896
+ // };
1896
1897
  const s = {
1897
1898
  bias: biasMeasurements,
1898
1899
  impact: impactMeasurements,
@@ -1901,7 +1902,7 @@ function makePartisanScorecard(Vf, VfArray, bLog = false) {
1901
1902
  rSVpoints: rSVpoints,
1902
1903
  averageDVf: averageDVf,
1903
1904
  averageRVf: averageRVf,
1904
- experimental: experimentalMetrics,
1905
+ // experimental: experimentalMetrics,
1905
1906
  details: {}
1906
1907
  };
1907
1908
  return s;
@@ -1986,14 +1987,6 @@ exports.calcDisproportionality = calcDisproportionality;
1986
1987
  exports.calcBigR = calcBigR;
1987
1988
  exports.calcMinimalInverseResponsiveness = calcMinimalInverseResponsiveness;
1988
1989
  exports.calcGamma = calcGamma;
1989
- exports.estLocalAsymmetry = estLocalAsymmetry;
1990
- exports.rangeAsymmetry = rangeAsymmetry;
1991
- exports.estLocalDisproportionality = estLocalDisproportionality;
1992
- exports.rangeDisproportionality = rangeDisproportionality;
1993
- exports.estLocalDisproportionalityAlt = estLocalDisproportionalityAlt;
1994
- exports.rangeDisproportionalityAlt = rangeDisproportionalityAlt;
1995
- exports.estLocalUnearnedSeats = estLocalUnearnedSeats;
1996
- exports.rangeUnearnedSeats = rangeUnearnedSeats;
1997
1990
  const U = __importStar(__webpack_require__(/*! ../utils/all */ "./lib/utils/all.ts"));
1998
1991
  const C = __importStar(__webpack_require__(/*! ../rate/dra-config */ "./lib/rate/dra-config.ts"));
1999
1992
  const method_1 = __webpack_require__(/*! ./method */ "./lib/partisan/method.ts");
@@ -2353,111 +2346,123 @@ function calcGamma(Vf, Sf, r) {
2353
2346
  const g = 0.5 + (r * (Vf - 0.5)) - Sf;
2354
2347
  return g;
2355
2348
  }
2356
- // EXPERIMENTAL
2357
- const pctWidth = 5; // The size in % points of the 'local' window to bracket <V>
2358
- // Average local asymmetry
2359
- function estLocalAsymmetry(Vf, dSVpoints, rSVpoints) {
2360
- try {
2361
- const dPts = svPointRange(Vf, dSVpoints);
2362
- const rPts = svPointRange(Vf, rSVpoints);
2363
- if (!dPts || !rPts)
2364
- return undefined;
2365
- const lSym = rangeAsymmetry(dPts, rPts);
2366
- return lSym;
2367
- }
2368
- catch (err) {
2369
- console.log("Exception in estLocalAsymmetry ...");
2370
- return undefined;
2371
- }
2372
- }
2373
- function rangeAsymmetry(dSVpoints, rSVpoints) {
2374
- const ndPts = dSVpoints.length;
2375
- const nrPts = rSVpoints.length;
2376
- console.assert(ndPts == nrPts, "# of D & R points don't match: ", ndPts, nrPts);
2377
- let tot = 0.0;
2378
- for (let i in dSVpoints) {
2379
- tot += calcGeometricSeatsBias(dSVpoints[i].s, rSVpoints[i].s);
2380
- }
2381
- return tot / ndPts;
2382
- }
2383
- // Average local disproportionality
2384
- function estLocalDisproportionality(Vf, dSVpoints) {
2385
- try {
2386
- const dPts = svPointRange(Vf, dSVpoints);
2387
- if (!dPts)
2388
- return undefined;
2389
- const lProp = rangeDisproportionality(dPts);
2390
- return lProp;
2391
- }
2392
- catch (err) {
2393
- console.log("Exception in estLocalDisproportionality ...");
2394
- return undefined;
2395
- }
2396
- }
2397
- function rangeDisproportionality(dSVpoints) {
2398
- const ndPts = dSVpoints.length;
2399
- let tot = 0.0;
2400
- for (let i in dSVpoints) {
2401
- tot += calcProp(dSVpoints[i].v, dSVpoints[i].s);
2402
- }
2403
- return tot / ndPts;
2404
- }
2405
- // Average local disproportionality from the best # of seats (closest to proportional)
2406
- function estLocalDisproportionalityAlt(Vf, N, dSVpoints) {
2407
- const dPts = svPointRange(Vf, dSVpoints);
2408
- if (!dPts)
2409
- return undefined;
2410
- const lPropAlt = rangeDisproportionalityAlt(N, dPts);
2411
- return lPropAlt;
2412
- }
2413
- // Dynamically calculate the best # seats, so this is a step function
2414
- function rangeDisproportionalityAlt(N, dSVpoints) {
2415
- const ndPts = dSVpoints.length;
2416
- let tot = 0.0;
2417
- for (let i in dSVpoints) {
2418
- const bestS = bestSeats(N, dSVpoints[i].v);
2419
- const bestSf = bestSeatShare(bestS, N);
2420
- tot += calcDisproportionalityFromBest(dSVpoints[i].s, bestSf);
2421
- }
2422
- return tot / ndPts;
2423
- }
2424
- // Average local unearned seats from the best # of seats (closest to proportional)
2425
- function estLocalUnearnedSeats(Vf, N, dSVpoints) {
2426
- const dPts = svPointRange(Vf, dSVpoints);
2427
- if (!dPts)
2428
- return undefined;
2429
- const lUE = rangeUnearnedSeats(N, dPts);
2430
- return lUE;
2431
- }
2432
- function rangeUnearnedSeats(N, dSVpoints) {
2433
- const ndPts = dSVpoints.length;
2434
- let tot = 0.0;
2435
- for (let i in dSVpoints) {
2436
- const estS = dSVpoints[i].s * N;
2437
- const bestS = bestSeats(N, dSVpoints[i].v);
2438
- tot += estUnearnedSeats(bestS, estS);
2439
- }
2440
- return tot / ndPts;
2441
- }
2442
- // Filter the full [0.25–0.75] range of S–V points down to the 'local' range.
2443
- // Make sure that range is w/in the full range.
2444
- function svPointRange(Vf, svPoints) {
2445
- const svRange = [0.25, 0.75]; // The range over which we infer the S–V curve points
2446
- const halfStep = (1 / 100) / 2; // The V point increments, i.e., every half a percent
2447
- const localWindow = pctWidth / 100; // # of % points wide as a fraction
2448
- const plusMinus = localWindow / 2; // +/– % as a fraction
2449
- const delta = plusMinus + (halfStep / 2); // +/– % plus half a half step to deal w/ floating point precision
2450
- if ((Vf < svRange[0]) || (Vf > svRange[1]))
2451
- return undefined;
2452
- const bracketingVs = [
2453
- (0, method_1.findBracketingLowerVf)(Vf, svPoints).v,
2454
- (0, method_1.findBracketingUpperVf)(Vf, svPoints).v
2455
- ];
2456
- const localRange = [bracketingVs[0] - delta, bracketingVs[1] + delta];
2457
- const subsetPts = svPoints.filter(x => inRange(x, localRange));
2458
- return subsetPts;
2459
- }
2460
- const inRange = (pt, range) => { return (pt.v >= range[0]) && (pt.v <= range[1]); };
2349
+ // // EXPERIMENTAL
2350
+ // const pctWidth = 5; // The size in % points of the 'local' window to bracket <V>
2351
+ // // Average local asymmetry
2352
+ // export function estLocalAsymmetry(Vf: number, dSVpoints: T.SVpoint[], rSVpoints: T.SVpoint[]): number | undefined
2353
+ // {
2354
+ // try
2355
+ // {
2356
+ // const dPts = svPointRange(Vf, dSVpoints);
2357
+ // const rPts = svPointRange(Vf, rSVpoints);
2358
+ // if (!dPts || !rPts) return undefined;
2359
+ // const lSym: number = rangeAsymmetry(dPts, rPts);
2360
+ // return lSym;
2361
+ // }
2362
+ // catch (err)
2363
+ // {
2364
+ // console.log("Exception in estLocalAsymmetry ...");
2365
+ // return undefined;
2366
+ // }
2367
+ // }
2368
+ // export function rangeAsymmetry(dSVpoints: T.SVpoint[], rSVpoints: T.SVpoint[]): number
2369
+ // {
2370
+ // const ndPts: number = dSVpoints.length;
2371
+ // const nrPts: number = rSVpoints.length;
2372
+ // console.assert(ndPts == nrPts, "# of D & R points don't match: ", ndPts, nrPts);
2373
+ // let tot: number = 0.0;
2374
+ // for (let i in dSVpoints)
2375
+ // {
2376
+ // tot += calcGeometricSeatsBias(dSVpoints[i].s, rSVpoints[i].s);
2377
+ // }
2378
+ // return tot / ndPts;
2379
+ // }
2380
+ // // Average local disproportionality
2381
+ // export function estLocalDisproportionality(Vf: number, dSVpoints: T.SVpoint[]): number | undefined
2382
+ // {
2383
+ // try
2384
+ // {
2385
+ // const dPts = svPointRange(Vf, dSVpoints);
2386
+ // if (!dPts) return undefined;
2387
+ // const lProp: number = rangeDisproportionality(dPts);
2388
+ // return lProp;
2389
+ // }
2390
+ // catch (err)
2391
+ // {
2392
+ // console.log("Exception in estLocalDisproportionality ...");
2393
+ // return undefined;
2394
+ // }
2395
+ // }
2396
+ // export function rangeDisproportionality(dSVpoints: T.SVpoint[]): number
2397
+ // {
2398
+ // const ndPts: number = dSVpoints.length;
2399
+ // let tot: number = 0.0;
2400
+ // for (let i in dSVpoints)
2401
+ // {
2402
+ // tot += calcProp(dSVpoints[i].v, dSVpoints[i].s);
2403
+ // }
2404
+ // return tot / ndPts;
2405
+ // }
2406
+ // // Average local disproportionality from the best # of seats (closest to proportional)
2407
+ // export function estLocalDisproportionalityAlt(Vf: number, N: number, dSVpoints: T.SVpoint[]): number | undefined
2408
+ // {
2409
+ // const dPts = svPointRange(Vf, dSVpoints);
2410
+ // if (!dPts) return undefined;
2411
+ // const lPropAlt: number = rangeDisproportionalityAlt(N, dPts);
2412
+ // return lPropAlt;
2413
+ // }
2414
+ // // Dynamically calculate the best # seats, so this is a step function
2415
+ // export function rangeDisproportionalityAlt(N: number, dSVpoints: T.SVpoint[]): number
2416
+ // {
2417
+ // const ndPts: number = dSVpoints.length;
2418
+ // let tot: number = 0.0;
2419
+ // for (let i in dSVpoints)
2420
+ // {
2421
+ // const bestS = bestSeats(N, dSVpoints[i].v);
2422
+ // const bestSf = bestSeatShare(bestS, N);
2423
+ // tot += calcDisproportionalityFromBest(dSVpoints[i].s, bestSf);
2424
+ // }
2425
+ // return tot / ndPts;
2426
+ // }
2427
+ // // Average local unearned seats from the best # of seats (closest to proportional)
2428
+ // export function estLocalUnearnedSeats(Vf: number, N: number, dSVpoints: T.SVpoint[]): number | undefined
2429
+ // {
2430
+ // const dPts = svPointRange(Vf, dSVpoints);
2431
+ // if (!dPts) return undefined;
2432
+ // const lUE: number = rangeUnearnedSeats(N, dPts);
2433
+ // return lUE;
2434
+ // }
2435
+ // export function rangeUnearnedSeats(N: number, dSVpoints: T.SVpoint[]): number
2436
+ // {
2437
+ // const ndPts: number = dSVpoints.length;
2438
+ // let tot: number = 0.0;
2439
+ // for (let i in dSVpoints)
2440
+ // {
2441
+ // const estS: number = dSVpoints[i].s * N;
2442
+ // const bestS = bestSeats(N, dSVpoints[i].v);
2443
+ // tot += estUnearnedSeats(bestS, estS);
2444
+ // }
2445
+ // return tot / ndPts;
2446
+ // }
2447
+ // // Filter the full [0.25–0.75] range of S–V points down to the 'local' range.
2448
+ // // Make sure that range is w/in the full range.
2449
+ // function svPointRange(Vf: number, svPoints: T.SVpoint[]): T.SVpoint[] | undefined
2450
+ // {
2451
+ // const svRange: number[] = [0.25, 0.75]; // The range over which we infer the S–V curve points
2452
+ // const halfStep: number = (1 / 100) / 2; // The V point increments, i.e., every half a percent
2453
+ // const localWindow: number = pctWidth / 100; // # of % points wide as a fraction
2454
+ // const plusMinus: number = localWindow / 2; // +/– % as a fraction
2455
+ // const delta: number = plusMinus + (halfStep / 2); // +/– % plus half a half step to deal w/ floating point precision
2456
+ // if ((Vf < svRange[0]) || (Vf > svRange[1])) return undefined;
2457
+ // const bracketingVs: number[] = [
2458
+ // findBracketingLowerVf(Vf, svPoints).v,
2459
+ // findBracketingUpperVf(Vf, svPoints).v
2460
+ // ];
2461
+ // const localRange: number[] = [bracketingVs[0] - delta, bracketingVs[1] + delta];
2462
+ // const subsetPts: T.SVpoint[] = svPoints.filter(x => inRange(x, localRange));
2463
+ // return subsetPts;
2464
+ // }
2465
+ // const inRange = (pt: T.SVpoint, range: number[]): boolean => {return (pt.v >= range[0]) && (pt.v <= range[1]);}
2461
2466
 
2462
2467
 
2463
2468
  /***/ }),