pmcore/algorithms/
postprob.rs

1use crate::{
2    algorithms::Status,
3    prelude::algorithms::Algorithms,
4    structs::{
5        psi::{calculate_psi, Psi},
6        theta::Theta,
7        weights::Weights,
8    },
9};
10use anyhow::{Context, Result};
11
12use pharmsol::prelude::{
13    data::{Data, ErrorModels},
14    simulator::Equation,
15};
16
17use crate::routines::evaluation::ipm::burke;
18use crate::routines::initialization;
19use crate::routines::output::{cycles::CycleLog, NPResult};
20use crate::routines::settings::Settings;
21
22/// Posterior probability algorithm
23/// Reweights the prior probabilities to the observed data and error model
24pub struct POSTPROB<E: Equation> {
25    equation: E,
26    psi: Psi,
27    theta: Theta,
28    w: Weights,
29    objf: f64,
30    cycle: usize,
31    status: Status,
32    data: Data,
33    settings: Settings,
34    cyclelog: CycleLog,
35    error_models: ErrorModels,
36}
37
38impl<E: Equation> Algorithms<E> for POSTPROB<E> {
39    fn new(settings: Settings, equation: E, data: Data) -> Result<Box<Self>, anyhow::Error> {
40        Ok(Box::new(Self {
41            equation,
42            psi: Psi::new(),
43            theta: Theta::new(),
44            w: Weights::default(),
45            objf: f64::INFINITY,
46            cycle: 0,
47            status: Status::Starting,
48            error_models: settings.errormodels().clone(),
49            settings,
50            data,
51            cyclelog: CycleLog::new(),
52        }))
53    }
54    fn into_npresult(&self) -> NPResult<E> {
55        NPResult::new(
56            self.equation.clone(),
57            self.data.clone(),
58            self.theta.clone(),
59            self.psi.clone(),
60            self.w.clone(),
61            self.objf,
62            self.cycle,
63            self.status.clone(),
64            self.settings.clone(),
65            self.cyclelog.clone(),
66        )
67    }
68    fn get_settings(&self) -> &Settings {
69        &self.settings
70    }
71
72    fn equation(&self) -> &E {
73        &self.equation
74    }
75
76    fn get_data(&self) -> &Data {
77        &self.data
78    }
79
80    fn get_prior(&self) -> Theta {
81        initialization::sample_space(&self.settings).unwrap()
82    }
83
84    fn likelihood(&self) -> f64 {
85        self.objf
86    }
87
88    fn inc_cycle(&mut self) -> usize {
89        0
90    }
91
92    fn get_cycle(&self) -> usize {
93        0
94    }
95
96    fn set_theta(&mut self, theta: Theta) {
97        self.theta = theta;
98    }
99
100    fn theta(&self) -> &Theta {
101        &self.theta
102    }
103
104    fn psi(&self) -> &Psi {
105        &self.psi
106    }
107
108    fn set_status(&mut self, status: Status) {
109        self.status = status;
110    }
111
112    fn status(&self) -> &Status {
113        &self.status
114    }
115
116    fn convergence_evaluation(&mut self) {
117        // POSTPROB algorithm converges after a single evaluation
118        self.status = Status::MaxCycles;
119    }
120
121    fn converged(&self) -> bool {
122        true
123    }
124
125    fn evaluation(&mut self) -> Result<()> {
126        self.psi = calculate_psi(
127            &self.equation,
128            &self.data,
129            &self.theta,
130            &self.error_models,
131            false,
132            false,
133        )?;
134        (self.w, self.objf) = burke(&self.psi).context("Error in IPM")?;
135        Ok(())
136    }
137
138    fn condensation(&mut self) -> Result<()> {
139        Ok(())
140    }
141    fn optimizations(&mut self) -> Result<()> {
142        Ok(())
143    }
144
145    fn logs(&self) {}
146
147    fn expansion(&mut self) -> Result<()> {
148        Ok(())
149    }
150}