How does Stockfish calculate advantage? 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?
Good Question! The reason that the evaluation changes when you put in the engine's best moves is because as you do this the computer gets to look deeper into the future. This is the same as if you were thinking for 10 minutes on a position and saw that it was a draw, but when you look for 30 minutes you get to a deeper conclusion realizing that it's actually a draw. Another evidence I have for this is when you let the computer think on a position for a long period of time(say 1 hour) It usually reaches a different evaluation than the initial evaluation. I hope it helps!