npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

election-compass-match

v1.0.0

Published

The algorithm used by https://valkompassen.svt.se to get a percentage of how well two entities' political opinions are aligned.

Downloads

4

Readme

Election ”Compass“ Match

The algorithm used by SVT's Valkompass to get a percentage of how well two entities' political opinions are aligned.

Overview

SVT's Valkompass works like this:

  1. Political candidates and political parties answer a set of questions developed by independent political analysts, with the express intent to find the most contentious and divisive issues, such that the answers given are as diverse as possible.
  2. Individuals answer the same set of questions.
  3. To each candidate/party, the individual's answers are compared, yielding a percentage of how similar the answers were.
  4. The percentages are revealed to the user, giving them an indication to which parties or candidates most closely reflect their political view.

This library fits in at step 3 :point_up:


The matching has a direction. It's not commutative. We call the individual me, and the political entity you. Of course, the algorithm can be run the other way around, but it will answer a different question.

It works like this:

  1. For each answer pair (me, you),
    • Let maxScore be the potential of a strong political consesus. This should correspond to a distinct statement by me.
    • Let score be a value corresponding to the closeness of the two answers.
  2. Sum all the maxScore and score values for both answer sets.
  3. Divide score with maxScore to get the fractional match result.

Types of Answers

To make the compass more interesting, it includes different types of questions, and therefore different kinds of answers. The match algorithm is built to support adding more types of questions/answers, as long as they conform to the procedure described above.

Here's what's available right now:

| Name | Description | Encoded format | | :------------------ | :---------------------------------------------------------------------------------- | :------------------------------------------------------------------ | | PropositionAnswer | A reaction to a political proposition given in a four-level Likert scale. | Characters A (strongly disagree) through D (strongly agree) | | PriorityAnswer | A multi-choice answer to a question of prioritizing political issues. | An array of indices in JSON-like format (e.g. [0,4]) | | RangeAnswer | A single choice from N alternatives. | The index of the choice and the number of alternatives (e.g. 0/5) |

Note: Currently, the RangeAnswer only supports answers to questions with exactly five alternatives.

Each answer can be marked as important to the respondent, making the match in that question more impactful to the overall match result (when marked by me). This is encoded by suffixing with an exclamation point (!).

The encoded format for multiple answers is separating the answers with semicolons (;).

Skipped Questions

Any answer can also be replaced with a "skipped" answer, both by me and you, impacting the match result in the following way:

  • If me has skipped a question, it has no impact at all. The question is simply removed from the calculation.
  • If you has skipped a question, it penalizes you by adding onto the maxScore but scoring 0.

A skipped-question answer is encoded as an underscore (_).

Usage

The answers can be parsed from their canonical encoding format, or constructed manually:

import {
  PropositionAnswer,
  PriorityAnswer,
  RangeAnswer,
  skippedAnswer,
  parseAnswers,
  match
} from 'election-compass-match';

const me = parseAnswers('D!;[0,3];_;0/5;B');
const you = [
  new PropositionAnswer('A', { isImportant: false }),
  new PriorityAnswer([1, 2], { isImportant: false }),
  new PropositionAnswer('B', { isImportant: true }),
  new RangeAnswer(0, 5, { isImportant: false }),
  skippedAnswer
];

const fractionalMatch = match(me, you); // 0.16753926701570682

Difference from 2018

Besides the newly added RangeAnswer type, a slight adjustment has also been made to how strong of a penalty the you side gets if it hasn't answered a question. In the 2018 algorithm, the penalty was a 5 unit increase in the maxScore, yielding a small decrease in the resulting quotient after the score / maxScore operation. In 2019 the penalty is 2.5 instead, which makes the penalty smaller.