Skip to content

Commit

Permalink
Moved Maclaurin Series degree for e^x approximation to be a parameter…
Browse files Browse the repository at this point in the history
… in the smart contract
  • Loading branch information
u-hubar committed Jan 16, 2024
1 parent 72fdcc3 commit cb4db12
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/service/proximity-scoring-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ class ProximityScoringService {
}

// Using Maclaurin Series to approximate e^x
_approximateExp(x, precision = 20) {
_approximateExp(x, degree) {
let xPow = x;
let factorial = 1;
let result = 1;

for (let i = 1; i <= precision; i += 1) {
for (let i = 1; i <= degree; i += 1) {
factorial *= i;
result += xPow / factorial;
xPow *= x;
Expand All @@ -122,7 +122,8 @@ class ProximityScoringService {
async LinearLogisticSum(blockchain, distance, stake, maxNeighborhoodDistance) {
const linearLogisticSumParams =
await this.blockchainModuleManager.getLinearLogisticSumParams(blockchain);
const { distanceScaleFactor, exponentMultiplier, x0, w1, w2 } = linearLogisticSumParams;
const { distanceScaleFactor, exponentMultiplier, maclaurinSeriesDegree, x0, w1, w2 } =
linearLogisticSumParams;

let dividend = distance;
let divisor = maxNeighborhoodDistance;
Expand All @@ -136,7 +137,7 @@ class ProximityScoringService {
parseFloat(divResult.toString()) / parseFloat(distanceScaleFactor.toString());

const exponentPart = exponentMultiplier * (stake - x0);
const mappedStake = 2 / (1 + this._approximateExp(exponentPart)) - 1;
const mappedStake = 2 / (1 + this._approximateExp(exponentPart, maclaurinSeriesDegree)) - 1;

const proximityScore = w1 * (1 - mappedDistance);
const stakeScore = w2 * mappedStake;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BlockchainModuleManagerMock {
return {
distanceScaleFactor: '1000000000000000000',
exponentMultiplier: -0.000001,
maclaurinSeriesDegree: 20,
x0: 50000,
w1: 1,
w2: 1,
Expand Down

0 comments on commit cb4db12

Please sign in to comment.