Fast and correct computations with uncertain values.

Related tags

Science uncertain
Overview

Uncertain

crates.io Released API docs CI MIT licensed

Fast and correct computations with uncertain values.

When working with values which are not exactly determined, such as sensor data, it can be difficult to handle uncertainties correctly.

The Uncertain trait makes such computations as natural as regular computations:

use uncertain::{Uncertain, Distribution};
use rand_distr::Normal;

// Some inputs about which we are not sure
let x = Distribution::from(Normal::new(5.0, 2.0).unwrap());
let y = Distribution::from(Normal::new(7.0, 3.0).unwrap());

// Do some computations
let distance = x.sub(y).map(|diff: f64| diff.abs());

// Ask a question about the result
let is_it_far = distance.map(|dist| dist > 2.0);

// Check how certain the answer is
assert_eq!(is_it_far.pr(0.9), false);
assert_eq!(is_it_far.pr(0.5), true);

This works by sampling a Bayesian network which is implicitly created by describing the computation on the uncertain type. The Uncertain trait only permits tests for simple boolean hypotheses. This is by design: using Wald's sequential probability ratio test, evaluation typically takes less than 100 samples.

Stability

While this crate is released as version 0.x, breaking API changes should be expected.

References

The Uncertain trait exported from the library is an implementation of the paper Uncertain.

