update_predictions.rkt 6.2 KB

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