1
0

update_predictions.rkt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. (define newf (string->number (getinput "What is your new predction")))
  77. (define newfdate (getinput "What is the date of the new prediction (YYYY-MM-DD)"))
  78. (define comments (getinput "Comments on the new prediction"))
  79. (query-exec conn "INSERT INTO predictions (ID, date, forecast, comments) values (?, ?, ?, ?)"
  80. ID newfdate newf comments))
  81. ; enter an outcome
  82. (define (addoutcome ID)
  83. (define outcome (string->number (getinput "What is the outcome (0 for didn't happen, 1 for happened)")))
  84. (define outcomedate (getinput "What was the date of the outcome (YYYY-MM-DD)"))
  85. (define comments (getinput "Comments on the outcome"))
  86. (cond
  87. [(not (or (eq? outcome 0) (eq? outcome 1))) (error "Outcome must be 0 or 1.\n")])
  88. (query-exec conn "INSERT INTO predictions (ID, date, outcome, comments) values (?, ?, ?, ?, ?)"
  89. ID outcomedate outcome comments))
  90. ; enter an outcome for a prediction
  91. (define (showoutcome)
  92. (pending))
  93. ; print open predictions
  94. (define (printopen)
  95. (define uIDs (query-list conn
  96. "SELECT DISTINCT ID FROM predictions"))
  97. (map printpred uIDs))
  98. ; find unresolved predictions
  99. (define (findpending)
  100. (printopen)
  101. (define upID (getinput "Please enter a prediction number to edit (enter 0 or nothing to exit)"))
  102. (cond
  103. [(eq? (string->number upID) 0) (exit)]
  104. [(string->number upID) (updatepred (string->number upID))]
  105. [else (exit)]))
  106. ; compute Brier score for all predictions with outcomes
  107. (define (score)
  108. (pending)
  109. )
  110. ; make sure we can use the sqlite3 connection
  111. (cond (not (sqlite3-available?))
  112. (error "Sqlite3 library not available."))
  113. ; open the database file
  114. (define conn (sqlite3-connect #:database dbloc))
  115. ; determine which mode we're in
  116. (cond
  117. [(regexp-match "help" mode) (printhelp)]
  118. [(regexp-match "add" mode) (addpred)]
  119. [(regexp-match "update" mode) (findpending)]
  120. [(regexp-match "list-open" mode) (printopen)]
  121. [(regexp-match "score" mode) (score)]
  122. [else (error(string-append "Unknown mode. Try " progname " help\n\n"))])
  123. ; close the databse
  124. (disconnect conn)