1
0

update_predictions.rkt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. (define (printpred ID)
  53. (cond
  54. [(not (knownoutcome? ID)) (write-string ((λ (myID)
  55. (define prediction (query-value conn "SELECT prediction FROM predictions WHERE ID=? ORDER BY date ASC LIMIT 1" myID))
  56. (define latest (query-row conn "SELECT date, forecast FROM predictions WHERE ID=? ORDER BY date DESC LIMIT 1" myID))
  57. (string-append (number->string myID)
  58. "("
  59. (vector-ref latest 0)
  60. ") "
  61. prediction
  62. ": "
  63. (number->string (vector-ref latest 1))
  64. "\n"))
  65. ID))]))
  66. ; update a prediction
  67. (define (updatepred ID)
  68. (define option (string->number (getinput "Enter \"1\" to add an updated prediction or \"2\" to enter an outcome")))
  69. (cond
  70. [(eq? option 1) (reviseprediction ID)]
  71. [(eq? option 2) (addoutcome ID)])
  72. ;TODO: get new prediction or outcome and enter into db
  73. )
  74. ; add a new forecast to an existing prediction
  75. (define (reviseprediction ID)
  76. (pending)
  77. )
  78. ; enter an outcome
  79. (define (addoutcome 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. ; enter an outcome for a prediction
  88. (define (showoutcome)
  89. (pending))
  90. ; print open predictions
  91. (define (printopen)
  92. (define uIDs (query-list conn
  93. "SELECT DISTINCT ID FROM predictions"))
  94. (map printpred uIDs))
  95. ; find unresolved predictions
  96. (define (findpending)
  97. (printopen)
  98. (define upID (getinput "Please enter a prediction number to edit (enter 0 or nothing to exit)"))
  99. (cond
  100. [(eq? (string->number upID) 0) (exit)]
  101. [(string->number upID) (updatepred (string->number upID))]
  102. [else (exit)]))
  103. ; compute Brier score for all predictions with outcomes
  104. (define (score)
  105. (pending)
  106. )
  107. ; make sure we can use the sqlite3 connection
  108. (cond (not (sqlite3-available?))
  109. (error "Sqlite3 library not available."))
  110. ; open the database file
  111. (define conn (sqlite3-connect #:database dbloc))
  112. ; determine which mode we're in
  113. (cond
  114. [(regexp-match "help" mode) (printhelp)]
  115. [(regexp-match "add" mode) (addpred)]
  116. [(regexp-match "update" mode) (findpending)]
  117. [(regexp-match "list-open" mode) (printopen)]
  118. [(regexp-match "score" mode) (score)]
  119. [else (error(string-append "Unknown mode. Try " progname " help\n\n"))])
  120. ; close the databse
  121. (disconnect conn)