Mans.hu

Of life and all its colors

About everything that amazes and confounds me.

About / Blog / Contact
  • 
  • 
  • 
  • 

Categories

  • Amazing
  • Books
  • Business and Technology
  • Chess
  • History
  • Mahabharata
  • Parenting

Search

How to create a normal curve in R?

April 17, 2019 by manshu Leave a Comment

It is surprisingly easy to create a normal curve in R. I was curious to see how many moves my games last for, and the distribution around it. I used the data frame I have created for classical games, and this is the code you need to create the curve:

normal_curve <- ggplot(tal, aes(x = NMoves))+
+ stat_function (fun = dnorm, color = “red”, args = list(mean = mean(tal$NMoves), sd = sd(tal$NMoves)))

You have to specify the variable name for which the curve has to be created, and then specify it again to calculate the mean and the standard deviation as shown above. Running this code will create a normal curve for you as shown below.

R - Normal Curve

Filed Under: Business and Technology

How to write a Nested Ifelse in R?

April 17, 2019 by manshu Leave a Comment

With the database of all my classical games created – I wanted to add two columns to the data frame to indicate what color I was in the game, and the result. Here is the data frame in a CSV format so you can understand the structure.

In order to determine whether I was black or white – I can simply evaluate if my username was present in the White or Black variable, and assign a color based on that.

This is the code that does that:

a <- ifelse(tal$White == “manshuv”, “White”, “Black”)

I first tried writing an If statement but R doesn’t evaluate an If statement on the entire vector rather it does so only on the first record. In order to run the statement on the entire vector you need to use the Ifelse statement, and that is the statement above.

The next thing I wanted to do was to record my wins and losses, and for that you need a slightly more involved Ifelse statement in the form of a nested statement.

This is the code for that:

Result_1 <- ifelse (tal$Result == “1/2-1/2”, “Draw”,
+ (ifelse(tal$White == “manshuv” &tal$Result == “1-0”, “Won”,
+ (ifelse(tal$Black == “manshuv” & tal$Result == “0-1”, “Won”, “Lost”)))))

In the first line of the code above – R evaluates if the result was 1/2-1/2, and if so it assigns Draw as the result.

The second check is to see if the color was White and the result was 1-0 as that is a win, and if that fails then check if the color was black and the result was 0-1 and that is a win for me. All other outcomes are my losses.

You can then add these two columns to the data frame by using the mutate command as follows:

tal <- mutate(tal, Result_1)

Filed Under: Business and Technology

How to create a database of your games using R bigchess?

April 15, 2019 by manshu Leave a Comment

I have recently started learning R with the help of the Data Science Program by HarvardX. I think this is a great program, and has been very helpful to me in learning R.

As I looked for exercises to practice on my own – I searched to see if there are any chess related packages in R that could be of use. I found bigchess, and I have been playing with it since yesterday, and I really like this package.

In this post I am going to describe how to create a data frame of all your chess games in R, and then use them for analysis.

Install bigchess

Installing bigchess is quite easy as is installing other R packages. You need to run the following two commands:

install.packages(“bigchess”)

library(“bigchess”)

bigchess has the ability to read through a PGN of all your games, and create a data frame out of it. This is really impressive, and I was quite surprised to find that such a package existed.

If you play online on Lichess then you can retrieve a PGN that contains all your games by using the export option on your profile.

I currently have a database of 900 or so games, and I exported all of that. You need to save this file in your users folder in order for the extraction to work. I am sure this is not the only way and you can specify a path, but since I am still learning – this is the only way I know. Rather annoyingly, the users folder is hidden on a Mac, and you have to cmd-shift-h in Finder in order to see it.

Create a data frame from your PGN

Now that you have a database of all your games – the next step is to create a data frame of these games in R.

By default, bigchess only supports five tags so when you import the games with the default command it skips over the ELO of both players. This is quite important to me, and in order to retain the ELOs you have to add them to the command while importing the games.

Here is the code for that:

tal <- read.pgn(“lichess_manshuv_2019-04-14.pgn”, add.tags = c(“WhiteElo”, “BlackElo”))

In the statement above – tal is the name of my dataframe, and lichess_manshuv_2019-04-14.pgn is the name of the file with all my games. Since the default statement without the add.tags only captures five tags, I added the WhiteElo and BlackElo to also record the Elos of the players. This command gives a warning, but adds all the games. To be honest, I am not sure what the coercion warning means here because I expected that all games would have corresponding Elos.

This is how the statement executed in R studio:

