tweak: get possibly working dmr function

This commit is contained in:
NunoSempere 2022-09-05 15:58:08 +02:00
parent b13da70c38
commit 8f8ca02730

View File

@ -214,7 +214,12 @@ module Internals = {
let increment = funds /. numDivisions let increment = funds /. numDivisions
let arrayOfIncrements = Belt.Array.makeBy(numDivisionsInt, _ => increment) let arrayOfIncrements = Belt.Array.makeBy(numDivisionsInt, _ => increment)
let findBiggestMarginalReturnsIndex = (_) => 0 let findBiggestElementIndex = (xs) => E.A.reducei(xs, 0, (acc, newElement, index) => {
switch(newElement > xs[acc]){
| true => index
| false => acc
}
})
let initAccumulator: diminishingReturnsAccumulator = Ok({ let initAccumulator: diminishingReturnsAccumulator = Ok({
optimalAllocations: [0.0, 0.0], optimalAllocations: [0.0, 0.0],
currentMarginalReturns: E.A.R.firstErrorOrOpen([ currentMarginalReturns: E.A.R.firstErrorOrOpen([
@ -228,20 +233,20 @@ module Internals = {
) => { ) => {
switch(acc){ switch(acc){
| Ok(accInner) => { | Ok(accInner) => {
let currentMarginalReturnsWrapped = accInner.currentMarginalReturns let oldMarginalReturnsWrapped = accInner.currentMarginalReturns
let newAccWrapped = switch(currentMarginalReturnsWrapped){ let newAccWrapped = switch(oldMarginalReturnsWrapped){
| Ok(currentMarginalReturns) => { | Ok(oldMarginalReturns) => {
let biggestMarginalReturnsIndex = findBiggestMarginalReturnsIndex(currentMarginalReturns) let indexOfBiggestDMR = findBiggestElementIndex(oldMarginalReturns)
let newOptimalAllocations = Belt.Array.copy(accInner.optimalAllocations) let newOptimalAllocations = Belt.Array.copy(accInner.optimalAllocations)
let newOptimalAllocationsi = let newOptimalAllocationsi =
newOptimalAllocations[biggestMarginalReturnsIndex] +. newIncrement newOptimalAllocations[indexOfBiggestDMR] +. newIncrement
newOptimalAllocations[biggestMarginalReturnsIndex] = newOptimalAllocationsi newOptimalAllocations[indexOfBiggestDMR] = newOptimalAllocationsi
let lambdai = biggestMarginalReturnsIndex == 0 ? lambda1: lambda2 // to do: generalize let lambdai = indexOfBiggestDMR == 0 ? lambda1: lambda2 // to do: generalize
let newMarginalResultsLambdai = applyFunctionAtFloatToFloatOption(lambdai, newOptimalAllocationsi) let newMarginalResultsLambdai = applyFunctionAtFloatToFloatOption(lambdai, newOptimalAllocationsi)
let newCurrentMarginalReturns = switch(newMarginalResultsLambdai){ let newCurrentMarginalReturns = switch(newMarginalResultsLambdai){
| Ok(value) => { | Ok(value) => {
let result = Belt.Array.copy(currentMarginalReturns) let result = Belt.Array.copy(oldMarginalReturns)
result[biggestMarginalReturnsIndex] = value result[indexOfBiggestDMR] = value
Ok(result) Ok(result)
} }
| Error(b) => Error(b) | Error(b) => Error(b)