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

utility-function

v1.0.2

Published

Quick and easy calculation of the utility function.

Downloads

7

Readme

utility-function

What It Do Be

After installing the package in the normal way, all you need to do is require the module into a variable:

`const utility = require('utility-function')

If you're writing your code in Typescript, the following should work:

import { UtilityFunction } from 'utility-function/src/utility-function'

After that, all you need to think about are utility.value and utility.add().

What It Do Does - Example The First

Say you're made the terrible decision of playing the lottery. A ticket is $5, and the odds of hitting the jackpot - for a $15,000,000 pay day - is exactly 0.00000015%. There are other, more likely, wins to consider as well, such as the 0.00025% chance of winning $100,000, the 0.001% chance of winning $1500, the 0.01% chance of winning $150 and the fairly decent 0.1% chance of winning $15.

With this in mind, you have to ask yourself what the expected utility of the lottery is. Enter utility-function.

const utility = require('utility-function')
utility.add(100, -5) // Because there's 100% chance of paying 5 dollars.
utility.add(0.00000015, 15000000)
utility.add(0.00025, 100000)
utility.add(0.001, 1500)
utility.add(0.01, 150)
utility.add(0.1, 15)

Once that's done, check the contents of utility.value, and you'll find that it's about -0.7804166666666669. Yup, that's right. Even buying a million tickets and winning on the approrpriate percentage of them would still net you an average loss of almost a dollar per ticket.

What It Do Does - Example The Second

You've got a weird tropical disease called Terriblylethaliosis. It's weird in the way that it doesn't give you any symptoms up until one day, you just spontaneously combust. The Doctors have analyzed the way the disease progresses, and they've given you the following data:

  • There's a 10% chance you'll die within a year.
  • There's a 40% chance you'll die within two years.
  • There's a 80% chance you'll die within three years.
  • There's a 100% chance you'll die within four years.

Enter utility function!

utility.add(10, 1)
utility.add(40, 2)
utility.add(80, 3)
utility.add(100,4)

According to the utility function, you can expect to die about 1.825 years. Fortunately, the doctors have an ingenious treatment for you, but they're weirdly sadistic and introduce it as a math problem. According to their best minds, the treatment has a 35% of killing you outright, a 25% chance of curing you from the disease, a 10% chance of doubling your current life expectancy and a 30% chance of doing nothing.

Let's think about this in terms of preference. Dying is bad, so we think that's desirable 0% of the time. Being cured, however, is something we want 100% of the time. Having our lifespan doubled is good, so let's give that a score of 75% "good". Finally, for the medicine to do nothing is neutral, so we'll assign that a score of 50%.

utility.add(35, 0)
utility.add(25, 100)
utility.add(10, 75)
utility.add(30, 50)

Adding up the odds of the likely results gets us 47.5, on the wrong side of neutral.

What It Do Does - Example The Third

You're at a restaurant, and you're famished. Unfortunately, it's one of those new hipster restaurants with a crazy theme - it's built in the old town morgue. Because of that, every item of food has two numbers associated to it - the cost of the itme and the percentage likelihood of getting food poisoning. Luckily, they have both vegan and gluten-free options for everything, so it's not all bad! Here's what you ended up getting:

  • Grass-fed Soy Cream Substitute Sprinkle: 15% chance.
  • Non-Dairy Pickle Sunday: 10% chance.
  • TwinkleTwinkle Non-Sectarian Fish and Chips: 11% chance.
  • Powdered Serotonin Inhibitor Smoothie: 19% chance.
  • Two Rhubarb Jelly Shots with Caffeine and L-Carnitine: 9% each.

You do the math, and it turns out you've got about a 73% chance of leaving that restaurant with the finest hipster dining fighting to evacuate your bowels. But hey, that means a 27% chance you'll be all right, right? Right.