update_predictions.rkt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #lang racket/base
  2. ; Add new predictions, update predictions, add outcomes
  3. (require racket/cmdline)
  4. (require racket/date)
  5. (require racket/list)
  6. (require db)
  7. (require "scoring_rules.rkt")
  8. (define progname "update_predictions.rkt")
  9. ; load configuration file
  10. (require (file "../config.rkt"))
  11. ; give us the date in YYYY-MM-DD format
  12. (date-display-format 'iso-8601)
  13. (define (pending)
  14. (write-string "Functionality not yet implemented.\n"))
  15. ; set up command line arguments
  16. (define mode (command-line
  17. #:program "update_prediction"
  18. #:args ([updatetype "help"]) ; (add, update, outcome help)
  19. updatetype))
  20. ; print some help
  21. (define (printhelp)
  22. (write-string (string-append "Usage: "
  23. progname " MODE\n\n"))
  24. (write-string "Where MODE is one of:\n")
  25. (write-string " add\t\t - add new prediction to database.\n")
  26. (write-string " update\t\t - update a prediction with results.\n")
  27. (write-string " list-open\t - Show all predictions that do not yet have outcomes.\n")
  28. (write-string " list-closed\t - Show all predictions that have outcomes.\n")
  29. (write-string " score\t\t - Calculate and display Brier scores for predictions with logged outcomes.\n")
  30. (write-string " help\t\t - Show this help message.\n")
  31. (write-string "\nCopyright 2019 George C. Privon\n"))
  32. ; set up a condensed prompt for getting information
  33. (define (getinput prompt)
  34. (write-string prompt)
  35. (write-string ": ")
  36. (read-line))
  37. ; add a new prediction
  38. (define (addpred)
  39. ; manually get incremented ID
  40. (define nID (+ 1
  41. (query-value conn "SELECT ID FROM predictions order by ID desc limit 1")))
  42. (define prediction (getinput "Enter the prediction"))
  43. (define fprob (getinput "What is your forecast probability? "))
  44. (define comments (getinput "Comments on the forecast"))
  45. (define categories (getinput "Enter any categories (comma-separated)"))
  46. (define date (getinput "Enter the date of the forecast (YYYY-MM-DD)"))
  47. (query-exec conn "INSERT INTO predictions (ID, date, prediction, forecast, comments, categories) values (?,?, ?, ?, ?, ?)"
  48. nID date prediction fprob comments categories))
  49. ; print a prediction given an ID
  50. (define (printpred ID)
  51. ; write most recent forecast information
  52. (write-string ((λ (myID)
  53. (define prediction (query-value conn "SELECT prediction FROM predictions WHERE ID=? ORDER BY date ASC LIMIT 1" myID))
  54. (define lastf (query-row conn "SELECT date, forecast FROM predictions WHERE ID=? AND forecast IS NOT NULL ORDER BY date DESC LIMIT 1" myID))
  55. (string-append (number->string myID)
  56. "("
  57. (vector-ref lastf 0)
  58. ") "
  59. prediction
  60. ": "
  61. (number->string (vector-ref lastf 1))))
  62. ID))
  63. (write-string "\n"))
  64. ; update a prediction
  65. (define (updatepred ID)
  66. (define option (string->number (getinput "Enter \"1\" to add an updated prediction or \"2\" to enter an outcome")))
  67. (cond
  68. [(eq? option 1) (reviseprediction ID)]
  69. [(eq? option 2) (addoutcome ID)]))
  70. ; add a new forecast to an existing prediction
  71. (define (reviseprediction ID)
  72. (define newf (string->number (getinput "What is your new predction")))
  73. (define newfdate (getinput "What is the date of the new prediction (YYYY-MM-DD)"))
  74. (define comments (getinput "Comments on the new prediction"))
  75. (query-exec conn "INSERT INTO predictions (ID, date, forecast, comments) values (?, ?, ?, ?)"
  76. ID newfdate newf comments))
  77. ; enter an outcome
  78. (define (addoutcome ID)
  79. (define lastpred (query-value conn "SELECT forecast FROM predictions WHERE ID=? ORDER BY date DESC LIMIT 1" ID))
  80. (define outcome (string->number (getinput "What is the outcome (0 for didn't happen, 1 for happened)")))
  81. (define outcomedate (getinput "What was the date of the outcome (YYYY-MM-DD)"))
  82. (define comments (getinput "Comments on the outcome"))
  83. (cond
  84. [(not (or (eq? outcome 0) (eq? outcome 1))) (error "Outcome must be 0 or 1.\n")])
  85. (query-exec conn "INSERT INTO predictions (ID, date, outcome, comments) values (?, ?, ?, ?)"
  86. ID outcomedate outcome comments)
  87. (define bscore (brier-score lastpred outcome))
  88. (write-string (string-append "Brier score of most recent forecast: "
  89. (number->string bscore)
  90. "\n.")))
  91. ; print open predictions
  92. (define (printopen)
  93. ; get a list of all IDs
  94. (define allIDs (query-list conn
  95. "SELECT DISTINCT ID FROM predictions"))
  96. ; get list of resolved predictions
  97. (define resIDs (query-list conn
  98. "SELECT DISTINCT ID FROM predictions WHERE outcome IS NOT NULL"))
  99. ; remove out the IDs that are resolved, keeping only the open predictions
  100. (define uIDs (filter-map (λ (testID)
  101. (if (member testID resIDs) #f testID))
  102. allIDs))
  103. ; print a header and individual entry information
  104. (write-string "ID(DATE) PREDICTION: LATEST FORECAST\n")
  105. (map printpred uIDs))
  106. ; print resolved predictions
  107. (define (printres)
  108. (define uIDs (query-list conn
  109. "SELECT DISTINCT ID FROM predictions WHERE outcome IS NOT NULL"))
  110. (write-string "ID(DATE) PREDICTION: LAST FORECAST, OUTCOME, BRIER SCORE\n")
  111. (map printpred uIDs))
  112. ; find unresolved predictions
  113. (define (findpending)
  114. (printopen)
  115. (define upID (getinput "Please enter a prediction number to edit (enter 0 or nothing to exit)"))
  116. (cond
  117. [(eq? (string->number upID) 0) (exit)]
  118. [(string->number upID) (updatepred (string->number upID))]
  119. [else (exit)]))
  120. ; compute Brier score for all predictions with outcomes
  121. (define (score)
  122. (define uIDs (query-list conn
  123. "SELECT DISTINCT ID FROM predictions WHERE outcome IS NOT NULL"))
  124. ; mapping across uIDs:
  125. ; - get list of all forecasts for this prediction
  126. ; - compute Brier score for all forecasts (relative to outcome)
  127. ; - write out
  128. (pending)
  129. )
  130. ; make sure we can use the sqlite3 connection
  131. (cond (not (sqlite3-available?))
  132. (error "Sqlite3 library not available."))
  133. ; open the database file
  134. (define conn (sqlite3-connect #:database dbloc))
  135. ; determine which mode we're in
  136. (cond
  137. [(regexp-match "help" mode) (printhelp)]
  138. [(regexp-match "add" mode) (addpred)]
  139. [(regexp-match "update" mode) (findpending)]
  140. [(regexp-match "list-open" mode) (printopen)]
  141. [(regexp-match "list-closed" mode) (printres)]
  142. [(regexp-match "score" mode) (score)]
  143. [else (error(string-append "Unknown mode. Try " progname " help\n\n"))])
  144. ; close the databse
  145. (disconnect conn)