From 8f8ca027307b2183498260e6f73e907bf6f4b411 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Mon, 5 Sep 2022 15:58:08 +0200 Subject: [PATCH] tweak: get possibly working dmr function --- .../FunctionRegistry/Library/FR_Danger.res | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res index 8a180c13..9f80fb7a 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res @@ -214,7 +214,12 @@ module Internals = { let increment = funds /. numDivisions 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({ optimalAllocations: [0.0, 0.0], currentMarginalReturns: E.A.R.firstErrorOrOpen([ @@ -228,20 +233,20 @@ module Internals = { ) => { switch(acc){ | Ok(accInner) => { - let currentMarginalReturnsWrapped = accInner.currentMarginalReturns - let newAccWrapped = switch(currentMarginalReturnsWrapped){ - | Ok(currentMarginalReturns) => { - let biggestMarginalReturnsIndex = findBiggestMarginalReturnsIndex(currentMarginalReturns) + let oldMarginalReturnsWrapped = accInner.currentMarginalReturns + let newAccWrapped = switch(oldMarginalReturnsWrapped){ + | Ok(oldMarginalReturns) => { + let indexOfBiggestDMR = findBiggestElementIndex(oldMarginalReturns) let newOptimalAllocations = Belt.Array.copy(accInner.optimalAllocations) let newOptimalAllocationsi = - newOptimalAllocations[biggestMarginalReturnsIndex] +. newIncrement - newOptimalAllocations[biggestMarginalReturnsIndex] = newOptimalAllocationsi - let lambdai = biggestMarginalReturnsIndex == 0 ? lambda1: lambda2 // to do: generalize + newOptimalAllocations[indexOfBiggestDMR] +. newIncrement + newOptimalAllocations[indexOfBiggestDMR] = newOptimalAllocationsi + let lambdai = indexOfBiggestDMR == 0 ? lambda1: lambda2 // to do: generalize let newMarginalResultsLambdai = applyFunctionAtFloatToFloatOption(lambdai, newOptimalAllocationsi) let newCurrentMarginalReturns = switch(newMarginalResultsLambdai){ | Ok(value) => { - let result = Belt.Array.copy(currentMarginalReturns) - result[biggestMarginalReturnsIndex] = value + let result = Belt.Array.copy(oldMarginalReturns) + result[indexOfBiggestDMR] = value Ok(result) } | Error(b) => Error(b)