Chi-Square Analysis: The Pokemon GO Survey.

My last math assignment was to conduct a survey on a certain topic and population sample. I chose the topic to be Pokemon GO, and the sample to be 2 of the classrooms at my school. After I collected the results, I had to present them to my teacher, along with high-level analysis on the results. For many questions, I had to check whether there was a correlation between 2 facts. For that, I discovered a mathematical way to do it: chi-square analysis.

Chi-square analysis is used to check if there is a correlation between the results of a survey. Here is the formula for it:

chisquare

Let me show you an example directly from the survey I conducted.

One of the things I wanted to find out was how much players liked Pokemon GO, to their level in Pokemon GO. This is the table I used:

0 10 20 30 40 Grand Total
5 2 1 3
6 1 2 3
7 1 1 2
8 1 1
9 2 2 1 1 6
10 1 1 2 4
Grand Total 1 7 8 1 2 19

Each row is the level range(10 is 10-19, 20 is 20-29), and each column is how much they like Pokemon GO.

You must now find the expected value for each cell of the table. To do so, simply multiply the column total with the row total, then dividing it by the grand total. Do this for each cell. Now that you have your expected values, do the formula for each cell, and since there is a sigma, add all of it up. Then, you should get your chi-square. What you need now is your P-value. I haven’t figured out how to calculate the P-value, so just enter your chi-square and degrees of freedom into a P-value calculator. You also need to know what the degrees of freedom are for the table. To find out, do this equation: (rowTotal – 1) * (columnTotal – 1).

I made a Google Slides presentation on the survey results. It is right below this text if you would like to see it.

For a handy chi-square calculator, click this link

Binary search and finding roots.

Binary search is one of my favourite algorithms. If you don’t know what it is, it is a searching algorithm with a logarithmic run-time and very simple logic, but it works only for sorted lists. Simply start in the middle element. If what you’re searching for is smaller then the middle element, go to the middle of the left side. If what you are searching for is bigger than the middle element, go to the middle of the right. Repeat this process until you find what you are looking for.

Surprisingly, this system can be used to find the square root of a number as we are searching for root in a sorted list of numbers on the number line. Suppose you want to find the square root of 9.

  • The middle of 0 and 9 is 4.5.
  • 4.5 squared is 20.25, not 9.
  • 9 is smaller than 20.25, so the square root of  9 should be smaller than 4.5.
  • 2.25, maybe? It is in the middle of 0 and 4.5.
  • However, 2.25 squared is 5.0625.

If you repeat that process over and over again(which I refuse to do), you will eventually reach 3, which is the correct answer.

In fact, you can even do this for cube roots, fourth roots, fifth roots, et cetera. Instead of checking the square of your current number each step, check the cube of the number power of 4, power of 5, power of… you get it.

I even created a program to execute this function, with the same logic I just explained. Down here is the gist for the program:

To use this program, click here.

Behold: The Online 4 Hours programming website!

As you may know, I love programming. And now, I am working on a webiste dedicated to the programs I make. And guess what? It is also a part of Online 4 Hours!

I will be creating connections between programs on my new website, and posts on my blog. For example, I have a blog post on voting systems, and a program that calculates the winner in an alternative vote! I already have the link to the post on my website, and the link to the program on my blog.

The website is up, and if you would like visit, just use this link.

Greek,Egyptian, and Hindu: The similarities in mythology.

I know the mythologies of many cultures. Greek, Egyptian, and Hindu mythology seem very far from each other, but there are many similarities between them which you may not have known.

Multiple gods

Okay, this is the most obvious similarity. I do find this interesting, though, for most religions nowadays believe in only one god, or none at all. However, these cultures saw the many different things that happen in the universe, and decided that one being could not have done everything

Order over chaos

Gaea and Uranus are born from chaos. The first island rises from the sea of chaos. The world and everything formed from chaos. Do you see a recurring pattern here? They all describe the very first thing in the universe, to be chaos. Surprisingly, they also describe chaos to be evil. The Egyptians called chaos Isfet, and described it as an evil force, constantly warring with Ma’at, or order. The Hindu god Shiva, an evil figure in most stories, was described to be the embodiment of chaos. Fact: Chaos is Greek for, the gap.  Nothingness.

Imperfection

