Answers.Body: <p>Just googling for "castling statistics" already yields a few results; even though the analysis is done on a different set of games than 'all grandmaster games', it gives a rough indication.</p>
  
  <ul>
  <li><a href="https://www.reddit.com/r/chess/comments/a32glt/castling_statistics/" rel="noreferrer">50 world class players</a></li>
  <li><a href="https://blog.ebemunk.com/a-visual-look-at-2-million-chess-games/" rel="noreferrer">2 million games in the MillionBase database</a></li>
  </ul>
  
  <p><strong>Kingside castling</strong> is done by 80-81% of the players, and <strong>queen side castling</strong> by 8-9%. I estimate that <strong>Never castled by the end of the game but still has the right to castle</strong> is negligible (less than 0.1%, since it requires a very short game). So that leaves about 11% for the final group <strong>Lost the right to castle</strong>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T13:08:34.573Z
Answers.Text: The first link is great!! I have never seen such interesting statistics on chess before.
Answers.UserDisplayName: Zuriel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T17:04:34.113Z
Answers.Text: It's less common these days, but historically, there were _many_ games where two grandmasters would play a few standard opening moves and agree a draw, for example, because the result of their game wasn't going to affect the tournament standings. Probably many of these games ended in a position where at least one of the players still had the right to castle. The frequency plot for different ply depths in your second link contains more than 35,000 games (~2%) that ended after 20 ply or less and nearly all of those will be agreed draws, probably with at least one player having castling rights.
Answers.UserDisplayName: David Richerby
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T07:18:36.000Z
Answers.LastActivityDate: 2019-04-18T07:18:36.000Z
Answers.UserDisplayName: Glorfindel
Answers.Body: <p>If you are prepared to use standard Linux command-line tools like <code>wc</code> and <code>grep</code> then I think my free PGN processor, <a href="https://www.cs.kent.ac.uk/~djb/pgn-extract/" rel="noreferrer">pgn-extract</a>, will do much of the pre-processing necessary to count games in each category. Below is a basic bash script I put together as a proof of concept. It assumes your file of games is called <code>inputfile.pgn</code> - adjust as necessary, or pass it in as a command-line argument:</p>
  
  <pre><code>#!/bin/bash
  # Output stats on castling.
  src=inputfile.pgn
  # Pre-process the games to allow textual analysis.
  pgn-extract -C -V -N --notags --nochecks --linelength 10000 -s -o stripped.pgn ${src}
  # Castling by white: detect a preceding move number.
  grep '\. O-O ' stripped.pgn &gt; Wkingside.pgn
  grep '\. O-O-O ' stripped.pgn &gt; Wqueenside.pgn
  # Castling by black: detect no preceding move number.
  grep '[^.] O-O ' stripped.pgn &gt; Bkingside.pgn
  grep '[^.] O-O-O ' stripped.pgn &gt; Bqueenside.pgn
  # Find games in which no castling occurred.
  grep -v ' O-O ' stripped.pgn | grep -v ' O-O-O ' | grep -v '^$' &gt; nocastle.pgn
  
  # Output the FEN of the final position to include castling rights and
  # look for the presence of rights.
  pgn-extract -C -V -N --notags --nomovenumbers --linelength 10000 -s nocastle.pgn --dropply -1 -F | grep ' [wb] [KkQq]' &gt; rights.txt
  
  # Output the results - assumes valid Result tags in the source.
  echo 'Number of games: ' `grep '^\[Result ' ${src} | wc -l`
  echo 'W Kingside: ' `cat Wkingside.pgn | wc -l`
  echo 'W Queenside: ' `cat Wqueenside.pgn | wc -l`
  echo 'B Kingside: ' `cat Bkingside.pgn | wc -l`
  echo 'B Queenside: ' `cat Bqueenside.pgn | wc -l`
  echo 'Neither: ' `cat nocastle.pgn | wc -l`
  echo 'Rights retained: ' `cat rights.txt | wc -l`
  </code></pre>
  
  <p>I tried the script on the <a href="https://www.kingbase-chess.net/" rel="noreferrer">KingBase database</a> (Apr 2019) and, after removing broken games and duplicates got the following stats:</p>
  
  <pre><code>Number of games:  2072354
  W Kingside:  1633762
  W Queenside:  246158
  B Kingside:  1670870
  B Queenside:  128124
  Neither:  69776
  Rights retained:  24280
  </code></pre>
  
  <p>The 'neither' figure is about 3.4% while the 'rights retained' figure is about 1.2%. If games with 20 or fewer ply are removed then the 'neither' figure reduces to about 2.9% and the 'rights retained' figure to about 0.7%.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T12:51:00.300Z
