public RandomVariable [ ] getGradient ( ) { // for now let us take the case for output-dimension equal to one! int numberOfVariables = getNumberOfVariablesInList ( ) ; int numberOfCalculationSteps = factory . getNumberOfEntriesInList ( ) ; RandomVariable [ ] omega_hat = new RandomVariable [ numberOfCalculationSteps ] ; // first entry gets initialized omega_hat [ numberOfCalculationSteps - 1 ] = new RandomVariableFromDoubleArray ( 1.0 ) ; /* * TODO: Find way that calculations form here on are not 'recorded' by the factory * IDEA: Let the calculation below run on {@link RandomVariableFromDoubleArray}, ie cast everything down! * */ for ( int functionIndex = numberOfCalculationSteps - 2 ; functionIndex > 0 ; functionIndex -- ) { // apply chain rule omega_hat [ functionIndex ] = new RandomVariableFromDoubleArray ( 0.0 ) ; /*TODO: save all D_{i,j}*\omega_j in vector and sum up later */ for ( RandomVariableUniqueVariable parent : parentsVariables ) { int variableIndex = parent . getVariableID ( ) ; omega_hat [ functionIndex ] = omega_hat [ functionIndex ] . add ( getPartialDerivative ( functionIndex , variableIndex ) . mult ( omega_hat [ variableIndex ] ) ) ; } } /* Due to the fact that we can still introduce 'new' true variables on the fly they are NOT the last couple of indices! * Thus save the indices of the true variables and recover them after finalizing all the calculations * IDEA: quit calculation after minimal true variable index is reached */ RandomVariable [ ] gradient = new RandomVariable [ numberOfVariables ] ; /* TODO: sort array in correct manner! */ int [ ] indicesOfVariables = getIDsOfVariablesInList ( ) ; for ( int i = 0 ; i < numberOfVariables ; i ++ ) { gradient [ i ] = omega_hat [ numberOfCalculationSteps - numberOfVariables + indicesOfVariables [ i ] ] ; } return gradient ; }
Apply the AAD algorithm to this very variable