Comments
  • Add CachedUncertain, #[no_std] compatible version of BoxedUncertain

    Add CachedUncertain, #[no_std] compatible version of BoxedUncertain

    The Rc in BoxedUncertain is unnecessary as Cell can be mutated behind an immutable reference. CachedUncertain provides the same functionality as BoxedUncertain by implementing Uncertain on its references.

    enhancement 
    opened by ivfranco 3
  • Implement expected values

    Implement expected values

    Closes #6

    Todo:

    • [x] Add missing docs
    • [x] Introspection of convergence errors
    • [x] Consider trait bounds to compute expectation: Currently it only works for f32/ f64 values (forcing the computation to use f64 precision). It would probably be better to use something like https://crates.io/crates/num-traits
    • [x] Add integration tests
    opened by dyedgreen 1
  • Computation of expected values

    Computation of expected values

    It might be interesting to offer evaluation of expectations for uncertain values which are floating point values.

    There are a few questions around how to best do that:

    • Should sample count be fixed?
    • Should the variance be reported?
    • Fixed sample count or convergence measure to stop sampling early?
    • Restrict to Float, or also allow integers(?), or maybe go completely different route with Into<f64>
    • Can we make the evaluation cheap enough such that it can be used e.g. in async contexts without worrying about blocking (like you can pr)?
    • Built in multi threading? (Probably not, most Uncertain values are Send, but they are not sync, also I think it's better to give control over threading to user: can send value to other thread if they want to)

    Proposed API

    fn get_value() -> impl Uncertain<Value = f64>;
    let x = get_value();
    let expected_value = x.expectation(); // should ideally be cheap to run (!)
    // or maybe
    let expected_value = x.expect();
    let expected_value = x.e(); // short like `pr`? probably not -> it's more expensive, so should not be easy to miss the call
    
    enhancement question 
    opened by dyedgreen 1
  • Allow boxing uncertain as a trait object

    Allow boxing uncertain as a trait object

    Currently contemplating what design would be best.

    The choices seem to be:

    1. Have sample be generic over RNG, and somehow force a specific generator when sampling through BoxedUncertain
    2. Remove the generic RNG support
    enhancement question 
    opened by dyedgreen 0
  • Consider sealing the trait

    Consider sealing the trait

    The contract for epoch caching is confusing, and most values that are e.g. returned from sensors should probably implement Clone.

    Implementing Distribution is easier, more compatible with other crates, and allows us to guarantee that computations are always correct, even if foreign types are used.

    opened by dyedgreen 0
  • Consider type erasure to bring back generic Rng

    Consider type erasure to bring back generic Rng

    Blog post on the technique: https://www.possiblerust.com/pattern/3-things-to-try-when-you-can-t-make-a-trait-object

    pros:

    • get back the pr_with method

    cons:

    • need to do more dynamic dispatch which is potential expensive
    help wanted question 
    opened by dyedgreen 0
  • Add `#![no_std]` option?

    Add `#![no_std]` option?

    Not sure how possible this is, but a #![no_std] option seems potentially useful. Could allow you to make more accurate sensor readings in IoT networks by taking into account local variances to decide measurement tolerances (like reading pressure or temp to determine the accuracy of other readings the device could do).

    That way your receiver that processes off the info doesn't have to do an educated guess on the tolerances per device (because you might have different sensors with different tolerances), it'd instead be encoded in the data stream for it to parse.

    enhancement 
    opened by sparky8251 2
Owner
Tilman Roeder
Physics, Maths and Programming. I study computer science at Imperial College London. Languages I like include JavaScript, C, Rust, Go, Python, and Ruby.
Tilman Roeder
ANISE provides an open-source and open-governed library and algorithmic specification for most computations for astrodynamics

ANISE provides an open-source and open-governed library and algorithmic specification for most computations for astrodynamics. It is heavily inspired by NAIF SPICE, and may be considered as an open-source modern rewrite of SPICE.

ANISE 4 Mar 9, 2022
A STARK prover and verifier for arbitrary computations.

A STARK is a novel proof-of-computation scheme to create efficiently verifiable proofs of the correct execution of a computation. The scheme was developed by Eli Ben-Sasson, Michael Riabzev et al. at Technion - Israel Institute of Technology. STARKs do not require an initial trusted setup, and rely on very few cryptographic assumptions. See references for more info.

Novi 512 Jan 3, 2023
A library for advanced finite element computations in Rust

A Rust library for building advanced applications with the Finite Element Method (FEM). Although developed with a special emphasis on solid mechanics

Interactive Computer Graphics 55 Dec 26, 2022
zenoh-flow aims at providing a zenoh-based data-flow programming framework for computations that span from the cloud to the device.

Eclipse Zenoh-Flow Zenoh-Flow provides a zenoh-based dataflow programming framework for computations that span from the cloud to the device. ⚠️ This s

null 35 Dec 12, 2022
This crate is an implementation of Sonic, a protocol for quickly verifiable, compact zero-knowledge proofs of arbitrary computations

Sonic This crate is an implementation of Sonic, a protocol for quickly verifiable, compact zero-knowledge proofs of arbitrary computations. Sonic is i

null 75 Jul 4, 2022
Diagnostic tools for timely dataflow computations

Timely Diagnostics Diagnostic tools for timely dataflow computations. Timely dataflows are data-parallel and scale from single threaded execution on y

Timely Dataflow 40 Aug 24, 2022
A repo for learning how to parallelize computations in the GPU using Apple's Metal, in Rust.

Metal playground in rust Made for learning how to parallelize computations in the GPU using Apple's Metal, in Rust, via the metal crate. Overview The

Lambdaclass 5 Feb 20, 2023
A CLI tool for CIs and build scripts, making file system based caching easy and correct (locking, eviction, etc.)

FS Dir Cache A CLI tool for CIs and build scripts, making file system based caching easy and correct (locking, eviction, etc.) When working on build s

Dawid Ciężarkiewicz 5 Aug 29, 2023
Typed, correct GraphQL requests and responses in Rust

graphql_client A typed GraphQL client library for Rust. Features Precise types for query variables and responses. Supports GraphQL fragments, objects,

GraphQL Rust 914 Dec 27, 2022
Try to find the correct word with only first letter and unknown letter count

Try to find the correct word with only first letter and unknown letter count

Alexandre 6 Apr 11, 2022
try to find the correct word with only first letter and unknown letter count.

MOTUS Current dictionaries are provided in french and can contain some words not included in the official Motus dictionary. Additionally, dictionaries

Alexandre 6 Apr 11, 2022
languagetool-code-comments integrates the LanguageTool API to parse, spell check, and correct the grammar of your code comments!

languagetool-code-comments integrates the LanguageTool API to parse, spell check, and correct the grammar of your code comments! Overview Install MacO

Dustin Blackman 17 Dec 25, 2022
Drpc-Correct, high performance, robust, easy use Remote invocation framework

Drpc - Correct, high performance, robust, easy use Remote invocation framework

darkrpc 30 Dec 17, 2022
NixEl is a Rust library that turns Nix code into a variety of correct, typed, memory-safe data-structures

?? NixEL Lexer, Parser, Abstract Syntax Tree and Concrete Syntax Tree for the Nix Expressions Language. NixEl is a Rust library that turns Nix code in

Kevin Amado 56 Dec 29, 2022
tidy-builder is a builder generator that is compile-time correct.

The Builder derive macro creates a compile-time correct builder which means that it only allows you to build the given struct if and only if you provi

M.Amin Rayej 7 Dec 18, 2022
LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values.

LevelDB is a fast key-value storage library written at Google that provides an ordered mapping from string keys to string values. Authors: Sanjay Ghem

Google 31.5k Jan 1, 2023
A fast bump allocator that supports allocation scopes / checkpoints. Aka an arena for values of arbitrary types.

bump-scope A fast bump allocator that supports allocation scopes / checkpoints. Aka an arena for values of arbitrary types. What is bump allocation? A

null 7 May 4, 2024
🍅 A command-line tool to get and set values in toml files while preserving comments and formatting

tomato Get, set, and delete values in TOML files while preserving comments and formatting. That's it. That's the feature set. I wrote tomato to satisf

C J Silverio 15 Dec 23, 2022
Tiny-lsm - super simple in-memory blocking LSM for constant-size keys and values

tiny-lsm Super simple in-memory blocking LSM for constant-size keys and values. Despite being single-threaded and blocking, this is still capable of o

Tyler Neely 50 Oct 15, 2022
A hashmap implementation, which uses hashset, and keys are contained within values.

A hashmap implementation, which uses hashset, and keys are contained within values.

Piotr Mikulski 2 Nov 29, 2022