Answers.Text: Thank you so much for sharing!! I believe that your code works great (unless one side castles more than once, which is illegal anyway).
Answers.UserDisplayName: Zuriel
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T13:32:57.670Z
Answers.Text: Great! Really digging this new trend of script-based answers for stats questions here on chess SE, like yours here or this [one recently](https://chess.stackexchange.com/a/24177/15078).
Answers.UserDisplayName: user929304
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T17:44:21.273Z
Answers.Text: So, to clarify, "Kingside" counts all those games in which one or both players castled kingside.  Games in which one player castled kingside and the other castled queenside will be counted in both categories.  Perhaps a more interesting statistic would be the fraction of games in which *White* (or respectively *Black*) castled kingside or queenside.
Answers.UserDisplayName: Nate Eldredge
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T19:26:14.037Z
Answers.Text: I shouldn't think it would affect the percentages much, but 0-0+ and 0-0-0+ have occasionally happened.
Answers.UserDisplayName: Rosie F
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T20:24:51.050Z
Answers.Text: @RosieF that is a good comment about checks so I have added --nochecks to the arguments to pgn-extract in the script and updated the game numbers. There were about 5,000 games affected so the 'neither' percentage is lower than before.
Answers.UserDisplayName: kentdjb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T20:56:58.673Z
Answers.Text: @RewanDemontay The --nochecks filter with pgn-extract will eliminate both '+' and '#' annotations in the above script. At the moment, I cannot think of a straight forward way to count the White and Black moves separately without a lot of contortion.
Answers.UserDisplayName: kentdjb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-19T08:33:04.557Z
Answers.Text: I have now corrected an error in the counting of 'rights retained' and found a straightforward way to count black and white moves separately.
Answers.UserDisplayName: kentdjb
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-20T18:29:28.163Z
Answers.Text: Why not `$(wc -l fbame)`?
Answers.UserDisplayName: D. Ben Knoble
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-18T08:25:01.373Z
Answers.LastActivityDate: 2019-04-19T08:31:25.360Z
Answers.LastEditDate: 2019-04-19T08:31:25.360Z
Answers.UserDisplayName: kentdjb
Body: <p>May I know how to find in all grandmaster games, what the percentage of each of the following is?</p>
  
  <ul>
  <li>Kingside castling</li>
  <li>Queenside castling</li>
  <li>Never castled by the end of the game but still has the right to castle</li>
  <li>Lost the right to castle</li>
  </ul>
  
  <p><strong>Note</strong> When one side has <a href="http://www.chess-poster.com/english/mail/mail_2002/three_castlings.htm" rel="noreferrer">castled more than once in a game</a>, only the first castle counts.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-18T05:20:58.587Z
Comments.Text: By "all grandmaster games" you mean all such games in a certain database you have?
Comments.UserDisplayName: user1583209
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-18T05:22:28.210Z
Comments.Text: Do you need this separately for white and black? (Percentage of white kingside castling, etc?)
Comments.UserDisplayName: user1583209
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-18T12:46:55.443Z
Comments.Text: @user1583209, I do not have  any certain database in my mind. If the percentage is available separately for white and black, it will be preferred.
Comments.UserDisplayName: Zuriel
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-19T04:26:04.793Z
Comments.Text: No player may ever castle twice in a game, since castling and then moving the king back to a position from which it could castle again requires at least one king move, disqualifying it from ever castling again.
Comments.UserDisplayName: user45266
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-04-19T06:58:08.107Z
Comments.Text: @user45266 It's an illegal move, but as per OP's link, some historical games do contain illegal moves that nobody caught at the time.
Comments.UserDisplayName: G_B
Tags: <statistics><castling><grandmaster>
Title: How often does castling occur in grandmaster games?
ThreadText: # How often does castling occur in grandmaster games?
  
  (Asked by: username_0 on 2019-04-18)
  
  May I know how to find in all grandmaster games, what the percentage of each of the following is?
  
  * Kingside castling
  * Queenside castling
  * Never castled by the end of the game but still has the right to castle
  * Lost the right to castle
  
  **Note** When one side has [castled more than once in a game](http://www.chess-poster.com/english/mail/mail_2002/three_castlings.htm), only the first castle counts.
  
  **Tags:** statistics, castling, grandmaster
  
  > **Comments:**
  > **username_1**: By "all grandmaster games" you mean all such games in a certain database you have?  
  > **username_1**: Do you need this separately for white and black? (Percentage of white kingside castling, etc?)  
  > **username_0**: @username_1, I do not have  any certain database in my mind. If the percentage is available separately for white and black, it will be preferred.  
  > **username_2**: No player may ever castle twice in a game, since castling and then moving the king back to a position from which it could castle again requires at least one king move, disqualifying it from ever castling again.  
  > **username_3**: @username_2 It's an illegal move, but as per OP's link, some historical games do contain illegal moves that nobody caught at the time.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_6 on 2019-04-18. Score: 32 | Accepted answer
  
  If you are prepared to use standard Linux command-line tools like wc and grep then I think my free PGN processor, pgn-extract, will do much of the pre-processing necessary to count games in each category. Below is a basic bash script I put together as a proof of concept. It assumes your file of games is called inputfile.pgn - adjust as necessary, or pass it in as a command-line argument:
  #!/bin/bash
  # Output stats on castling.
  src=inputfile.pgn
  # Pre-process the games to allow textual analysis.
  pgn-extract -C -V -N --notags --nochecks --linelength 10000 -s -o stripped.pgn ${src}
  # Castling by white: detect a preceding move number.
  grep '\. O-O ' stripped.pgn > Wkingside.pgn
  grep '\. O-O-O ' stripped.pgn > Wqueenside.pgn
  # Castling by black: detect no preceding move number.
  grep '[^.] O-O ' stripped.pgn > Bkingside.pgn
  grep '[^.] O-O-O ' stripped.pgn > Bqueenside.pgn
  # Find games in which no castling occurred.
  grep -v ' O-O ' stripped.pgn | grep -v ' O-O-O ' | grep -v '^$' > nocastle.pgn
  
  # Output the FEN of the final position to include castling rights and
  # look for the presence of rights.
  pgn-extract -C -V -N --notags --nomovenumbers --linelength 10000 -s nocastle.pgn --dropply -1 -F | grep ' [wb] [KkQq]' > rights.txt
  
  # Output the results - assumes valid Result tags in the source.
  echo 'Number of games: ' `grep '^\[Result ' ${src} | wc -l`
  echo 'W Kingside: ' `cat Wkingside.pgn | wc -l`
  echo 'W Queenside: ' `cat Wqueenside.pgn | wc -l`
  echo 'B Kingside: ' `cat Bkingside.pgn | wc -l`
  echo 'B Queenside: ' `cat Bqueenside.pgn | wc -l`
  echo 'Neither: ' `cat nocastle.pgn | wc -l`
  echo 'Rights retained: ' `cat rights.txt | wc -l`
  
  I tried the script on the KingBase database (Apr 2019) and, after removing broken games and duplicates got the following stats:
  Number of games:  2072354
  W Kingside:  1633762
  W Queenside:  246158
  B Kingside:  1670870
  B Queenside:  128124
  Neither:  69776
  Rights retained:  24280
  
  The 'neither' figure is about 3.4% while the 'rights retained' figure is about 1.2%. If games with 20 or fewer ply are removed then the 'neither' figure reduces to about 2.9% and the 'rights retained' figure to about 0.7%.
  
  > **Comments:**
  > **username_0**: Thank you so much for sharing!! I believe that your code works great (unless one side castles more than once, which is illegal anyway).  
  > **username_7**: Great! Really digging this new trend of script-based answers for stats questions here on chess SE, like yours here or this [one recently](https://chess.stackexchange.com/a/24177/15078).  
  > **username_8**: So, to clarify, "Kingside" counts all those games in which one or both players castled kingside.  Games in which one player castled kingside and the other castled queenside will be counted in both categories.  Perhaps a more interesting statistic would be the fraction of games in which *White* (or respectively *Black*) castled kingside or queenside.  
  > **username_9**: I shouldn't think it would affect the percentages much, but 0-0+ and 0-0-0+ have occasionally happened.  
  > **username_6**: @RosieF that is a good comment about checks so I have added --nochecks to the arguments to pgn-extract in the script and updated the game numbers. There were about 5,000 games affected so the 'neither' percentage is lower than before.  
  > **username_6**: @RewanDemontay The --nochecks filter with pgn-extract will eliminate both '+' and '#' annotations in the above script. At the moment, I cannot think of a straight forward way to count the White and Black moves separately without a lot of contortion.  
  > **username_6**: I have now corrected an error in the counting of 'rights retained' and found a straightforward way to count black and white moves separately.  
  > **username_10**: Why not `$(wc -l fbame)`?  
  
  ---
  
  ### Answer by username_4 on 2019-04-18. Score: 11
  
  Just googling for "castling statistics" already yields a few results; even though the analysis is done on a different set of games than 'all grandmaster games', it gives a rough indication.
  
  * [50 world class players](https://www.reddit.com/r/chess/comments/a32glt/castling_statistics/)
  * [2 million games in the MillionBase database](https://blog.ebemunk.com/a-visual-look-at-2-million-chess-games/)
  
  **Kingside castling** is done by 80-81% of the players, and **queen side castling** by 8-9%. I estimate that **Never castled by the end of the game but still has the right to castle** is negligible (less than 0.1%, since it requires a very short game). So that leaves about 11% for the final group **Lost the right to castle**.
  
  > **Comments:**
  > **username_0**: The first link is great!! I have never seen such interesting statistics on chess before.  
  > **username_5**: It's less common these days, but historically, there were _many_ games where two grandmasters would play a few standard opening moves and agree a draw, for example, because the result of their game wasn't going to affect the tournament standings. Probably many of these games ended in a position where at least one of the players still had the right to castle. The frequency plot for different ply depths in your second link contains more than 35,000 games (~2%) that ended after 20 ply or less and nearly all of those will be agreed draws, probably with at least one player having castling rights.  
  
  ---
  
  