update_predictions.rkt 8.0 KB

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