@nrk/valg-valgomat-algoritme

3.0.1 • Public • Published

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 we are unable to determine a distance, the output will be null. This happens if we are comparing a pair of vectors where one of the vectors are empty.

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

Dependents (0)

Package Sidebar

Install

npm i @nrk/valg-valgomat-algoritme

Weekly Downloads

5

Version

3.0.1

License

MIT

Unpacked Size

27.8 kB

Total Files

14

Last publish

Collaborators

  • hammeralf
  • janerikbr
  • thormodb
  • siivers
  • torsrex
  • haraldsk
  • eskilgh
  • ragnaroh-nrk
  • daardal
  • arevjensen
  • julusian
  • madsern
  • andrefau
  • jfjeldskaar
  • muddah
  • jensrage
  • oysteinkoppang
  • phajsi
  • jorn_georg
  • bjornhels
  • halvorh
  • morten-nrk
  • nicklassvendsrud
  • kjellvnnrk
  • sanderknrk
  • nikolaia
  • eirikjstnrk
  • carinafraning
  • helenper
  • stefanogdennrk
  • jimmeloysund
  • tobiasrp
  • martiosk
  • jimalexberger
  • gunderwonder
  • hamnis
  • luminrk
  • supermeisen
  • vagifabilov
  • claudio-nrk
  • haakemon
  • zenangst
  • rannveignc
  • eschoien
  • balte
  • toshb
  • emte123
  • opet
  • klizter
  • mikkelnygard
  • feiring
  • dervodev
  • grimbur
  • gardkroyer
  • kariaan
  • edplayz
  • elias-chairi
  • miatollaksvik
  • ytterbo
  • machineboycom
  • trulsl
  • mslhm
  • cbjerkan
  • hermangudesen
  • andreeldareide
  • henningkoller
  • espenhalstensen
  • danjohnrk
  • olapeter
  • teodor-elstad
  • lorecaster
  • nrk-ps-teamcity
  • swla
  • nrk-midas-jenkins
  • andorpandor
  • nrkrichard
  • gesi
  • gundelsby-nrk
  • jonstalecarlsen
  • nrk-sofie-ci
  • nytamin
  • jesperstarkar
  • skjalgepalg
  • eirikhalvard
  • astokke
  • n640071
  • n07073
  • henrik-mattsson
  • haavardm
  • yr
  • nrk-kurator-jenkins
  • torgeilo
  • nrk-user-sync
  • dhdeploy
  • espenwa
  • ovstetun
  • stianlj
  • haraldkj
  • mariusu
  • cristobal
  • knuthaug
  • thohalv
  • johnarne
  • eshaswini
  • morrow
  • oyvindeh
  • laat
  • toggu
  • nrk-jenkins
  • plomma
  • evjand
  • moltubakk
  • ingridguren
  • lu-lux
  • andersli
  • silje
  • stiandg
  • sjurlur
  • andipodnrk
  • pkej
  • yosrimti
  • morten.nyhaug
  • ingvildcath
  • erlend.jones
  • brneirik
  • mollerse
  • tbnrk
  • nordanke
  • simonmitternacht
  • martintorgersen
  • rebchr
  • steipal
  • discobus
  • martingundersen
  • tinkajts
  • hallvardlid
  • tomivar
  • ajaco
  • tobinus
  • mortenok
  • nrk-ark-deploy
  • jeangilbertlouis
  • heidimork
  • ingriddraagen
  • fridajalborg
  • bruusi
  • rosvoll
  • christianeide
  • enordby
  • glen_imrie
  • mia.aasbakken
  • elathamna
  • evjjan17
  • olatoft
  • kongsrud
  • chrpeter
  • ingvildforseth
  • haraldk76
  • stigok
  • johannesodland
  • anders993
  • vildefj
  • vildepk
  • rolerboler
  • meloygutt
  • anders.baggethun