I have a function that creates a ChunkArray<ListType> from two chunk arrays, however I'm having a hard time converting the column function into a "Function Expression".
The goal is something similar to pearson or spearman correlation implementation in Rust Polars where it takes in two parameters instead of self, see this.
The output should be a new column of type List.
Here is my code where I'm trying to create synthetic_data_expr, however I hit a dead end on evaluating the expressions.
use polars::prelude::*;
use rand::thread_rng;
use rand_distr::{Normal, Distribution};
fn synthetic_data(
mean_series:&ChunkedArray<Float64Type>,
variance_series:&ChunkedArray<Float64Type>,
) -> PolarsResult<ChunkedArray<ListType>> {
let mut rng = thread_rng();
let random_values: Vec<Vec<f64>> = mean_series.iter()
.zip(variance_series.iter())
.map(|(mean, variance)| {
let std_dev = variance.unwrap().sqrt();
let normal_dist = Normal::new(mean.unwrap(), std_dev).unwrap();
(0..39).map(|_| normal_dist.sample(&mut rng)).collect()
})
.collect();
let mut list_chunk = ListPrimitiveChunkedBuilder::<Float64Type>::new(
"intraday".into(),
5, //rows of data
39,
DataType::Float64
);
for row in random_values {
list_chunk.append_slice(&row);
}
Ok(list_chunk.finish())
}
fn synthetic_data_column(s:&[Column]) -> PolarsResult<Column> {
let _mean = &s[0];
let _varaince = &s[1];
let calc = synthetic_data(_mean.f64().unwrap(), _varaince.f64().unwrap());
Ok(calc?.into_column())
}
fn synthetic_data_expr(mean_column: Expr, variance_column: Expr) -> Expr {
mean_column.apply_many(
synthetic_intraday_data_column(),
&[variance_column],
GetOutput::same_type(),
)
}
Here is an example that I'm trying to accomplish for synthetic_data_expr
/// Compute the pearson correlation between two columns.
pub fn pearson_corr(a: Expr, b: Expr) -> Expr {
let input = vec![a, b];
let function = FunctionExpr::Correlation {
method: CorrelationMethod::Pearson,
};
Expr::Function {
input,
function,
options: FunctionOptions {
collect_groups: ApplyOptions::GroupWise,
cast_options: Some(CastingRules::cast_to_supertypes()),
flags: FunctionFlags::default() | FunctionFlags::RETURNS_SCALAR,
..Default::default()
},
}
}