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