-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathmain_with_no_framework.py
More file actions
executable file
·1648 lines (1371 loc) · 59.8 KB
/
Copy pathmain_with_no_framework.py
File metadata and controls
executable file
·1648 lines (1371 loc) · 59.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import asyncio
from collections import Counter
import datetime
import json
import os
import re
import sys
import dotenv
dotenv.load_dotenv()
import forecasting_tools
import numpy as np
import requests
from asknews_sdk import AskNewsSDK
from openai import AsyncOpenAI
from pydantic import BaseModel, Field, model_validator
"""
This file provides a simple forecasting bot built from the ground up.
We provide this for people who want to dissect
it to build their own bot without using forecasting-tools.
This template assumes you are using a OpenAI model and have an OpenAI API key
You will also need a Metaculus API key, for posting questions to Metaculus
and a Perplexity or AskNews API key for online research
This is not a representative of the template bots used by Metaculus, as there are some
differences in implementation. The actual template bot (e.g. like main.py) has the following differences:
- An LLM now parses the final forecast output (rather than programmatic parsing)
- Support for nominal bounds was added (i.e. when there are discrete questions and normal upper/lower bounds are not as intuitive)
- Upper/Lower bounds are mentioned as suggestions (not ignored) when the bounds are open
- Group questions, conditional questions, and date questions are supported (these types are optional and won't be launched in Summer AIB)
- The research prompt mentions resolution criteria and fine print explicitly
We realize the below code could probably be cleaned up a bit in a few places
Though we are assuming most people will dissect it enough to make this not matter much
Note that this is code is given as-is and though we have have done basic testing
with this file it may be worth double checking key components locally.
Updates pending for Spring season:
- AskNews can now use ASKNEWS_API_KEY via client api_key parameter
"""
######################### CONSTANTS #########################
# Constants
SUBMIT_PREDICTION = True # set to True to publish your predictions to Metaculus
USE_EXAMPLE_QUESTIONS = False # set to True to forecast on the bot-testing-area tournament
NUM_RUNS_PER_QUESTION = (
5 # The median forecast is taken between NUM_RUNS_PER_QUESTION runs
)
SKIP_PREVIOUSLY_FORECASTED_QUESTIONS = True
# Environment variables
# You only need *either* Exa or Perplexity or AskNews keys for online research
METACULUS_TOKEN = os.getenv("METACULUS_TOKEN")
PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY")
ASKNEWS_CLIENT_ID = os.getenv("ASKNEWS_CLIENT_ID")
ASKNEWS_SECRET = os.getenv("ASKNEWS_SECRET")
EXA_API_KEY = os.getenv("EXA_API_KEY")
OPENAI_API_KEY = os.getenv(
"OPENAI_API_KEY"
) # You'll also need the OpenAI API Key if you want to use the Exa Smart Searcher
# The tournament IDs below can be used for testing your bot.
Q4_2024_AI_BENCHMARKING_ID = 32506
Q1_2025_AI_BENCHMARKING_ID = 32627
FALL_2025_AI_BENCHMARKING_ID = "fall-aib-2025"
SPRING_2026_AI_BENCHMARKING_ID = "spring-aib-2026"
SUMMER_2026_AI_BENCHMARKING_ID = 33022 # https://www.metaculus.com/tournament/summer-futureeval-2026/
CURRENT_MINIBENCH_ID = "minibench"
Q4_2024_QUARTERLY_CUP_ID = 3672
Q1_2025_QUARTERLY_CUP_ID = 32630
CURRENT_METACULUS_CUP_ID = None # TBD (Use the slug from the Metaculus Cup URL)
AXC_2025_TOURNAMENT_ID = 32564
AI_2027_TOURNAMENT_ID = "ai-2027"
# Bot Testing Area - contains all question types and is the recommended target for test runs.
# https://www.metaculus.com/tournament/bot-testing-area/
BOT_TESTING_AREA_ID = "bot-testing-area"
TOURNAMENT_ID = SUMMER_2026_AI_BENCHMARKING_ID
######################### HELPER FUNCTIONS #########################
# @title Helper functions
AUTH_HEADERS = {"headers": {"Authorization": f"Token {METACULUS_TOKEN}"}}
API_BASE_URL = "https://www.metaculus.com/api"
def post_question_comment(post_id: int, comment_text: str) -> None:
"""
Post a comment on the question page as the bot user.
"""
response = requests.post(
f"{API_BASE_URL}/comments/create/",
json={
"text": comment_text,
"parent": None,
"included_forecast": True,
"is_private": True,
"on_post": post_id,
},
**AUTH_HEADERS, # type: ignore
)
if not response.ok:
raise RuntimeError(response.text)
def post_question_prediction(question_id: int, forecast_payload: dict) -> None:
"""
Post a forecast on a question.
"""
url = f"{API_BASE_URL}/questions/forecast/"
response = requests.post(
url,
json=[
{
"question": question_id,
"source": "api",
**forecast_payload,
},
],
**AUTH_HEADERS, # type: ignore
)
print(f"Prediction Post status code: {response.status_code}")
if not response.ok:
raise RuntimeError(response.text)
def create_forecast_payload(
forecast: float | dict[str, float] | list[float],
question_type: str,
) -> dict:
"""
Accepts a forecast and generates the api payload in the correct format.
If the question is binary, forecast must be a float.
If the question is multiple choice, forecast must be a dictionary that
maps question.options labels to floats.
If the question is numeric, forecast must be a dictionary that maps
quartiles or percentiles to datetimes, or a 201 value cdf.
"""
if question_type == "binary":
return {
"probability_yes": forecast,
"probability_yes_per_category": None,
"continuous_cdf": None,
}
if question_type == "multiple_choice":
return {
"probability_yes": None,
"probability_yes_per_category": forecast,
"continuous_cdf": None,
}
# numeric or date
return {
"probability_yes": None,
"probability_yes_per_category": None,
"continuous_cdf": forecast,
}
def list_posts_from_tournament(
tournament_id: int | str = TOURNAMENT_ID, offset: int = 0, count: int = 50
) -> list[dict]:
"""
List (all details) {count} posts from the {tournament_id}
"""
url_qparams = {
"limit": count,
"offset": offset,
"order_by": "-hotness",
"forecast_type": ",".join(
[
"binary",
"multiple_choice",
"numeric",
"discrete",
]
),
"tournaments": [tournament_id],
"statuses": "open",
"include_description": "true",
}
url = f"{API_BASE_URL}/posts/"
response = requests.get(url, **AUTH_HEADERS, params=url_qparams) # type: ignore
if not response.ok:
raise Exception(response.text)
data = json.loads(response.content)
return data
def get_open_question_ids_from_tournament(
tournament_id: int | str = TOURNAMENT_ID,
) -> list[tuple[int, int]]:
posts = list_posts_from_tournament(tournament_id)
post_dict = dict()
for post in posts["results"]:
if question := post.get("question"):
# single question post
post_dict[post["id"]] = [question]
open_question_id_post_id = [] # [(question_id, post_id)]
for post_id, questions in post_dict.items():
for question in questions:
if question.get("status") == "open":
print(
f"ID: {question['id']}\nQ: {question['title']}\nCloses: "
f"{question['scheduled_close_time']}"
)
open_question_id_post_id.append((question["id"], post_id))
return open_question_id_post_id
def get_post_details(post_id: int) -> dict:
"""
Get all details about a post from the Metaculus API.
"""
url = f"{API_BASE_URL}/posts/{post_id}/"
print(f"Getting details for {url}")
response = requests.get(
url,
**AUTH_HEADERS, # type: ignore
)
if not response.ok:
raise Exception(response.text)
details = json.loads(response.content)
return details
CONCURRENT_REQUESTS_LIMIT = 5
llm_rate_limiter = asyncio.Semaphore(CONCURRENT_REQUESTS_LIMIT)
async def call_llm(prompt: str, model: str = "gpt-4o", temperature: float = 0.3) -> str:
"""
Makes a streaming completion request to OpenAI's API with concurrent request limiting.
"""
# Remove the base_url parameter to call the OpenAI API directly
# Also checkout the package 'litellm' for one function that can call any model from any provider
# Also checkout OpenRouter for allowing one API key for many providers (especially powerful if combined with litellm)
client = AsyncOpenAI()
async with llm_rate_limiter:
response = await client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=temperature,
stream=False,
)
answer = response.choices[0].message.content
if answer is None:
raise ValueError("No answer returned from LLM")
return answer
def run_research(question: str) -> str:
research = ""
if ASKNEWS_CLIENT_ID and ASKNEWS_SECRET:
research = call_asknews(question)
elif EXA_API_KEY:
research = call_exa_smart_searcher(question)
elif PERPLEXITY_API_KEY:
research = call_perplexity(question)
else:
research = "No research done"
print(
f"########################\nResearch Found:\n{research}\n########################"
)
return research
def call_perplexity(question: str) -> str:
url = "https://api.perplexity.ai/chat/completions"
api_key = PERPLEXITY_API_KEY
headers = {
"accept": "application/json",
"authorization": f"Bearer {api_key}",
"content-type": "application/json",
}
payload = {
"model": "llama-3.1-sonar-huge-128k-online",
"messages": [
{
"role": "system", # this is a system prompt designed to guide the perplexity assistant
"content": """
You are an assistant to a superforecaster.
The superforecaster will give you a question they intend to forecast on.
To be a great assistant, you generate a concise but detailed rundown of the most relevant news, including if the question would resolve Yes or No based on current information.
You do not produce forecasts yourself.
""",
},
{
"role": "user", # this is the actual prompt we ask the perplexity assistant to answer
"content": question,
},
],
}
response = requests.post(url=url, json=payload, headers=headers)
if not response.ok:
raise Exception(response.text)
content = response.json()["choices"][0]["message"]["content"]
return content
def call_exa_smart_searcher(question: str) -> str:
if OPENAI_API_KEY is None:
searcher = forecasting_tools.ExaSearcher(
include_highlights=True,
num_results=10,
)
highlights = asyncio.run(
searcher.invoke_for_highlights_in_relevance_order(question)
)
prioritized_highlights = highlights[:10]
combined_highlights = ""
for i, highlight in enumerate(prioritized_highlights):
combined_highlights += f'[Highlight {i+1}]:\nTitle: {highlight.source.title}\nURL: {highlight.source.url}\nText: "{highlight.highlight_text}"\n\n'
response = combined_highlights
else:
searcher = forecasting_tools.SmartSearcher(
temperature=0,
num_searches_to_run=2,
num_sites_per_search=10,
)
prompt = (
"You are an assistant to a superforecaster. The superforecaster will give"
"you a question they intend to forecast on. To be a great assistant, you generate"
"a concise but detailed rundown of the most relevant news, including if the question"
"would resolve Yes or No based on current information. You do not produce forecasts yourself."
f"\n\nThe question is: {question}"
)
response = asyncio.run(searcher.invoke(prompt))
assert response is not None
return response
def call_asknews(question: str) -> str:
"""
Use the AskNews `news` endpoint to get news context for your query.
The full API reference can be found here: https://docs.asknews.app/en/reference#get-/v1/news/search
"""
ask = AskNewsSDK(
client_id=ASKNEWS_CLIENT_ID, client_secret=ASKNEWS_SECRET, scopes=set(["news"])
)
# get the latest news related to the query (within the past 48 hours)
hot_response = ask.news.search_news(
query=question, # your natural language query
n_articles=6, # control the number of articles to include in the context, originally 5
return_type="both",
strategy="latest news", # enforces looking at the latest news only
)
# get context from the "historical" database that contains a news archive going back to 2023
historical_response = ask.news.search_news(
query=question,
n_articles=10,
return_type="both",
strategy="news knowledge", # looks for relevant news within the past 60 days
)
hot_articles = hot_response.as_dicts
historical_articles = historical_response.as_dicts
formatted_articles = "Here are the relevant news articles:\n\n"
if hot_articles:
hot_articles = [article.__dict__ for article in hot_articles]
hot_articles = sorted(hot_articles, key=lambda x: x["pub_date"], reverse=True)
for article in hot_articles:
pub_date = article["pub_date"].strftime("%B %d, %Y %I:%M %p")
formatted_articles += f"**{article['eng_title']}**\n{article['summary']}\nOriginal language: {article['language']}\nPublish date: {pub_date}\nSource:[{article['source_id']}]({article['article_url']})\n\n"
if historical_articles:
historical_articles = [article.__dict__ for article in historical_articles]
historical_articles = sorted(
historical_articles, key=lambda x: x["pub_date"], reverse=True
)
for article in historical_articles:
pub_date = article["pub_date"].strftime("%B %d, %Y %I:%M %p")
formatted_articles += f"**{article['eng_title']}**\n{article['summary']}\nOriginal language: {article['language']}\nPublish date: {pub_date}\nSource:[{article['source_id']}]({article['article_url']})\n\n"
if not hot_articles and not historical_articles:
formatted_articles += "No articles were found.\n\n"
return formatted_articles
return formatted_articles
############### BINARY ###############
# @title Binary prompt & functions
# This section includes functionality for binary questions.
BINARY_PROMPT_TEMPLATE = """
You are a professional forecaster interviewing for a job.
Your interview question is:
{title}
Question background:
{background}
This question's outcome will be determined by the specific criteria below. These criteria have not yet been satisfied:
{resolution_criteria}
{fine_print}
Your research assistant says:
{summary_report}
Today is {today}.
Before answering you write:
(a) The time left until the outcome to the question is known.
(b) The status quo outcome if nothing changed.
(c) A brief description of a scenario that results in a No outcome.
(d) A brief description of a scenario that results in a Yes outcome.
You write your rationale remembering that good forecasters put extra weight on the status quo outcome since the world changes slowly most of the time.
The last thing you write is your final answer as: "Probability: ZZ%", 0-100
"""
def extract_probability_from_response_as_percentage_not_decimal(
forecast_text: str,
) -> float:
matches = re.findall(r"(\d+)%", forecast_text)
if matches:
# Return the last number found before a '%'
number = int(matches[-1])
number = min(99, max(1, number)) # clamp the number between 1 and 99
return number
else:
raise ValueError(f"Could not extract prediction from response: {forecast_text}")
async def get_binary_gpt_prediction(
question_details: dict, num_runs: int
) -> tuple[float, str]:
today = datetime.datetime.now().strftime("%Y-%m-%d")
title = question_details["title"]
resolution_criteria = question_details["resolution_criteria"]
background = question_details["description"]
fine_print = question_details["fine_print"]
question_type = question_details["type"]
summary_report = run_research(title)
content = BINARY_PROMPT_TEMPLATE.format(
title=title,
today=today,
background=background,
resolution_criteria=resolution_criteria,
fine_print=fine_print,
summary_report=summary_report,
)
async def get_rationale_and_probability(content: str) -> tuple[float, str]:
rationale = await call_llm(content)
probability = extract_probability_from_response_as_percentage_not_decimal(
rationale
)
comment = (
f"Extracted Probability: {probability}%\n\nGPT's Answer: "
f"{rationale}\n\n\n"
)
return probability, comment
probability_and_comment_pairs = await asyncio.gather(
*[get_rationale_and_probability(content) for _ in range(num_runs)]
)
comments = [pair[1] for pair in probability_and_comment_pairs]
final_comment_sections = [
f"## Rationale {i+1}\n{comment}" for i, comment in enumerate(comments)
]
probabilities = [pair[0] for pair in probability_and_comment_pairs]
median_probability = float(np.median(probabilities)) / 100
final_comment = f"Median Probability: {median_probability}\n\n" + "\n\n".join(
final_comment_sections
)
return median_probability, final_comment
####################### NUMERIC ###############
# @title Numeric prompt & functions
NUMERIC_PROMPT_TEMPLATE = """
You are a professional forecaster interviewing for a job.
Your interview question is:
{title}
Background:
{background}
{resolution_criteria}
{fine_print}
Units for answer: {units}
Your research assistant says:
{summary_report}
Today is {today}.
{lower_bound_message}
{upper_bound_message}
Formatting Instructions:
- Please notice the units requested (e.g. whether you represent a number as 1,000,000 or 1m).
- Never use scientific notation.
- Always start with a smaller number (more negative if negative) and then increase from there
Before answering you write:
(a) The time left until the outcome to the question is known.
(b) The outcome if nothing changed.
(c) The outcome if the current trend continued.
(d) The expectations of experts and markets.
(e) A brief description of an unexpected scenario that results in a low outcome.
(f) A brief description of an unexpected scenario that results in a high outcome.
You remind yourself that good forecasters are humble and set wide 90/10 confidence intervals to account for unknown unkowns.
The last thing you write is your final answer as:
"
Percentile 10: XX
Percentile 20: XX
Percentile 40: XX
Percentile 60: XX
Percentile 80: XX
Percentile 90: XX
"
"""
def extract_percentiles_from_response(forecast_text: str) -> dict:
# Helper function that returns a list of tuples with numbers for all lines with Percentile
def extract_percentile_numbers(text) -> dict:
pattern = r"^.*(?:P|p)ercentile.*$"
number_pattern = (
r"-\s*(?:[^\d\-]*\s*)?(\d+(?:,\d{3})*(?:\.\d+)?)|(\d+(?:,\d{3})*(?:\.\d+)?)"
)
results = []
for line in text.split("\n"):
if re.match(pattern, line):
numbers = re.findall(number_pattern, line)
numbers_no_commas = [
next(num for num in match if num).replace(",", "")
for match in numbers
]
numbers = [
float(num) if "." in num else int(num) for num in numbers_no_commas
]
if len(numbers) > 1:
first_number = numbers[0]
last_number = numbers[-1]
# Check if the original line had a negative sign before the last number
if "-" in line.split(":")[-1]:
last_number = -abs(last_number)
results.append((first_number, last_number))
# Convert results to dictionary
percentile_values = {}
for first_num, second_num in results:
key = first_num
percentile_values[key] = second_num
return percentile_values
percentile_values = extract_percentile_numbers(forecast_text)
if len(percentile_values) > 0:
return percentile_values
else:
raise ValueError(f"Could not extract prediction from response: {forecast_text}")
def generate_continuous_cdf(
percentile_values: dict,
question_type: str,
open_upper_bound: bool,
open_lower_bound: bool,
upper_bound: float,
lower_bound: float,
zero_point: float | None,
cdf_size: int,
) -> list[float]:
"""
Returns: list[float]: A list of 201 float values representing the CDF.
"""
percentiles = []
for percentile, value in percentile_values.items():
percentiles.append(Percentile(percentile=percentile/100, value=value))
numeric_distribution = NumericDistribution(
declared_percentiles=percentiles,
open_upper_bound=open_upper_bound,
open_lower_bound=open_lower_bound,
upper_bound=upper_bound,
lower_bound=lower_bound,
zero_point=zero_point,
cdf_size=cdf_size,
)
cdf_as_objects = numeric_distribution.get_cdf()
cdf_as_floats = [percentile.percentile for percentile in cdf_as_objects]
return cdf_as_floats
class NumericDefaults:
DEFAULT_CDF_SIZE = (
201 # Discrete questions have fewer points, Numeric will have 201 points
)
DEFAULT_INBOUND_OUTCOME_COUNT = DEFAULT_CDF_SIZE - 1
MAX_NUMERIC_PMF_VALUE = 0.2
@classmethod
def get_max_pmf_value(
cls, cdf_size: int, include_wiggle_room: bool = True
) -> float:
# cap depends on inboundOutcomeCount (0.2 if it is the default 200)
inbound_outcome_count = cdf_size - 1
normal_cap = cls.MAX_NUMERIC_PMF_VALUE * (
cls.DEFAULT_INBOUND_OUTCOME_COUNT / inbound_outcome_count
)
if include_wiggle_room:
return normal_cap * 0.95
else:
return normal_cap
class Percentile(BaseModel):
percentile: float = Field(
description="A number between 0 and 1 (e.g. '90% of people are age 60 or younger' translates to '0.9')",
)
value: float = Field(
description="The number matching the percentile (e.g. '90% of people are age 60 or younger' translates to '60')",
)
@model_validator(mode="after")
def validate_percentile(self: Percentile) -> Percentile:
if self.percentile < 0 or self.percentile > 1:
raise ValueError(
f"Percentile must be between 0 and 1, but was {self.percentile}"
)
if np.isnan(self.percentile):
raise ValueError(f"Percentile must be a number, but was {self.percentile}")
return self
class NumericDistribution(BaseModel):
declared_percentiles: list[Percentile]
open_upper_bound: bool
open_lower_bound: bool
upper_bound: float
lower_bound: float
zero_point: float | None
cdf_size: int | None = (
None # Normal numeric questions have 201 points, but discrete questions have fewer
)
standardize_cdf: bool = True
strict_validation: bool = True
is_date: bool = False
@model_validator(mode="after")
def validate_percentiles(self: NumericDistribution) -> NumericDistribution:
percentiles = self.declared_percentiles
self._check_percentiles_increasing()
self._check_log_scaled_fields()
if not self.strict_validation:
return self
self._check_percentile_spacing()
if self.standardize_cdf:
self._check_too_far_from_bounds(percentiles)
if self.standardize_cdf and len(percentiles) == self.cdf_size:
self._check_distribution_too_tall(percentiles)
self.declared_percentiles = self._check_and_update_repeating_values(percentiles)
return self
def _check_percentiles_increasing(self) -> None:
percentiles = self.declared_percentiles
for i in range(len(percentiles) - 1):
if percentiles[i].percentile >= percentiles[i + 1].percentile:
raise ValueError("Percentiles must be in strictly increasing order")
if percentiles[i].value > percentiles[i + 1].value:
raise ValueError("Values must be in strictly increasing order")
if len(percentiles) < 2:
raise ValueError("NumericDistribution must have at least 2 percentiles")
def _check_percentile_spacing(self) -> None:
percentiles = self.declared_percentiles
for i in range(len(percentiles) - 1):
if abs(percentiles[i + 1].percentile - percentiles[i].percentile) < 5e-05:
raise ValueError(
f"Percentiles at indices {i} and {i+1} are too close. CDF must be increasing by at least 5e-05 at every step. "
f"{percentiles[i].percentile} and {percentiles[i+1].percentile} "
f"at values {percentiles[i].value} and {percentiles[i+1].value}. "
"One possible reason is that your prediction is mostly or completely out of the upper/lower "
"bound range thus assigning very little probability to any one x-axis value."
)
def _check_log_scaled_fields(self) -> None:
if self.zero_point is not None and self.lower_bound <= self.zero_point:
raise ValueError(
f"Lower bound {self.lower_bound} is less than or equal to the zero point {self.zero_point}. "
"Lower bound must be greater than the zero point."
)
for percentile in self.declared_percentiles:
if self.zero_point is not None and percentile.value < self.zero_point:
raise ValueError(
f"Percentile value {percentile.value} is less than the zero point {self.zero_point}. "
"Determining probability less than zero point is currently not supported."
)
def _check_and_update_repeating_values(
self, percentiles: list[Percentile]
) -> list[Percentile]:
unique_value_count = Counter(percentile.value for percentile in percentiles)
final_percentiles = []
for percentile in percentiles:
value = percentile.value
count = unique_value_count[value]
repeated_value = count > 1
value_in_bounds = self.lower_bound < value < self.upper_bound
value_above_bound = value >= self.upper_bound
value_below_bound = value <= self.lower_bound
epsilon = 1e-10
if not repeated_value:
final_percentiles.append(percentile)
elif value_in_bounds:
greater_epsilon = 1e-6 # TODO: Figure out why normal epsilon doesn't work. Could cause brittle behavior.
modification = (1 - percentile.percentile) * greater_epsilon
final_percentiles.append(
Percentile(
value=value - modification,
percentile=percentile.percentile,
)
)
elif value_above_bound:
modification = epsilon * percentile.percentile
final_percentiles.append(
Percentile(
value=self.upper_bound + modification,
percentile=percentile.percentile,
)
)
elif value_below_bound:
modification = epsilon * (1 - percentile.percentile)
final_percentiles.append(
Percentile(
value=self.lower_bound - modification,
percentile=percentile.percentile,
)
)
else:
raise ValueError(
f"Unexpected state: value {value} is repeated {count} times. Bound is {self.lower_bound} and {self.upper_bound}"
)
return final_percentiles
def _check_too_far_from_bounds(self, percentiles: list[Percentile]) -> None:
max_to_min_range = self.upper_bound - self.lower_bound
# TODO: Better handle log scaled questions (a fixed wiggle room percentage doesn't work well for them)
wiggle_percent = 0.25
wiggle_room = max_to_min_range * wiggle_percent
upper_bound_plus_wiggle_room = self.upper_bound + wiggle_room
lower_bound_minus_wiggle_room = self.lower_bound - wiggle_room
percentiles_within_bounds_plus_wiggle_room = [
percentile
for percentile in percentiles
if lower_bound_minus_wiggle_room
<= percentile.value
<= upper_bound_plus_wiggle_room
]
if len(percentiles_within_bounds_plus_wiggle_room) == 0:
raise ValueError(
f"No declared percentiles are within the range of the question +/- {wiggle_percent * 100}%. "
f"Lower bound: {self.lower_bound}, upper bound: {self.upper_bound}. "
f"Percentiles: {percentiles}"
)
max_to_min_range_buffer = max_to_min_range * 2
percentiles_far_exceeding_bounds = [
percentile
for percentile in percentiles
if percentile.value < self.lower_bound - max_to_min_range_buffer
or percentile.value > self.upper_bound + max_to_min_range_buffer
]
if len(percentiles_far_exceeding_bounds) > 0:
raise ValueError(
"Some declared percentiles are far exceeding the bounds of the question. "
f"Lower bound: {self.lower_bound}, upper bound: {self.upper_bound}. "
f"Percentiles: {percentiles_far_exceeding_bounds}"
)
def _check_distribution_too_tall(self, cdf: list[Percentile]) -> None:
if len(cdf) != self.cdf_size:
raise ValueError(
f"CDF size is not the same as the declared percentiles. CDF size: {len(cdf)}, declared percentiles: {self.cdf_size}"
)
cap = NumericDefaults.get_max_pmf_value(len(cdf), include_wiggle_room=False)
for i in range(len(cdf) - 1):
pmf_value = cdf[i + 1].percentile - cdf[i].percentile
if pmf_value > cap:
raise ValueError(
f"Distribution is too concentrated. The probability mass between "
f"values {cdf[i].value} and {cdf[i + 1].value} is {pmf_value:.4f}, "
f"which exceeds the maximum allowed of {cap:.4f}."
)
def get_cdf(self) -> list[Percentile]:
"""
Turns a list of percentiles into a full distribution (201 points, if numeric, otherwise based on discrete values)
between upper and lower bound (taking into account probability assigned above and below the bounds)
that is compatible with Metaculus questions.
cdf stands for 'continuous distribution function'
At Metaculus CDFs are often represented with 201 points. Each point has:
- percentile ("X% of values are below this point". This is the y axis of the cdf graph)
- 'value' or 'nominal location' (The real world number that answers the question)
- cdf location (a number between 0 and 1 representing where the point is on the cdf x axis, where 0 is range min, and 1 is range max)
"""
cdf_size = self.cdf_size or NumericDefaults.DEFAULT_CDF_SIZE
continuous_cdf = []
cdf_xaxis = []
cdf_eval_locations = [i / (cdf_size - 1) for i in range(cdf_size)]
for l in cdf_eval_locations:
continuous_cdf.append(self._get_cdf_at(l))
cdf_xaxis.append(self._cdf_location_to_nominal_location(l))
if self.standardize_cdf:
continuous_cdf = self._standardize_cdf(continuous_cdf)
percentiles = [
Percentile(value=value, percentile=percentile)
for value, percentile in zip(cdf_xaxis, continuous_cdf)
]
assert len(percentiles) == cdf_size
validation_distribution = NumericDistribution(
declared_percentiles=percentiles,
open_upper_bound=self.open_upper_bound,
open_lower_bound=self.open_lower_bound,
upper_bound=self.upper_bound,
lower_bound=self.lower_bound,
zero_point=self.zero_point,
standardize_cdf=self.standardize_cdf,
)
NumericDistribution.model_validate(validation_distribution)
return percentiles
@classmethod
def _percentile_list_to_dict(
cls, percentiles: list[Percentile], multiply_by_100: bool
) -> dict[float, float]:
return {
(
percentile.percentile * 100
if multiply_by_100
else percentile.percentile
): percentile.value
for percentile in percentiles
}
@classmethod
def _dict_to_percentile_list(
cls, percentile_dict: dict[float, float], divide_by_100: bool
) -> list[Percentile]:
return [
Percentile(
percentile=percentile / 100 if divide_by_100 else percentile,
value=value,
)
for percentile, value in percentile_dict.items()
]
def _add_explicit_upper_lower_bound_percentiles(
self,
input_percentiles: list[Percentile],
) -> list[Percentile]:
open_upper_bound = self.open_upper_bound
open_lower_bound = self.open_lower_bound
range_max = self.upper_bound
range_min = self.lower_bound
return_percentiles = self._percentile_list_to_dict(
input_percentiles, multiply_by_100=True
)
percentile_max = max(percentile for percentile in return_percentiles.keys())
percentile_min = min(percentile for percentile in return_percentiles.keys())
range_size = abs(range_max - range_min)
buffer = 1 if range_size > 100 else 0.01 * range_size
# Adjust any values that are exactly at the bounds
for percentile, value in list(return_percentiles.items()):
# TODO: Handle this more gracefully for log scaled questions
# (where buffer could be quite a bit on the lower bound side)
if not open_lower_bound and value <= range_min + buffer:
return_percentiles[percentile] = range_min + buffer
if not open_upper_bound and value >= range_max - buffer:
return_percentiles[percentile] = range_max - buffer
# Set cdf values outside range
if open_upper_bound:
if range_max > return_percentiles[percentile_max]:
halfway_between_max_and_100th_percentile = 100 - (
0.5 * (100 - percentile_max)
)
return_percentiles[halfway_between_max_and_100th_percentile] = range_max
else:
return_percentiles[100] = range_max
# Set cdf values outside range
if open_lower_bound:
if range_min < return_percentiles[percentile_min]:
halfway_between_min_and_0th_percentile = 0.5 * percentile_min
return_percentiles[halfway_between_min_and_0th_percentile] = range_min
else:
return_percentiles[0] = range_min
sorted_return_percentiles = dict(sorted(return_percentiles.items()))
return_list = self._dict_to_percentile_list(
sorted_return_percentiles, divide_by_100=True
)
return return_list
def _nominal_location_to_cdf_location(self, nominal_value: float) -> float:
"""
Takes the real world value (like $17k - that would answer the forecasting question)
and converts it to a cdf location between 0 and 1 depending on
how far it is between the upper and lower bound
(it can go over 1 or below 0 if beyond the bounds)
"""
range_max = self.upper_bound
range_min = self.lower_bound
zero_point = self.zero_point
if zero_point is not None:
# logarithmically scaled question
deriv_ratio = (range_max - zero_point) / (range_min - zero_point)
if nominal_value == zero_point:
# If nominal = zero point, then you would take the log of 0. Add a small epsilon to avoid this.
nominal_value += 1e-10
unscaled_location = (
np.log(
(nominal_value - range_min) * (deriv_ratio - 1)
+ (range_max - range_min)
)
- np.log(range_max - range_min)
) / np.log(deriv_ratio)
else:
# linearly scaled question
unscaled_location = (nominal_value - range_min) / (range_max - range_min)
return float(unscaled_location)
def _get_cdf_at(self, cdf_location: float) -> float:
"""
Helper function that takes a cdf location and returns
the height (percentile) of the cdf at that location, linearly