Skip to content
The algorithm used to calculate distance between sets of positions for NRKs Valgomat
Branch: master
Clone or download
DonnyCraft1 and mollerse Fix typo 'betwen'
Jeg kommer hit fra NRKbeta-artikkelen, som forresten var ganske interessant. Jeg fant en typo i README.md. Det er kanskje litt mye å forke hele repoet bare for én bokstav, men nå er det gjort 😀

***

*I found a typo in README.md, here is a pull request just for ONE letter. I guess I could just submit an issue, but it's too late now..*
Latest commit b654d05 Jul 5, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Remove undocumented distanceMix function Jul 3, 2019
.eslintrc.js
.gitignore
.npmrc
LICENSE
README.md
package.json
tools.js
tools.md
validation.js
validation.md

README.md

Valgomat algorithm

Calculate the distance between two sets of positions.

Calculates the distance between two sets of positions. Mostly used to calculate the distance between various party positions and a voter position.

Usage

The simple case is comparing to sets of positions.

import { distance } from "@nrk/valg-valgomat-algoritme";

let partyPositions = {
  "1": { value: 1 },
  "2": { value: -1 }
};

let voterPositions = {
  "1": { value: -2 },
  "2": { value: 2 }
};

let d = distance(partyPositions, voterPositions); // => 0.25

The module also comes with a function to compare many positions to one position.

import { distanceMap } from "@nrk/valg-valgomat-algoritme";

let partyAPositions = {
  "1": { value: 1 },
  "2": { value: -1 }
};

let partyBPositions = {
  "1": { value: 2 },
  "2": { value: -2 }
};

let partyPostitions = {
  partyA: partyAPositions,
  partyB: partyBPositions
};

let voterPositions = {
  "1": { value: 0 },
  "2": { value: -2 }
};

let optionalWeights = {
  partyA: 1.0,
  partyB: 1.0
};

let distances = distanceMap(voterPositions, partyPositions, optionalWeights); // => { "partyA": 0.75, "partyB": 1.0 }

API

import { distance, distanceMap } from "@nrk/valg-valgomat-algoritme";

let d = distance(positionsA, positionsB);

Accepts two sets of positions and returns the distance between them.

Positions are given in the form of sets:

{
  [statement: string]: { value: number }
}

Where value is in the interval [-2.0, 2.0]

Output will be a number in the interval [0.0, 1.0] where 0.0 is the largest possible distance and 1.0 is the smallest possible distance (iow. identical).

If a value is close to 0 (currently defined as +/-0.3), it will be handled as a special case and given twice the distance to a value that is not close to 0. I.e. the distance between 0.1 and 1.0 will become 1.8.

This is done to more accuratly represent the situation where a party, voter or model party is taking a position close to 0. Such a position is considered as a completely unknown position, it could go either way when one is forced to consider it. Comparing moderate positions (-1/1) to this unknown position would be unreasonably favorable if we hadn't implemented this mechanism.

let distances = distanceMap(positionsA, positionsMap, optionalWeights = {});

Accepts a set of positions and a map of many sets of positions and returns a map of the distances between a position in the position map and the first position. Optionally provide weights for keys in positionsMap, will multiply distance with weight for that key.

This is useful if you want to calculate the distance between one set of positions and many sets of positions. For instance between all parties and a single voter.

Output will be a map from the keys in the positionsMap and the distance to the given position.

Extra

See validation or tools for documentation on some of the extra features of this pacakge.

Installation

npm install @nrk/valg-valgomat-algoritme

Glossary

  • Position = Standpunkt
  • Statement = Påstand
  • Party = Parti
  • Voter = Velger

A note on codestyle

This module uses default configuration of Prettier.

This module uses CommonJS/Node-style require for broadest possible ecosystem-compatbility without additional compile-steps.

This module uses a fairly modern flavor of JavaScript and will require compilation if it is to be used with older runtimes.

See also

You can’t perform that action at this time.