Novelty Polygonal Mathematics

    @nrk/valg-valgomat-algoritme

    2.0.0 • Public • Published

    Valgomat algorithm

    Calculate the distance betwen 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

    Install

    npm i @nrk/valg-valgomat-algoritme

    DownloadsWeekly Downloads

    64

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    27 kB

    Total Files

    14

    Last publish

    Collaborators

    • dervodev
    • grimbur
    • gardkroyer
    • kariaan
    • edplayz
    • elias-chairi
    • miatollaksvik
    • ytterbo
    • glennwedin
    • machineboycom
    • trulsl
    • syndrol
    • henit
    • mslhm
    • cbjerkan
    • hermangudesen
    • andreeldareide
    • henningkoller
    • oleob
    • meisen
    • espenhalstensen
    • hakonknutzen2
    • danjohnrk
    • olapeter
    • teodor-elstad
    • michaelpande
    • lysebraate
    • lorecaster
    • wwalmnes
    • nrk-ps-teamcity
    • vincent.andersson
    • swla
    • nrk-midas-jenkins
    • andorpandor
    • jarlelin
    • aevare
    • nrkrichard
    • gesi
    • gundelsby-nrk
    • jonstalecarlsen
    • vagifabilov
    • nrk-sofie-ci
    • nytamin
    • jesperstarkar
    • loftum
    • emte123
    • skjalgepalg
    • eirikhalvard
    • astokke
    • n640071
    • n07073
    • henrik-mattsson
    • eirikbacker
    • haavardm
    • yr
    • nrk-kurator-jenkins
    • toshb
    • torgeilo
    • nrk-user-sync
    • dhdeploy
    • janerikbr
    • espenwa
    • rogerhmar
    • ovstetun
    • eivind
    • stianlj
    • haraldkj
    • mariusu
    • spesialsnorre
    • cristobal
    • knuthaug
    • thohalv
    • johnarne
    • eshaswini
    • morrow
    • oyvindeh
    • laat
    • toggu
    • nrk-jenkins
    • plomma
    • evjand
    • moltubakk
    • ingridguren
    • lu-lux
    • andersli
    • silje
    • stiandg
    • sjurlur
    • anderscan
    • andipodnrk
    • pkej
    • yosrimti
    • zenangst
    • morten.nyhaug
    • ingvildcath
    • erlend.jones
    • brneirik
    • mollerse
    • eriksalhus
    • frdrks
    • tbnrk
    • nordanke
    • balte
    • mikaelrss
    • simonmitternacht
    • christiankarlsson9
    • martintorgersen
    • rebchr
    • steipal
    • discobus
    • ingvesund
    • martingundersen
    • tinkajts
    • hallvardlid
    • tomivar
    • ajaco
    • tobinus
    • mortenok
    • nrk-ark-deploy
    • jeangilbertlouis
    • heidimork
    • ingriddraagen
    • fridajalborg
    • bruusi
    • rosvoll
    • christianeide
    • enordby
    • artzag
    • glen_imrie
    • mia.aasbakken
    • elathamna
    • evjjan17
    • olatoft
    • kongsrud
    • chrpeter
    • ingvildforseth
    • esseb
    • talepre
    • haraldk76
    • stigok
    • dagfinno
    • johannesodland
    • anders993
    • vildefj
    • vildepk
    • andreakn
    • rolerboler
    • meloygutt
    • anders.baggethun