1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
pub mod fp_decimal;
mod utils;
pub mod vector;
use cosmwasm_std::{StdResult, Uint128};
pub use fp_decimal::*;
use std::str::FromStr;
pub use utils::*;
pub use vector::*;
/// ## Description
/// Calculates the cluster imbalance.
///
/// ## Params
/// - **i** is a reference to an array containing objects of type [`FPDecimal`] which
/// is the asset inventory.
///
/// - **p** is a reference to an array containing objects of type [`FPDecimal`] which
/// are the prices of the assets.
///
/// - **w** is a reference to an array containing objects of type [`FPDecimal`] which
/// are the target weights of the assets.
pub fn imbalance(i: &[FPDecimal], p: &[FPDecimal], w: &[FPDecimal]) -> FPDecimal {
// Target weights with prices
// -- u = elem_mul(targets, prices)
let u = mul(w, p);
// NAV with target weights instead of inventory
// -- wp = dot(targets, prices)
let wp = dot(w, p);
// Suppose
// A is the capital allocation
// -- A = elem_mul(inventory, prices)
// A_opt is the optimal capital allocation
// -- A_opt = u * rescale_to_actual_NAV
// = u * dot(inventory, prices) / wp
// Compute imbalance
// -- imb = | A_opt - A |
// = | u * dot(inventory, prices) / wp - elem_mul(inventory, prices) |
// = | u * dot(inventory, prices) - elem_mul(inventory, prices) * wp | / wp
let err_portfolio = sub(&mul_const(&u, dot(i, p)), &mul_const(&mul(i, p), wp));
sum(&abs(&err_portfolio)) / wp
}
/// ## Description
/// Converts an int32 array to a FPDecimal array.
///
/// ## Params
/// - **arr** is a reference to an array containing objects of type [`u32`].
pub fn int32_vec_to_fpdec(arr: &[u32]) -> Vec<FPDecimal> {
arr.iter().map(|val| FPDecimal::from(*val as u128)).collect()
}
/// ## Description
/// Converts an Uint128 array to a FPDecimal array.
///
/// ## Params
/// - **arr** is a reference to an array containing objects of type [`Uint128`].
pub fn int_vec_to_fpdec(arr: &[Uint128]) -> Vec<FPDecimal> {
arr.iter().map(|val| FPDecimal::from(val.u128())).collect()
}
/// ## Description
/// Converts an String array to a FPDecimal array.
///
/// ## Params
/// - **arr** is a reference to an array containing objects of type [`String`].
pub fn str_vec_to_fpdec(arr: &[String]) -> StdResult<Vec<FPDecimal>> {
arr.iter().map(|val| FPDecimal::from_str(val)).collect::<StdResult<Vec<FPDecimal>>>()
}