public Map < Integer , RandomVariable > getGradient ( ) { int numberOfCalculationSteps = getFunctionList ( ) . size ( ) ; RandomVariable [ ] omegaHat = new RandomVariable [ numberOfCalculationSteps ] ; omegaHat [ numberOfCalculationSteps - 1 ] = new RandomVariableFromDoubleArray ( 1.0 ) ; for ( int variableIndex = numberOfCalculationSteps - 2 ; variableIndex >= 0 ; variableIndex -- ) { omegaHat [ variableIndex ] = new RandomVariableFromDoubleArray ( 0.0 ) ; ArrayList < Integer > childrenList = getAADRandomVariableFromList ( variableIndex ) . getChildrenIndices ( ) ; for ( int functionIndex : childrenList ) { RandomVariable D_i_j = getPartialDerivative ( functionIndex , variableIndex ) ; omegaHat [ variableIndex ] = omegaHat [ variableIndex ] . addProduct ( D_i_j , omegaHat [ functionIndex ] ) ; } } ArrayList < Integer > arrayListOfAllIndicesOfDependentRandomVariables = getArrayListOfAllIndicesOfDependentRandomVariables ( ) ; Map < Integer , RandomVariable > gradient = new HashMap < Integer , RandomVariable > ( ) ; for ( Integer indexOfDependentRandomVariable : arrayListOfAllIndicesOfDependentRandomVariables ) { gradient . put ( indexOfDependentRandomVariable , omegaHat [ arrayListOfAllIndicesOfDependentRandomVariables . get ( indexOfDependentRandomVariable ) ] ) ; } return gradient ; }
Implements the AAD Algorithm