Understanding WPA2 authentication in details
The following answers are taken directly from the 802.11 standard (2012 version).


The PTK is generated as follows:
$$\mathrm{PTK} \gets \operatorname{\mathrm{PRF-X}(PMK, ``pairwise \ key \ expansion", \\
min(AA, SPA) || max(AA,SPA)|| \\ min(ANonce, SNonce) || max(ANonce, SNonce)) } $$
where $\mathrm{AA}$ and $\mathrm{SPA}$ are the MAC addresses of the Authenticator and Supplicant, respectively.  The pseudorandom function $\operatorname{\mathrm{PRF-X}}$ is defined as follows for $X = 128,192,256,384,512$:

PRF-X(K,A,B):
   for i = 0 to (X + 159) / 160:
       Z = Z || HMAC-SHA1(K, A || 0 || B || i)
   return LeftBits(Z,0,X)

The MICs are computed using either HMAC-SHA1-128 or AES-CMAC-128 (which one to use is negotiated during the handshake).
The GTK is simply chosen at random by the AP.