> tal <- read.pgn(“lichess_manshuv_2019-04-14.pgn”, add.tags = c(“WhiteElo”, “BlackElo”))
2019-04-14 18:15:04, successfully imported 901 games
2019-04-14 18:15:04, N moves computed
2019-04-14 18:15:04, extract moves done
2019-04-14 18:15:05, stat moves computed
Warning messages:
1: In read.pgn(“lichess_manshuv_2019-04-14.pgn”, add.tags = c(“WhiteElo”, :
NAs introduced by coercion
2: In read.pgn(“lichess_manshuv_2019-04-14.pgn”, add.tags = c(“WhiteElo”, :
NAs introduced by coercion

Now, I want to see whether these games were loaded correctly or not, and in order to do that you can use the very helpful command in R Studio – View() so that you can see the data in a tabular format.
The command is:

View(tal)

Personally, I am only interested in the analysis of my classical games on Lichess so I filtered my data frame on that, but you don’t need to do that. Here is the command for that.

tal <- filter(tal, Event == “Rated Classical game”)

Information about your bigchess R data frame

I was quite confused by this data frame when I first saw it, and I was rather amazed by it when I understood it!

bigchess creates a data frame with 51 variables! What are these 51 columns?!

bigchess extracts everything out of your PGN (excluding the tags you didn’t add), and then creates a few extra columns with this data that you didn’t think of yourself. It stores the total number of moves, total number of moves for each piece, the first ten moves of white and black in individual columns, and of course the entire PGN of each and every game. This is very helpful for opening analysis, and other type of analysis as well.

Once you have the data frame you can use other R goodness to conduct analysis, create graphs etc. The strength of the bigchess package is in extracting all your games and providing it in a data frame that is extremely powerful, and amenable to analysis. There are some other features of this package as well, but I have not explored them yet. I will do future posts on them when I use them.

Analysis with R

Time for some analysis now! This is a simple chart that I created to see how my opponents were rated when I was black.

My Opponent's ELO Rating
My Opponent’s ELO Rating

Code:

data <- filter(tal, Black == “manshuv”)
>ggplot(data, aes(x=WhiteElo)) +
+ geom_histogram(fill=”skyblue”, alpha=0.5) +
+ ggtitle(“White’s Opening Move”) +
+ theme_minimal()

This was fairly simple, and now I’d like to see how many times my opponent played each opening against me. I know e4 must figure as the most popular, but don’t know what the distribution is. This is slightly trickier because the column W1 which holds white’s first move contains alphanumeric values such as e4, d4 etc. and you can’t use the code given above straightaway. You may think you need to aggregate this data, but you don’t – you just need to use geom_bar instead of geom_histogram which is quite fantastic! 

This yields the following chart:

White - Opening Move

This can be achieved with the following code:

ggplot(data, aes(x=W1)) +
+ geom_bar(fill=”skyblue”, alpha=0.5) +
+ ggtitle(“White’s Opening Move”) +
+ theme_minimal()

As you can see and probably expected – e4 is played much more frequently than anything else. I didn’t think that it was played twice as many times as d4 though, so this is interesting to me.

Finally, I am very happy to have found the big chess  package, and be able to combine two of my hobbies to learn something new in both of them at the same time! I am certainly hopeful that this will be the first of many more posts on R, and that R may even help my chess!

Filed Under: Business and Technology

What is BPM (Business Process Management)?

December 4, 2016 by manshu Leave a Comment

I have worked in the BPM (Business Process Management) area for a number of years now, and I have heard a lot of definitions of BPM, and I’ve also defined it myself several times mostly as a way to explain it to others who aren’t familiar with the term.

The simplest and most holistic definition I have come up with takes a three step approach to defining BPM.

First, you have to start by defining a workflow.

Workflow: A workflow entails moving a piece of work or information from a person to person, person to machine, machine to person, or machine to machine. For many years, I didn’t think of moving anything other than work, but then I worked with a lot of Settlement systems and saw workflows which were focused purely on moving money.

Upon some reflection, I felt what you are truly moving when you move money is information about ownership of that money, and I think work and information are nice umbrella terms that convey our meaning very clearly.

Now that you have defined a workflow, you can move on to define a business process.

Business Process: A business process is when one or more workflows come together to achieve a goal.

You can argue that all workflows are business processes, but I would counter that some manual and system tasks like collecting mail from the mail room, or taking data from one set of tables and posting them to another while necessary to perform are not really business processes in themselves.

Finally, you are ready to define BPM.

BPM (Business Process Management):  BPM is the way to make a business process more efficient or adaptable.

I used to think that automation is very important but over the years I have realized that automation is a means to the end, and the end is efficiency. If a company could make their process more efficient without IT, heaven knows no one would spend a dime on software.

Adaptability is a recent addition to my definition because I have encountered a few refactoring projects lately whose sole purpose is to move code to a newer version with eyes on improvement in the future.

I feel that this definition is simple enough that everyone understands it, and at the same time it captures the essence of BPM correctly and wholly.

If you have any questions or clarifications to add, go ahead and leave a comment, and I’ll be sure to respond to it.

Filed Under: Business and Technology

The Ultimatum Game

October 28, 2016 by manshu Leave a Comment

Learned about this interesting experiment that scoffs at the idea of rational beings. I especially enjoyed the part about results in particular tribes.

Imagine that someone offers you and some other anonymous person $100 to share. The rules are strict and known to both players. The two of you are in separate rooms and cannot exchange information. A coin toss decides which of you will propose how to share the money. If you are the proposer you can make a single offer of how to split the sum, and the other person—the responder—can say yes or no. If the responder’s answer is yes, the deal goes ahead. If the answer is no, neither of you gets anything. In both cases, the game is over and will not be repeated. What will you do?
Instinctively, many people feel they should offer 50 percent, because such a division is “fair” and therefore likely to be accepted. More daring people, however, think they might get away with offering somewhat less than half of the sum.
You may not be surprised to learn that two-thirds of the offers are between 40 and 50 percent. Only four in 100 people offer less than 20 percent. Proposing such a small amount is risky, because it might be rejected. More than half of all responders reject offers that are less than 20 percent. But why should anyone reject an offer as “too small”? The responder has just two choices: take what is offered or receive nothing. The only rational option for a selfish individual is to accept any offer. A selfish proposer who is sure that the responder is also selfish will therefore make the smallest possible offer and keep the rest. This game-theory analysis, which assumes that people are selfish and rational, tells you that the proposer should offer the smallest possible share and the responder should accept it. But this is not how most people play the game.

Filed Under: Amazing, Business and Technology

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • …
  • 7
  • Next Page »