@@ -167,18 +167,11 @@ impl<T: Config> Pallet<T> {
167
167
inplace_col_normalize ( & mut bonds) ; // sum_i b_ij = 1
168
168
log:: trace!( "B:\n {:?}\n " , & bonds) ;
169
169
170
+ // Get alpha values
171
+ let alpha = Self :: compute_liquid_alpha ( netuid, consensus. clone ( ) ) ;
172
+
170
173
// Compute the Exponential Moving Average (EMA) of bonds.
171
- // Check if Liquid Alpha is enabled, consensus is not empty, and contains non-zero values.
172
- let mut ema_bonds = if let Some ( clamped_bonds_alpha) =
173
- Self :: compute_liquid_alpha ( netuid, consensus. clone ( ) )
174
- {
175
- // Compute the Exponential Moving Average (EMA) of bonds using the clamped alpha values.
176
- Self :: compute_ema_bonds_with_liquid_alpha ( & weights. clone ( ) , & bonds, clamped_bonds_alpha)
177
- } else {
178
- log:: trace!( "Using Bonds Moving Average" ) ;
179
- // Compute the EMA of bonds using a normal alpha value.
180
- Self :: compute_ema_bonds_normal ( & weights. clone ( ) , & bonds, netuid)
181
- } ;
174
+ let mut ema_bonds = Self :: compute_ema_bonds ( & weights. clone ( ) , & bonds, alpha) ;
182
175
// Normalize EMA bonds.
183
176
inplace_col_normalize ( & mut ema_bonds) ; // sum_i b_ij = 1
184
177
log:: trace!( "emaB:\n {:?}\n " , & ema_bonds) ;
@@ -530,22 +523,11 @@ impl<T: Config> Pallet<T> {
530
523
inplace_col_normalize_sparse ( & mut bonds, n) ;
531
524
log:: trace!( "B (mask+norm): {:?}" , & bonds) ;
532
525
533
- // Compute the Exponential Moving Average (EMA) of bonds.
534
- let mut ema_bonds = if let Some ( clamped_bonds_alpha) =
535
- Self :: compute_liquid_alpha ( netuid, consensus. clone ( ) )
536
- {
537
- // Compute the Exponential Moving Average (EMA) of bonds using the clamped alpha values.
538
- Self :: compute_ema_bonds_with_liquid_alpha_sparse (
539
- & weights. clone ( ) ,
540
- & bonds,
541
- clamped_bonds_alpha,
542
- )
543
- } else {
544
- log:: trace!( "Using Bonds Moving Average" ) ;
545
- // Compute the EMA of bonds using a normal alpha value.
546
- Self :: compute_ema_bonds_normal_sparse ( & weights. clone ( ) , & bonds, netuid)
547
- } ;
526
+ // Get alpha values
527
+ let alpha = Self :: compute_liquid_alpha ( netuid, consensus. clone ( ) ) ;
548
528
529
+ // Compute the Exponential Moving Average (EMA) of bonds.
530
+ let mut ema_bonds = Self :: compute_ema_bonds_sparse ( & weights. clone ( ) , & bonds, alpha) ;
549
531
// Normalize EMA bonds.
550
532
inplace_col_normalize_sparse ( & mut ema_bonds, n) ; // sum_i b_ij = 1
551
533
log:: trace!( "Exponential Moving Average Bonds: {:?}" , & ema_bonds) ;
@@ -937,16 +919,16 @@ impl<T: Config> Pallet<T> {
937
919
clamped_alpha
938
920
}
939
921
940
- /// Compute the Exponential Moving Average (EMA) of bonds using the clamped alpha values for a sparse matrix.
922
+ /// Compute the Exponential Moving Average (EMA) of bonds using the alpha values for a sparse matrix.
941
923
///
942
924
/// # Args:
943
925
/// * `bonds_delta` - A vector of bond deltas.
944
926
/// * `bonds` - A vector of bonds.
945
- /// * `alpha` - A vector of clamped alpha values.
927
+ /// * `alpha` - A vector of clamped alpha values (for liquid alpha) or constant alpha values .
946
928
///
947
929
/// # Returns:
948
930
/// A vector of EMA bonds.
949
- pub fn compute_ema_bonds_with_liquid_alpha_sparse (
931
+ pub fn compute_ema_bonds_sparse (
950
932
bonds_delta : & [ Vec < ( u16 , I32F32 ) > ] ,
951
933
bonds : & [ Vec < ( u16 , I32F32 ) > ] ,
952
934
alpha : Vec < I32F32 > ,
@@ -955,25 +937,22 @@ impl<T: Config> Pallet<T> {
955
937
let ema_bonds = mat_ema_alpha_vec_sparse ( bonds_delta, bonds, & alpha) ;
956
938
957
939
// Log the computed EMA bonds for debugging purposes.
958
- log:: trace!(
959
- "Exponential Moving Average Bonds Liquid Alpha: {:?}" ,
960
- ema_bonds
961
- ) ;
940
+ log:: trace!( "Exponential Moving Average Bonds: {:?}" , ema_bonds) ;
962
941
963
942
// Return the computed EMA bonds.
964
943
ema_bonds
965
944
}
966
945
967
- /// Compute the Exponential Moving Average (EMA) of bonds using the clamped alpha values.
946
+ /// Compute the Exponential Moving Average (EMA) of bonds using the alpha values.
968
947
///
969
948
/// # Args:
970
949
/// * `bonds_delta` - A vector of bond deltas.
971
950
/// * `bonds` - A vector of bonds.
972
- /// * `alpha` - A vector of clamped alpha values.
951
+ /// * `alpha` - A vector of clamped alpha values (for liquid alpha) or constant alpha values .
973
952
///
974
953
/// # Returns:
975
954
/// A vector of EMA bonds.
976
- pub fn compute_ema_bonds_with_liquid_alpha (
955
+ pub fn compute_ema_bonds (
977
956
bonds_delta : & [ Vec < I32F32 > ] ,
978
957
bonds : & [ Vec < I32F32 > ] ,
979
958
alpha : Vec < I32F32 > ,
@@ -982,76 +961,7 @@ impl<T: Config> Pallet<T> {
982
961
let ema_bonds = mat_ema_alpha_vec ( bonds_delta, bonds, & alpha) ;
983
962
984
963
// Log the computed EMA bonds for debugging purposes.
985
- log:: trace!(
986
- "Exponential Moving Average Bonds Liquid Alpha: {:?}" ,
987
- ema_bonds
988
- ) ;
989
-
990
- // Return the computed EMA bonds.
991
- ema_bonds
992
- }
993
-
994
- /// Compute the Exponential Moving Average (EMA) of bonds using a normal alpha value for a sparse matrix.
995
- ///
996
- /// # Args:
997
- /// * `bonds_delta` - A vector of bond deltas.
998
- /// * `bonds` - A vector of bonds.
999
- /// * `netuid` - The network ID.
1000
- ///
1001
- /// # Returns:
1002
- /// A vector of EMA bonds.
1003
- pub fn compute_ema_bonds_normal_sparse (
1004
- bonds_delta : & [ Vec < ( u16 , I32F32 ) > ] ,
1005
- bonds : & [ Vec < ( u16 , I32F32 ) > ] ,
1006
- netuid : u16 ,
1007
- ) -> Vec < Vec < ( u16 , I32F32 ) > > {
1008
- // Retrieve the bonds moving average for the given network ID and scale it down.
1009
- let bonds_moving_average: I64F64 = I64F64 :: from_num ( Self :: get_bonds_moving_average ( netuid) )
1010
- . saturating_div ( I64F64 :: from_num ( 1_000_000 ) ) ;
1011
-
1012
- // Calculate the alpha value for the EMA calculation.
1013
- // Alpha is derived by subtracting the scaled bonds moving average from 1.
1014
- let alpha: I32F32 =
1015
- I32F32 :: from_num ( 1 ) . saturating_sub ( I32F32 :: from_num ( bonds_moving_average) ) ;
1016
-
1017
- // Compute the Exponential Moving Average (EMA) of bonds using the calculated alpha value.
1018
- let ema_bonds = mat_ema_sparse ( bonds_delta, bonds, alpha) ;
1019
-
1020
- // Log the computed EMA bonds for debugging purposes.
1021
- log:: trace!( "Exponential Moving Average Bonds Normal: {:?}" , ema_bonds) ;
1022
-
1023
- // Return the computed EMA bonds.
1024
- ema_bonds
1025
- }
1026
-
1027
- /// Compute the Exponential Moving Average (EMA) of bonds using a normal alpha value.
1028
- ///
1029
- /// # Args:
1030
- /// * `bonds_delta` - A vector of bond deltas.
1031
- /// * `bonds` - A vector of bonds.
1032
- /// * `netuid` - The network ID.
1033
- ///
1034
- /// # Returns:
1035
- /// A vector of EMA bonds.
1036
- pub fn compute_ema_bonds_normal (
1037
- bonds_delta : & [ Vec < I32F32 > ] ,
1038
- bonds : & [ Vec < I32F32 > ] ,
1039
- netuid : u16 ,
1040
- ) -> Vec < Vec < I32F32 > > {
1041
- // Retrieve the bonds moving average for the given network ID and scale it down.
1042
- let bonds_moving_average: I64F64 = I64F64 :: from_num ( Self :: get_bonds_moving_average ( netuid) )
1043
- . saturating_div ( I64F64 :: from_num ( 1_000_000 ) ) ;
1044
-
1045
- // Calculate the alpha value for the EMA calculation.
1046
- // Alpha is derived by subtracting the scaled bonds moving average from 1.
1047
- let alpha: I32F32 =
1048
- I32F32 :: from_num ( 1 ) . saturating_sub ( I32F32 :: from_num ( bonds_moving_average) ) ;
1049
-
1050
- // Compute the Exponential Moving Average (EMA) of bonds using the calculated alpha value.
1051
- let ema_bonds = mat_ema ( bonds_delta, bonds, alpha) ;
1052
-
1053
- // Log the computed EMA bonds for debugging purposes.
1054
- log:: trace!( "Exponential Moving Average Bonds Normal: {:?}" , ema_bonds) ;
964
+ log:: trace!( "Exponential Moving Average Bonds: {:?}" , ema_bonds) ;
1055
965
1056
966
// Return the computed EMA bonds.
1057
967
ema_bonds
@@ -1065,7 +975,7 @@ impl<T: Config> Pallet<T> {
1065
975
///
1066
976
/// # Returns:
1067
977
/// A vector of alphas
1068
- pub fn compute_liquid_alpha ( netuid : u16 , consensus : Vec < I32F32 > ) -> Option < Vec < I32F32 > > {
978
+ pub fn compute_liquid_alpha ( netuid : u16 , consensus : Vec < I32F32 > ) -> Vec < I32F32 > {
1069
979
// Check if Liquid Alpha is enabled, consensus is not empty, and contains non-zero values.
1070
980
if LiquidAlphaOn :: < T > :: get ( netuid)
1071
981
&& !consensus. is_empty ( )
@@ -1092,15 +1002,30 @@ impl<T: Config> Pallet<T> {
1092
1002
) ;
1093
1003
1094
1004
// Compute the alpha values using the logistic function parameters.
1005
+ // alpha = 1 / (1 + math.e ** (-a * C + b)) # alpha to the old weight
1095
1006
let alpha = Self :: compute_alpha_values ( & consensus, a, b) ;
1096
1007
1097
1008
// Clamp the alpha values between alpha_high and alpha_low.
1098
1009
let clamped_alpha = Self :: clamp_alpha_values ( alpha, alpha_high, alpha_low) ;
1099
1010
1100
- return Some ( clamped_alpha) ;
1011
+ return clamped_alpha;
1101
1012
}
1102
1013
}
1103
- None
1014
+
1015
+ // Liquid Alpha is disabled
1016
+ // or high and low consensus values do not meet the required conditions.
1017
+ // return vector of constant alpha
1018
+
1019
+ // Retrieve the bonds moving average for the given network ID and scale it down.
1020
+ let bonds_moving_average: I64F64 = I64F64 :: from_num ( Self :: get_bonds_moving_average ( netuid) )
1021
+ . saturating_div ( I64F64 :: from_num ( 1_000_000 ) ) ;
1022
+
1023
+ // Calculate the alpha value for the EMA calculation.
1024
+ // Alpha is derived by subtracting the scaled bonds moving average from 1.
1025
+ let alpha: I32F32 =
1026
+ I32F32 :: from_num ( 1 ) . saturating_sub ( I32F32 :: from_num ( bonds_moving_average) ) ;
1027
+
1028
+ vec ! [ alpha; consensus. len( ) ]
1104
1029
}
1105
1030
1106
1031
pub fn do_set_alpha_values (
0 commit comments