% Example calculation of the (weighted) imbalance in Octave % % Imbalance of a channel: % b <-> 0 = 100% % 0 <-> b = 100% % 1/2b <-> 1/2b = 0% % Set 3 channel balances (in msatoshi) peer(1)=1000000000; we(1)=500000000; peer(2)=1100000000; we(2)=300000000; peer(3)=0; we(3)=300000000; imbalance=0; tot=0; for n=1:3 totc=peer(n)+we(n); % total of this channel tot=tot+totc; % total of all channels % Normalize difference <= 1, square, weight and sum imbalance=imbalance+((peer(n)-we(n))/totc)^2*totc; end % Normalize weighting <= 1, take root and multiply by 100 imbalance=100*sqrt(imbalance/tot) % 53.730 per cent