Recently when I was on Lichess, somebody pointed out in chat that Stockfish calculated advantage based on what it considered as the best move for both sides. However, when we play the best moves (moves suggested by Stockfish), the advantage changes, which means that the advantage cannot be solely calculated assuming best moves.
However, the advantage cannot be calculated based on some kind of weighted mean of the possible moves either since Stockfish clearly spots winning strategies such as 'Mate in n' for sufficiently small n.
To make the previous paragraph clear, assume that white has mate in 1, and assume that white has 2 legal moves. Also assume that if white doesn't play the move of checkmate, then black has a forced move that is also mate. In this scenario, if we were taking average, advantage would be zero since random moves would give 50% win chance for white and 50% for black. However, Stockfish would show #1.
So how does Stockfish calculate advantage?
EDIT : This question is not asking how the engine finds the best moves. I am familiar with the decision tree method that engines implement to do so. The question is about the 'advantage' value that the engine evaluates. @SubhanKhan 's comment clarifies why the advantage value changes after performing the best move. Does the engine evaluate advantage solely based on the best moves then?