Have you read Homer`s Illiad? Well, if you haven’t, there is a humongous war between the Greek city states and the kingdom of Troy. And you know what started it? A golden apple. In the middle of a huge wedding, The goddess Eris puts a golden apple on the table, with these words written: FOR THE FAIREST. It causes a giant argument between the goddesses, and kicks off a chain of events leading to the Trojan War. This comes to show that the Greek gods aren’t perfect, but is that trait exclusive to the Greeks? No. The world is lead to corruption for one mistake by the Egyptian god Osiris.  Indra’s pride results in the power of the Hindu Deva to vanish.  The list goes on. It is very strange to think that the beings who created everything are imperfect, and it adds a sense of reality to these otherwise myths.

It may sound crazy that there are similarities between the mythologies of three civilizations, completely isolated from each other, especially considering the major differences you can see at first glance. But hopefully, after reading this post, you will think in a different way.

 

LiD, and why you can become successful from studying goats.

Have you heard about LiD? It is the best concept ever to rise in education. A few years ago, professors in SFU gathered to discuss the idea of a broad education. They finally came with the conclusion that broad education sucks! And then, was born the concept of choosing one of dozens of highly interesting topics, ranging from the brilliant study of apples, to the unsolved mysteries of zebras. (But seriously, are zebras black with white stripes, or white with black stripes?). LiD was created!

In fact, many people can become successful using this system. Imagine you study goats as your LiD topic. During your observations, you realize that goats eat cash. Using this knowledge, you find work as a realtor!

One of the best parts of this system is the rule that you cannot change your topic. Imagine a boy who is given the LiD topic of jungles. He goes so extreme into his study, he retreats into the jungles of India! after three years, he returns to the city, knowing the jungle like the back of his hand. He goes to his teacher and asks, “I already know all there is about the jungle, for I have spent three years living with a savage pack of wolves in India. May I please change my LiD topic?” Guess what the teacher answered from the below options:

A: Why, of course you can change your topic!

B: No way, nobody can change a LiD topic!

C: Wait a second…

The answer is obviously B! Everybody knows LiD topics are for life! Three years studying jungles is nothing, even if you spend it living like Mowgli! The topic of jungles is almost as broad as the topic of metal!

Another good thing is that silly topics aren’t available as LiD topics. Cause really, who would waste their time studying computer science, when instead you can learn about the fascinating properties of wool! After all, this is the 21st century, and cotton farming may be the most important job there is! I mean, programming jobs are so 19th century, which is pathetic, considering JavaScript was made in 1995!

Conclusion: All shall follow the practices of LiD!(Even if your topic is rivers.)

Behold: The Collatz Conjecture Library!

You might have read my post on the Collatz Conjecture. If you didn’t, you can read it with this link. If you read it, you know I talked about a cool problem called the Collatz Conjecture, and also showed you a piece of code I made. Well, I updated the code, into a library of functions meant for calculating numbers on the Collatz Conjecture. I have the Gist right here:

There is a class called Collatz, and inside it 6 methods. The first two are simply for returning variables, nothing cool. The most important method inside it is find_steps, which calculates the amount of steps it takes to get from a certain number to 1. The find_max_steps method sees which number from a range of 1 to the method argument has the most steps. The final method, find_smallest steps, returns the smallest number whose steps are more than itself times the argument.

I’m thinking of posting this as a GitHub project, once I improve it. If you have any suggestions for more methods, or just to increase quality, please write in the comments. Thanks!

Voting: The big problems with a “Majority Rules” system.

Recently, school started, and we had to vote for the name of our class pet(which is a venus flytrap). These were the names up for voting:

  • Mochi
  • Jaws
  • Venus
  • Trapper
  • Gardenia
  • Furious Destroyer
  • Miri
  • Cilan

The voting system we were using was a system called First Past The Post, or Majority Rules. Just like the name describes, majority rules. There is a problem with that. For example, imagine these were the results of the election:

  • Mochi = 12%
  • Jaws = 13%
  • Venus = 13%
  • Trapper = 12%
  • Gardenia = 14%
  • Furious Destroyer = 13%
  • Miri = 12%
  • Cilan = 11%

Under First Past The Post, Gardenia would have won, since it has the most votes of any other name. However, it only beat the biggest loser, Cilan, by 3%, which in a class of 24 kids is basically nothing. in fact, 86% of the class voted against Gardenia, so that is very un-representative.

Fortunately, there is a better voting system. Single Transferable Vote, or STV,

STV can be used to select any number of candidates for one district. In this case we only need one, so to win, a candidate requires 100% of the vote. Let’s see how that works..

On election day,  voters do not write one candidate on their ballot. Instead, they rank all of the candidates from favorite to least favorite.  This is how the same election will go in STV:

  • Mochi = 12%
  • Jaws = 13%
  • Venus = 13%
  • Trapper = 12%
  • Gardenia = 14%
  • Furious Destroyer = 13%
  • Miri = 12%
  • Cilan = 11%

As you can see, no one has 100% of the vote yet. So, in each round, eliminate the biggest loser. In this case, the biggest loser is Cilan. Let’s say that the Cilan voters listed Gardenia as their second choice. Now, the results look more like this:

  • Mochi = 12%
  • Jaws = 13%
  • Venus = 13%
  • Trapper = 12%
  • Gardenia = 25%
  • Furious Destroyer = 13%
  • Miri = 12%

Still, no one has 100%. So, we eliminate the next biggest loser.

In this case, it’s a tie between Mochi, Trapper, and Miri. Suppose Mochi and Miri voters both listed each other as their second choice. Both of them are out, so we do the third choices, which for both voters is Venus. And let’s say Trapper voters split between Jaws and Furious Destroyer as their second choice. We will assume that it’s a 50-50 split, so now the results look like this:

  • Jaws = 19%
  • Venus = 37%
  • Gardenia = 25%
  • Furious Destroyer = 19%

The next biggest losers are Jaws and Furious Destroyer, so we eliminate them. Let’s say all of the voters on Jaws or Furious Destroyer listed Venus as their second/third choice. So those votes go to Venus, and the results look like this:

  • Venus = 75%
  • Gardenia = 25%

There are only two names left, and Venus obviously has the majority. So Venus wins.

The best part about this method of voting is that everyone feels represented. Mochi and Miri voters chose Venus as their second choice. Jaws voters chose Venus second, and so did Furious Destroyer voters. Trapper voters chose Venus as their third choice. last but not least, Gardenia and Cilan voters all chose Venus as their third choice.

Another good thing about STV is that it works well when multiple candidates can be chosen. You just divide the number of people you want to choose by 100.

Imagine that the USA decides to send three representatives from the state of California, and there are five people running.

  • Bob
  • Jim
  • Sally
  • Darth Vader
  • Mike

When people vote, the election goes something like this:

  • Bob = 10%
  • Jim = 10%
  • Sally = 10%
  • Darth Vader = 60% – Winner
  • Mike = 10%

In First Past The Post, Darth Vader would have won the first seat, but everyone else is tied, and there are still two seats left.

However, under STV, a candidate needs 100/number of winners to win. Darth Vader has 60 percent of the vote, and he was the only one to cross the 33% percent needed to win, so we take his extra votes and redistribute them.

Suppose every one who selected Darth Vader chose a different candidate as their second choice. If 3/8th of Vader voters chose Bob, 1/4 Jim, 1/4 Sally, and 1/8 Mike, then the results look like this:

  • Bob = 32.5%
  • Jim = 25%
  • Sally = 25%
  • Darth Vader = 33% – Winner
  • Mike = 17.5%

Then, the election continues, with votes being transferred through all the candidates.

And that, readers, is Single Transferable Vote.

Please tell your teacher about this the next time you vote for the movie you are watching in class.

For a program which calculates the result of a STV election with one winner(AKA an alternative vote election),  click here

Collatz Conjecture: The Unsolvable Problem

Recently I discovered a series of videos called Numberphile. I only watched two, both about a very interesting problem: the Collatz Conjecture.

Imagine you take a number. If it is even, divide it by two, if it is odd, multiply it by three and add one. Take the next number and repeat. Simple, right?

Let’s try it with the number 7.

3 * 7 + 1 = 22

22 / 2 = 11

3 * 11 + 1 = 34

34 / 2 = 17

3 * 17 + 1 = 52

52 / 2 = 13

3 * 13 + 1 = 40

40 / 2 = 20

20 / 2 = 10

10 / 2 = 5

3 * 5 + 1 = 16

16 / 2 = 8

8 / 2 = 4

4 / 2 = 2

2 / 2 = 1

Let’s continue from 1, then.

3 * 1 + 1 = 4

4 / 2 = 2

2 / 2 = 1

The idea of the Collatz Conjecture is that it will always reach 1, and that once it reaches one, it will keep on looping 1,4,2,1,4,2 forever. Nobody knows why this phenomenon happens.

Another interesting problem comes from counting how many steps it takes to get from a certain number to 1 with the Collatz Conjecture. I wrote a program for it which you can see right here:

The problem is finding the pattern between the number and Collatz Conjecture operations. So far, no one has been able to, and some mathematicians think no one will.

Thanks for reading!

For a program which calculates the steps to get from a number to 1 using the Collatz Conjecture, click here.

Book Review: Best Practices Of Spell Design

Not too long ago, I put together a book review on the book Computational Fairy Tales. If you did not read my review, it`s a book which explains computer science through a medieval-ish story line. The author, Jeremy Kubica, wrote a sequel called Best Practices Of Spell Design. Whereas Comp(Computational Fairy Tales) teaches you about programming concepts and algorithms, Best Practices teaches you the best way to use those tools when designing software.

