Replies: 4 comments 4 replies
-
You can do this via your problem definition: struct MyProblem {
data: MyData,
}
impl CostFunction for MyProblem {
type Param = Vec<f64>;
type Output = f64;
fn cost(&self, p: &Self::Param) -> Result<Self::Output, Error> {
Ok(my_cost_function(p, self.data))
}
} |
Beta Was this translation helpful? Give feedback.
-
Is it possible to have a model defined by a struct that contains the parameters, and then solve for those parameters against some exogenous data ? Something like: struct MyModel {
a: f64,
b: f64,
}
impl MyModel {
fn my_line(&self, x: f64) -> f64 {
self.a * x + self.b
}
fn my_cost(&self, x: f64, y: f64) -> f64 {
(self.my_line(x) - y).powi(2)
}
}
const SOME_X_DATA: [f64; 9] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
const SOME_Y_DATA: [f64; 9] = [0.0, 1.0, 2.0, 4.0, 7.0, 9.0, 5.0, 3.0, 7.0]; I would expect a result of: Y ~= 0.75 * X + 0.4722 So, I want to get: MyModel {
a: 0.75,
b: 0.4722
} Thanks for any advice you can give ! Cheers! |
Beta Was this translation helpful? Give feedback.
-
Do you mean such that struct MyProblem {}
impl CostFunction for MyProblem {
type Param = Vec<f64>;
type Output = f64;
fn cost(&self, p: &Self::Param) -> Result<Self::Output, Error> {
let cost = (0..9).iter().map(|i| (p[0] * SOME_X_DATA[i] + p[1] - SOME_Y_DATA[i]).powi(2)).sum();
Ok(cost)
}
}
// Then run optimization, obtain final solution `res`
// ...
let final_params = MyModel { a: res[0], b: res[1] }; // Of course you can also implement `From<Vec<f64>>` for `MyModel` Then |
Beta Was this translation helpful? Give feedback.
-
Hi, taking a look at this again after a while. If the additional data/arguments are not e.g. |
Beta Was this translation helpful? Give feedback.
-
If I want to minimise a squared error (or similar) between a function and some data, how do I pass the data to the cost function ?
Beta Was this translation helpful? Give feedback.
All reactions