Published
Edited
Aug 19, 2020
Insert cell
Insert cell
Insert cell
function generateMatrix( n=12, start=1) { // this is most basic way of calculating
var matrix = []
//matrix.push(0) // to index by n not n-1
for (let x=start; x<n+1; x++) { // for every number
let row = []
for(let y=1; y< Math.floor(x+1); y++) {
if (x%y == 0) // if divisible with no remainder
row.push(y)
}
let denominator = row.reduce( (x,y) => (x+y) ) // bottom of fraction = sum of divisors
let weightedRow = row.map( (z) => z/denominator )
// matrix.push(weightedRow)
matrix.push( {payoutTokens: row, payoutAmounts: weightedRow} )
}
return matrix
}(12,5)
Insert cell
Insert cell
function generateMatrix( n=12) {
var matrix = []
//matrix.push(0) // to index by n not n-1
for (let x=1; x<n+1; x++) { // for every number
let row = []
for(let y=1; y< Math.floor(x+1); y++) {
if (x%y == 0) // if divisible with no remainder
row.push(y)
else
row.push(0)
}
let denominator = row.reduce( (x,y) => (x+y) ) // bottom of fraction = sum of divisors
let weightedRow = row.map( (z) => z/denominator )
// matrix.push(weightedRow)
matrix.push( weightedRow )
}
// calculate sums for each token....
var sums = []
for (let t=1; t<n+1; t++) { // for each token
let sum = 0
for(let e=t; e<n+1; e++) { // for each epoch (starting with epoch token was minted
sum += matrix[e-1][t-1]
}
sums.push( sum/n *100)
}
// sums = sums.map( x => x/n*100 ) // this is same as doing when pushing see above
return {tokenReturnsPerEpoch: matrix, sums}
}(10)
Insert cell
md `As we are working with trianglular array we can flatted it to a single dimension and index by the triangular numbers`

Insert cell
function triangle( n ) { // returns the triangular number for n (total size of array)
return (n*(n+1)/2)
}
Insert cell
function triangleIndex( n ) { //
let end = triangle(n)
return [ triangle(n-1), end-1 ]
}
Insert cell
Insert cell
Insert cell
Insert cell
function getFactors( n ) { // returns factors for row n (by slicing not efficient!)
let [x,y] = triangleIndex( n )
return generateFlattenedTriangleArrayOfWeights( n ).slice(x,y+1)
}(1) // .filter( (x) => x != 0 )
Insert cell
function getWeights(n=12) { // gets weight for specific dimension (more efficient than above)
let array = []
for(let y=1; y< n+1; y++) {
if (n%y == 0) // if divisible with no remainder
array.push(y/ sumOfFactors(n) )
else
array.push(0)
}
return array
}
Insert cell
function tokenReturns( n=10 ) {
// calculate token returns for specific token
let tokenReturns = [];
// get array of all weights
let array = generateFlattenedTriangleArrayOfWeights(n)
// find start and end position in array for token n

for( let token=1; token<n+1; token++ ) {
let [x,y] = triangleIndex( token ) // get's index for first and last item in row
let index = y-x
for( let row=token; row<n+1; row++) { // start from end of row == y and
// for first round sum x
let p=y+index // pointer
tokenReturns.push( {token, row, x, y, p, y, index } )
index+=row
}
}
return {tokenReturns,array}
}(5)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more