The Plot: Experienced wizard Marcus has a huge problem: A wizard called Hannaldous cast a spell on the castle, and while intending to create a magic shield around the castle, he accidentally puts it in the process of destruction. Marcus must now create a counter-spell, with help from his apprentice Shelly. As the book goes along, multiple software best practices are introduced.

Remarks: As a sequel to Comp, Best Practices remains in the same universe, and seems  to be a few months after the darkness is stopped in Comp. It personally taught me a lot of good coding habits that I would not find in most other coding books.

For example, I used to write an if statement like this:

if value == 3

But Best Practices taught me that to avoid accidentally changing the value, it’s safer to put the constant first such as:

if 3 == value

I also learned about unit tests for functions, and how it can help avoid errors.

Another thing I learned is refactoring, a form of optimization in which you change the structure of the code entirely, while keeping the same functionality. Before I learned about refactoring, whenever I wanted to add a certain functionality, I would just update the current structure, causing a lot of chaos. A better way is to use refactoring to restructure the code in a way to include the new functionality while keeping the code tidy and preserving the original functionality.

It also has a lot of references to Comp, such as Ivan mentioning Princess Ann`s apprenticeship under Breadstita. finally, it is packed with humor, which I love and believe should be essential to any fictional story.

Why Donald Trump is using the same tactic as Star Wars villain Emperor Palpatine(Wait, what?)

 

I am a big Star Wars fan. I am also a big fan of politics, especially this year’s election. However, after looking at the political news, and reviewing the Star Wars storyline, I discovered a similarity in two people: Emperor Palpatine and Donald Trump.

I’m not saying that Trump is an idiot. In fact, I’m saying that he is actually intelligent enough to deceive voters and insult minorities in a way that nobody has before, except for the fictional character, Sheev Palpatine.

Now, I can guarantee you that this similarity is not intentional, Palpatine’s character was designed in 1980, long before Trump ran for president.

The main similarity is their treatment of minorities. Although this wasn’t seen fully in the films, official text and even the Rebels TV series have proven that Palpatine’s main strategy after gaining power, is basically banishing aliens from Imperial society. He appeared in holotransmissions spreading lies and fear about aliens, which led humans all around the galaxy to reject and hate their alien counterparts. Meanwhile, Trump’s ideals of racism are strengthened by inflammatory lies designed to strike fear into US voters, and lead them to believe that the country belongs to white people, and that immigrants cannot be tolerated.

Racism is one of the uses of Trump’s lies and metaphorical fear gas, but he also uses it to attack rivals such as Ted Cruz and Hillary Clinton. He used the rumored emails to insult Clinton’s crookedness, and attacked Cruz on his hypocrisy after going from “Not making America a police state” to requesting patrol of Muslim neighborhoods. Back to Star Wars, Palpatine exploits the chaos of the Senate when dissolving it, and sends propaganda on the Holonet painting the Rebel Alliance as a group of scoundrels, and the Galactic Empire as a protective force to prevent them.

If you don’t fully get it, here’s an analogy for you. Imagine you have a green table, but your friend thinks it should be brown, so he paints the table brown. In response, you paint the table green, and the war goes on. As you run out of paint, you both agree to have the rest of your friends decide. A good strategy is to frame the brown paint as filth, and your green paint a force of beauty. In this case, you are Trump/Palpatine, and your friend is the Democratic Party/Rebel Alliance.

Conclusion: The Sith Master, corrupt Emperor of the galaxy, and Master Of Deceiving, is running for president and is currently winning. No, this isn’t an excuse to stress eat the ice cream cake for your friend’s birthday. Think about Palpatine, who was later killed by Luke and Anakin Skywalker, and now is but a legend. Let’s hope somebody rises up to defeat the Galactic Empire and restore peace to the galaxy, whether it’s Bernie Sanders, Hillary Clinton, or a random Canadian Star Wars fan/political blogger.

Cause you never know.