1
0

update_predictions.rkt 4.7 KB

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