Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. {-|
  2. > ```
  3. Consider a simple model for whether a person has the flu or not. Let F=1
  4. indicate that a person has the flu and F=0 indicate that they don't have the
  5. flu. Let C=1 indicate that the person has a cough and C=0 indicate that they
  6. don't have a cough. Let M=1 indicate that the person has muscle pain and M=0
  7. indicate that they don't have muscle pain. Assume that C and M are conditionally
  8. independent given F so that the probability model is
  9. P(C=c,M=m,F=f)=P(C=c|F=f)P(M=m|F=f)P(F=f).
  10. Suppose that we ask two different doctors to supply probabilities for this model
  11. and we obtain the following results:
  12. Doctor 1:
  13. P(F=1)=0.4
  14. P(C=1|F=0)=0.2, P(C=1|F=1)=0.8
  15. P(M=1|F=0)=0.3, P(M=1|F=1)=0.9
  16. Doctor 2:
  17. P(F=1)=0.5
  18. P(C=1|F=0)=0.1, P(C=1|F=1)=0.7
  19. P(M=1|F=0)=0.2, P(M=1|F=1)=0.8
  20. Suppose we also have access to 10 patient records recording both the symptoms
  21. and whether each patient was diagnosed with flu or not as shown below:
  22. Patient C M F
  23. 1 1 0 0
  24. 2 1 1 1
  25. 3 1 1 1
  26. 4 0 1 0
  27. 5 0 1 1
  28. 6 1 0 1
  29. 7 0 0 0
  30. 8 1 0 0
  31. 9 0 0 0
  32. 10 1 1 1
  33. ```
  34. -}
  35.  
  36. import Html exposing (text)
  37.  
  38. {-| There are three different types of events that can happen
  39. -}
  40. type EventType = C | M | F
  41.  
  42.  
  43. {-| An event is a fact about what happened with a certain patient.
  44. -}
  45. type Event a = Event EventType a
  46.  
  47.  
  48. {-| A patient is a a combination of three events: C, M, and F
  49. We represent it as a List, instead of a record, so that we have the
  50. types C M and F represent event types throughout.
  51. -}
  52. type alias Patient = List (Event Int)
  53.  
  54. {-| Checks if an event happened for a patient.
  55. -}
  56. event_happened : Patient -> Event Int -> Bool
  57. event_happened patient event =
  58. List.member event patient
  59.  
  60. {-| A known probability
  61.  
  62. A marginal probability
  63. P(C=True) = 0.8
  64. can be written as
  65. MarginalProb (C True) 0.8
  66.  
  67.  
  68. A Conditional probability
  69. P(C=True|A=False) = 0.5
  70. can be written as
  71. ConditionalProb (C True) (A False) 0.5
  72. -}
  73. type EventProb a
  74. = MarginalProb (Event a) Float
  75. | ConditionalProb (Event a) (Event a) Float
  76.  
  77. {-|
  78. A combination of statements about the world.
  79.  
  80. P(C=c|F=f)P(M=m|F=f)P(F=f)
  81. -}
  82. type alias Model a = List (EventProb a)
  83. type alias Doctor = Model Int
  84.  
  85. {-|
  86. Given some known probabilities about the world,
  87. return the probability of each of the patients, assuming they are independent
  88. -}
  89. likelihood_patients : Doctor -> List Patient -> Float
  90. likelihood_patients m ps =
  91. ps
  92. |> List.map (likelihood_patient m)
  93. |> List.product
  94.  
  95. {-|
  96. Given some known probabilities about the world,
  97. return the probability of the patient.
  98. -}
  99. likelihood_patient : Doctor -> Patient -> Float
  100. likelihood_patient model patient =
  101. let
  102. happened = event_happened patient
  103. event_prob event prob = if (happened event) then prob else 1 - prob
  104. in
  105. Debug.log ("patient: " ++ (toString patient))
  106. model
  107. |> Debug.log "model: "
  108. -- remove all conditional events where the condition isnt true
  109. |> List.filter
  110. (\ep -> case ep of
  111. MarginalProb _ _ -> True
  112. ConditionalProb _ given _ -> happened given
  113. )
  114. |> Debug.log "filtered: "
  115. -- give the probability of the event, if it happened
  116. -- and 1 - probability if it didnt happen
  117. |> List.map
  118. (\ep -> case ep of
  119. MarginalProb event prob -> event_prob event prob
  120. ConditionalProb event _ prob -> event_prob event prob
  121. )
  122. |> Debug.log "mapped: "
  123. |> List.product
  124.  
  125.  
  126.  
  127.  
  128. main : Html.Html
  129. main =
  130. let
  131. doctor1 : Doctor
  132. doctor1 =
  133. [ MarginalProb (Event F 1) 0.4
  134. , ConditionalProb (Event C 1) (Event F 0) 0.2
  135. , ConditionalProb (Event C 1) (Event F 1) 0.8
  136. , ConditionalProb (Event M 1) (Event F 0) 0.3
  137. , ConditionalProb (Event M 1) (Event F 1) 0.9
  138. ]
  139. patients : List Patient
  140. patients =
  141. [ [1, 0, 0]
  142. , [1, 1, 1]
  143. , [1, 1, 1]
  144. , [0, 1, 0]
  145. , [0, 1, 1]
  146. , [1, 0, 1]
  147. , [0, 0, 0]
  148. , [1, 0, 0]
  149. , [0, 0, 0]
  150. , [1, 1, 1]
  151. ]
  152. |> List.map
  153. (\l -> case l of
  154.  
  155. c :: m :: f :: [] ->
  156. [ Event C c
  157. , Event M m
  158. , Event F f
  159. ]
  160.  
  161. _ ->
  162. Debug.crash ""
  163. )
  164.  
  165. prob_patients_doctor1 = likelihood_patients doctor1 patients
  166. doctor2 : Doctor
  167. doctor2 =
  168. [ MarginalProb (Event F 1) 0.5
  169. , ConditionalProb (Event C 1) (Event F 0) 0.1
  170. , ConditionalProb (Event C 1) (Event F 1) 0.7
  171. , ConditionalProb (Event M 1) (Event F 0) 0.2
  172. , ConditionalProb (Event M 1) (Event F 1) 0.8
  173. ]
  174. prob_patients_doctor2 = likelihood_patients doctor2 patients
  175.  
  176. doctorTest =
  177. [ MarginalProb (Event F 1) 1
  178. , ConditionalProb (Event C 1) (Event F 0) 1
  179. , ConditionalProb (Event C 1) (Event F 1) 1
  180. , ConditionalProb (Event M 1) (Event F 0) 1
  181. , ConditionalProb (Event M 1) (Event F 1) 1
  182. ]
  183. test_prob = likelihood_patients doctorTest [[Event C 1, Event M 1, Event F 1]]
  184. test_prob_2 = likelihood_patients doctorTest [[Event C 0, Event M 0, Event F 0]]
  185. in
  186. text
  187. ( "doctor1: "
  188. ++ (toString prob_patients_doctor1)
  189. ++ " doctor2: "
  190. ++ (toString prob_patients_doctor2)
  191. --++ "test1: " ++ (toString test_prob)
  192. --++ "test2: " ++ (toString test_prob_2)
  193. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement