| 89 | * @throws {InvalidOrder} If the value is missing or out of range. |
| 90 | */ |
| 91 | export function validateProbability(price: number | undefined): number { |
| 92 | if (price === undefined || price === null) { |
| 93 | throw new InvalidOrder( |
| 94 | "Metaculus createOrder requires `price` (the probability to forecast, between 0 and 1 exclusive).", |
| 95 | "Metaculus", |
| 96 | ); |
| 97 | } |
| 98 | if (typeof price !== "number" || isNaN(price)) { |
| 99 | throw new InvalidOrder( |
| 100 | `Invalid price "${price}": must be a number between 0 and 1 exclusive.`, |
| 101 | "Metaculus", |
| 102 | ); |
| 103 | } |
| 104 | if (price <= 0 || price >= 1) { |
| 105 | throw new InvalidOrder( |
| 106 | `Probability ${price} is out of range. Metaculus requires a value strictly between 0 and 1 (e.g., 0.01 to 0.99).`, |
| 107 | "Metaculus", |
| 108 | ); |
| 109 | } |
| 110 | return price; |
| 111 | } |
| 112 | |
| 113 | // --------------------------------------------------------------------------- |
| 114 | // Multiple-Choice Redistribution |