<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Demo of the two thirds library
This notebook gives a demo of the two thirds library which can be used to analyse runnings of the two thirds library.
To install the library you can run pip install twothirds or get the git repository here.
A basic single game
Step1:  Let as assume we have the following list of random guesses
Step2:  Now we create a single game instance
Step3:  Let's find the two thirds of the average
Step4:  We can identify the winning guess
Step6:  Note that the data could also be in the form of a dictionary that maps names of players to guesses
Step7:  We see that quite a few people won.
Step8:  Handling data
Note that it might be much easier to collect the data in a spreadsheet. This library allows for that. Let's first write the data file we will be using (in effect doing things backwords).
Step9:  We can now read in this data
Step10:  The data file has a dataframe attribute
Step11:  We can get the data in a nicer format and ready for use. The format is a list of objects representing every play of the game (so for example we could have a file with muliple columns for each game).
Step12:  Here we create the game (as above)
Step13:  Managing an activity
If we have a spreadsheet with multiple columns for guesses we can read it in and create an activity that will contain all the data and analysis we need. Let's tweak our guesses to have a second guess that should be lower than the first.
Step14:  Here we write the data to file again
Step15:  We have still got access to the raw data
Step16:  We also have an instance for each game
Step17:  The winning guess for each game can be found below
Step18:  The winners of the first game
Step19:  The winners of the second game (there are more of them)
Step20:  The library has some inbuilt plots
Step21:  Finally you can see a summary of everything here
Step22:  Here is a larger example
<ASSISTANT_TASK:>
Python Code:
import twothirds 
import random
Explanation: Demo of the two thirds library
This notebook gives a demo of the two thirds library which can be used to analyse runnings of the two thirds library.
To install the library you can run pip install twothirds or get the git repository here.
A basic single game
End of explanation
N = 2000
guesses = [int(round(random.triangular(0, 100, 44), 0)) for k in range(N)]
Explanation: Let as assume we have the following list of random guesses:
End of explanation
g = twothirds.TwoThirdsGame(guesses)
Explanation: Now we create a single game instance
End of explanation
g.two_thirds_of_the_average()
Explanation: Let's find the two thirds of the average:
End of explanation
g.find_winner()
Explanation: We can identify the winning guess:
End of explanation
import string
def randomword(length):
    A function to generate a random name: http://stackoverflow.com/questions/2030053/random-strings-in-python
    return ''.join(random.choice(string.lowercase) for i in range(length))
guesses = {randomword(8):guess for guess in guesses}
g = twothirds.TwoThirdsGame(guesses)
g.two_thirds_of_the_average()
Explanation: Note that the data could also be in the form of a dictionary that maps names of players to guesses:
End of explanation
g.find_winner()
Explanation: We see that quite a few people won.
End of explanation
import pandas
df = pandas.DataFrame(guesses.items())
df.to_csv('demo.csv', index=False)
Explanation: Handling data
Note that it might be much easier to collect the data in a spreadsheet. This library allows for that. Let's first write the data file we will be using (in effect doing things backwords).
End of explanation
data = twothirds.Data('demo.csv')
data.read()
Explanation: We can now read in this data:
End of explanation
data.df.head()
Explanation: The data file has a dataframe attribute:
End of explanation
guesses = data.out()[0]
Explanation: We can get the data in a nicer format and ready for use. The format is a list of objects representing every play of the game (so for example we could have a file with muliple columns for each game).
End of explanation
g = twothirds.TwoThirdsGame(guesses)
g.find_winner()
Explanation: Here we create the game (as above):
End of explanation
guesses = [[key, guesses[key], int(random.triangular(0, guesses[key], 1.0 * guesses[key] / 3))] for key in guesses]
Explanation: Managing an activity
If we have a spreadsheet with multiple columns for guesses we can read it in and create an activity that will contain all the data and analysis we need. Let's tweak our guesses to have a second guess that should be lower than the first.
End of explanation
df = pandas.DataFrame(guesses)
df.to_csv('demo.csv', index=False)
activity = twothirds.Activity('demo.csv')
Explanation: Here we write the data to file again:
End of explanation
activity.raw_data.df.head()
Explanation: We have still got access to the raw data:
End of explanation
activity.games
Explanation: We also have an instance for each game:
End of explanation
[g.find_winner()[-1] for g in activity.games]
Explanation: The winning guess for each game can be found below:
End of explanation
activity.games[0].find_winner()[:-1]
Explanation: The winners of the first game:
End of explanation
activity.games[1].find_winner()[:-1]
Explanation: The winners of the second game (there are more of them):
End of explanation
%matplotlib inline
activity.analyse()
activity.distplot();
activity.pairplot();
Explanation: The library has some inbuilt plots:
End of explanation
activity
Explanation: Finally you can see a summary of everything here:
End of explanation
import twothirds
activity = twothirds.Activity('data.csv')
activity.analyse()
%matplotlib inline
activity.distplot();
activity.pairplot();
Explanation: Here is a larger example
End of explanation
<END_TASK>