Rock-Scissor-Paper made Easy with Python – An Introduction to Randomness in Python

What will we cover?

How to use Randomness in Python and create a Rock-Scissor-Paper game.

Step 1: Randomness in Python

To make games interesting you need some unpredictable aspects. This is where randomness is used.

Luckily, Python has a library to make randomness. To simulate rolling a die can be done as follows.

import random

die = random.randint(1, 6)
print(die)

Where randint(1, 6) returns a random integer from 1 to 6, both inclusive.

Step 2: The Rock-Scissor-Paper game

I think most know the game. If not, read the rules on wiki.

Let’s try a game of it.

print("Enter choice \n 1. Rock \n 2. Paper \n 3. Scissor \n")
choice = int(input("Choice: "))
computer_choice = random.randint(1, 3)

if choice == computer_choice:
    print("Draw")
elif choice == 1:
    if computer_choice == 2:
        print("Computer wins, Paper")
    else:
        print("You win, Scissor")
elif choice == 2:
    if computer_choice == 1:
        print("You win, Rock")
    else:
        print("Computer wins, Scissor")
elif choice == 3:
    if computer_choice == 1:
        print("Computer wins, Rock")
    else:
        print("You win, Paper")

Step 3: What is Next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Program Flows in Python – Conditional Statements and Boolean Expressions Controls the Program Flow

What will we cover?

We will cover what a program flow is, how this links to conditional statements and boolean expressions.

Step 1: Let’s recall what boolean expressions are

Boolean expression is the building block of conditional statements. So let’s make a brief recap of it.

Given two variables a and b, we have the following boolean expressions with them.

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

Where depending on the values in a and b the expressions will evaluate to True of False.

Step 2: Conditional statements

Let’s demonstrate a few if-statements here.

Equality between variables.

a = 10
b = 10
if a == b:
    print("a equals b")

Notice, that if a was not equal b, then it would not print the statement. That is, the indented code after the if-statement, is only executed if the boolean expression evaluates to True.

Another example with less-than.

a = 10
b = 8
if a < b:
    print("a is less than b")

Again the same principles applies.

You can also have an else-clause.

a = 12
b = 12
if a > b:
    print("a is greater than b")
else:
    print("a is not greater than b")

And a elif-clause.

a = 13
b = 12
if a > b:
    print("a is greater than b")
elif a == b:
    print("a is equal to b")
else:
    print("a is less than b")

And a not-equal statement.

a = 10
b = 10
if a != b:
    print("a does not equal b")

What next?

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Type Conversion in Python

What will we cover?

What is type conversion, why is it useful, and how to use type conversion in Python.

Step 1: What is type conversion in Python?

As we learned, variables in Python have types.

Sometimes we need to convert from one type to another. Say, you have an integer variable, but you need the value represented as a string.

That is type conversion. Converting from one type to another.

Notice that type conversion has many names, you might have heard of type casting, type coercion, or type juggling. But they are all about changing the expression of a data type to another.

Step 2: Why do we need type conversion?

Let’s demonstrate an issue.

Consider the following program.

name = input("What is your name? ")
print(f"Hello {name}!")
birth_year = input("What is your birht year? ")
print(f"You are {2021 - birth_year} old!")

Well, you expect the program to write out your age.

Unfortunately, it will not function.

Why? Because 2021 – birth_year is not valid. 2021 is an integer, while birth_year is a string. But, you cannot subtract a string from an integer.

So what to do?

Type conversion.

Step 3: How to make type conversion in Python

Now we understand the problem. Let’s try to solve it.

Luckily, Python is our friend and has built in functions.

  • int() Converts to an integer.
  • float() Converts to a float.
  • str() Converts to a string.

Let’s try the first one.

name = input("What is your name? ")
print(f"Hello {name}!")
birth_year = input("What is your birht year? ")
birth_year = int(birth_year)
print(f"You are {2021 - birth_year} old!")

Now it works as expected and the only difference is the statement birth_year = int(birth_year).

Nice and easy, it converts birth_year to an integer (if possible)

The other functions work similarly.

What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Understand Variables in Python and the Main Types of Variables

What will we cover?

We will cover the what a variable is. How you should name the variables, and finally, the main types of variables in Python.

Step 1: What is a Python variable?

Simply said.

  • A variable is a reserved memory location.
  • A variable in a python program gives data to the computer for processing.

When you work with data, you need somewhere to keep it. That is what variable are used for.

A simple example of a variable can be demonstrated as follows.

s = input('What is your name')
print('Hi', s)

Here, s, is a variable. It takes input from the user and stores it there. Then it prints the value of the variable on the next line.

In Python we have the following basic types.

  • Integers
  • Floats
  • Strings
  • Boolean

Step 2: Naming convention of Variables in Python

A variable name in Python should fulfil the following

  • Use lowercase
  • Separate words with underscore.

A variable name in Python can be

  • A single lowercase letter
  • A lowercase word
  • Or lowercase words separated by underscore.

Examples of variable names

  • x
  • i
  • cars
  • my_cars

Note that other programming languages can have other naming conventions.

You can declare variables as follows.

s = "This is a string"
a = 2
b = 2.4

And get the type of a variable as follows.

type(b)

Step 3: Python type Integer

An integer is a number which is not a fraction; a whole number.

Examples of integers: 

  • -23
  • -5
  • 0
  • 1
  • 78
  • 1,980,350

Examples of non-integers: 

  • 3.14
  • 0.001
  • 9,999.999

Try the following.

a = 2
b = 3

Then you can make arithmetic operations with them.

c = a + b

Now c is an integer with value 5.

Not convinced? Try to get the type as follows.

type(a + b)

Step 4: Python type Float

A float is a numbers with digits on both sides of a decimal point. This is in contrast to the integer data type, which houses an integer or whole number.

Examples of floats:

  • 3.13
  • 9.99
  • 0.0001

Note that a float data type in Python can represent a whole number.

  • 1.0
  • 3.0

An example could be.

a = 1.1
b = 2.3

You can also make arithmetic operations.

c = a + b

Then c will be a float.

Funny enough, if you have a float and add an integer, then you get a float. Let’s try.

d = 1
e = 1.1
f = d + e

Try to take the type of all them to make sure they are the expected types.

type(d), type(e), type(f)

Notice you can comma separate statements like that, and it will output it in a comma separated line.

Step 5: A few useful Math functions

When working with integers and floats it can be handy with a few helpful mathematical functions.

Here we will look at.

  • abs() Returns absolute value of a number
  • pow() Raises a number to a power
  • round() Rounds a floating-point value

The abs() function returns the absolute value of an integer or float.

a = -2
abs(a)

The pow() returns the number to a power.

pow(2, 3)

Notice that this can also be accomplished as follows.

2**3

Finally, round() is very useful function used all the time. It rounds the value of a float.

f = 1.234456778
round(f, 4)

Where the second argument of round is the precision after the decimal point.

Step 6: String in Python

A string in Python is a sequence of characters.

Examples of strings in Python includes the following.

  • “I am a string!”
  • ‘I am another string’

That is, you can both use single and double quoted strings. The standard is to use single-quoted strings, but the double-quoted are used in special cases.

  • “I’m a double quoted string”

Where you see it can have a quote inside the string.

That said, many, me included, use single and double quoted string inconsistently.

s1 = 'I am a string'
s2 = "I am another string"

Given the two strings above you can use addition to concatenate them.

s1 + s2

Yes, that is right. You can add strings together. But you cannot add a string and an integer or float.

Step 7: Print Strings and Integers and Floats with Python

The easiest way to print variables of different types in Python is to comma separate them.

a = 1
b = 1.1
c = "I am a string"
print(a, b, c)

This will work fine, while something like print(a + b + c) will fail.

A great way to print formatted output is as follows.

a = 1
print(f'I am a string with an int here {a + 2} - and more text')

Hence, if you put an f in front of the quoted string, then you can add {} with integer or float statements – see it can be a variable or an arithmetic expression.

Step 8: Boolean variables in Python

If you are new to programming, then boolean expression can be quite difficult to understand. Or actually not. Most people think they get them, but do not understand the full content of the power of boolean expression.

Actually, boolean expressions is what makes programming possible. Boolean expressions take simple scripts that do the exact same every time they are executed to full programs, which behave differently depending on input and context.

So what are boolean values?

  • True or False (case sensitive)

And a boolean expression is something that evaluates to either True or False.

That said, everything in your life is the same.

Say, every morning you look out the window to see if it rains. If it rains you prepare yourself for that. If not, well, you enjoy it not raining.

Some object to this. They say, it is not always easy to say if it rains or not. Well, you can the break it down to, either you bring your umbrella or not.

  • Either it rains or not -> Either you take your umbrella or not

Somehow all your decisions can be translated to boolean expression.

But let’s get started simple.

b = 10 > 5

What? Well, you assign the boolean expression to 10 > 5. If 10 is larger than 5, then it will be True.

Check the value in b, it is True.

How can this be powerful, you might ask?

Well, when we look at conditional flows, you will be able to make powerful programs that can do whatever you want it to.

Step 9: What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is part of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

See the full FREE course page here.

Your First Python Lesson – One Step Closer to Mastery

What will we cover?

We will take you from knowing nothing about Python, what to install to get started, how to write your first programs, make a few interactive programs, and learn a few string methods.

Finally, if you still are hooked, guide you to our free 8 hours Python course for beginners, which is a 17 lesson course, including 17 projects and 17 video tutorials you can follow along.

Step 1: Install JuPyter Notebook and Launch your first Notebook

Anaconda install all you need and it is free. That is, it install Python, most of the libraries you need, and a simple interactive environment to develop Python code in, the JuPyter Notebook.

Shortly explained.

  • Anaconda installs all and you can launch JuPyter Notebook from Anaconda Navigator (often just called Anaconda).
  • JuPyter Notebook can navigate through your files and open Notebooks, where you write your code.

How to get started?

  1. Go to Anaconda and download the individual edition (it is free). 
  2. It will install Python and Jupyter notebook. That is all you need to get started and it is all free.
  3. Launch Anaconda Navigator.
  4. In Anaconda launch Jupyter Notebook.
  5. Navigate to a place where you want to save your files.
  6. Create a new Python Notebook with the “new” menu in the right.

Step 2: Writing your first program “Hello, World!”

Click on the first cell and write the following code.

print('Hello, World!')

Press SHIFT-ENTER to execute it.

It should print “Hello, World!” below the cell.

The Hello, World! program is traditionally the first program you write in a language, which demonstrates the syntax.

Python has very little syntax and is easy to use. To compare with other languages see Wikipedia.

Step 3: Input a string from the user

Making programs interactive is fun. The first step is to get the user to type something and let your program do something with that.

s = input("What is your name? ")
print(s)

Look at the code and guess what it does.

Then copy the code into a cell and execute it.

What happened?

It prompted you for something – and you might type some letters, say, your name. Then what will it do? Yes, print your name.

Step 4: Your first interactive program

Actually, you have the tools now to make simple interactive programs.

Let’s try that now.

name = input("What is your name? ")
print("Hello", name.capitalize())
age = input("How old are you? ")
print("You are", age, "years old")

See how simple that is.

Well, you learned a few things here.

if you have a string (like, name), then you can apply capitalize() on it. What does it do?

Well, if you type your name in lowercase, then it will capitalize your name for you. If it already is capitalized, it will do nothing.

Now that is smart.

Step 5: A few methods on strings

Like capitalize() there are a lot of other methods you can apply.

In the beginning you probably worry about remember them all. I got good news for you – you do not need to. You will most likely only use a few and the others you will lookup when you need them.

But a few ones that can be funny to master are the following.

"Rune".upper()
"Rune".lower()
"223".isdecimal()
"Rune".isdecimal()

Try to copy each one into a cell by itself and see what it does. It will most likely make sense to you.

Notice, if you copy them all into one cell in JuPyter Notebook, then it will only give the output of the last statement of the cell. Therefore, try one line at the time in one cell.

Notice, that the “Rune” is a string and the methods could be applied on variables as well.

s = "Rune"
s.upper()

Step 6: Replace something in a string

My favorite and most used string method is replace(.)

Let’s just try it.

name = "Rune"
name.replace('R', 'S')

Put it into a cell and execute.

It will output Sune. It will change all occurrences of R to S.

Notice, that these methods are case sensitive, meaning, that the following code.

name = "Rune"
name.replace('r', 's')

It will output Rune, because there is no lowercase r to replace.

Step 7: What next?

I am happy you asked.

If this is something you like and you want to get started with Python, then this is the first lesson of a 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

Check out the first lesson here.

What about the full course?

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE eBook with all the learnings from the lessons.

See the full FREE course page here.

Click and get started on your journey to Python Mastery

How to Learn Python for Data Science

What will we cover?

  • Is Python the correct language to learn for a Data Scientist?
  • How much Python do you need to learn as a Data Scientist?
  • How to learn Python fast?
  • How long does it take to become good at Python?
  • How to get started with Python?

Is Python the correct language to learn for a Data Scientist?

That is a good question to ask yourself. You want to become a Data Scientist, maybe you have some experience, but feel weak in the programming aspect, or maybe you start from scratch.

If I was to start my journey as a Data Scientist one of the questions I would ask myself, is, do I have the tools for it.

R is often high on the scale of programming language and environment to use as a Data Scientist. The language R is designed for effective data handling, operations on arrays and matrices, has data analysis tools, graphical facilities, and well established environment.

That sounds like all we need, so why bother looking further?

In the top there is a battle between two candidates: Python vs R.

Actually, Python is a general purpose language that has a wide aspects of uses, not only Data Scientist. Also, web services, game development, big data backend systems processing high volume data, just to mention a few.

With this description, it looks like R is tailored for Data Science, while Python is used for everything. The choice seems easy – do you want a tool made for the purpose, or something for general purpose?

Funny enough, as it might seem at first, Python has become more popular than R. Why is that?

A few reasons why Python is more popular than R.

  • Python is easy to use and learn.
  • Python has powerfull fast libraries.
  • Python has a huge community and it is easy to get help.
  • Python has easy data handling tools for reading and generating spreadsheets, parquet files, csv files, web scraping, sql databasis, and much more.
  • Python has great Machine Learning libraries developed by giants like Google (tensorflow) and Facebook (PyTorch).
  • Python support graphical data representation with libraries like Matplotlib.
  • Python has SciKit-learn for predictive data analysis.
  • Python has easy to use data representation with NumPy and pandas.

…and the list could go on.

Python is also a great fit when you want to build tailored-made system, which integrate up against any other platform or service, like automatically get data from various sources.

Do I need a Computer Science degree to use Python?

Python is programming and programmers have computer science degrees. Do you need one to become a good Data Scientist?

The short answer is: No.

A Computer Science degrees will enable you to build anything. Let’s try to think of it differently.

Think of transportation – car, busses, bikes, trains, which can move you from A to B. People without a driving license can use busses and trains. All they need is to know how to buy a ticket, understand a schedule to find out to get from A to B. If you get a driver license, then you can driver your own car. Finally, if you are a car mechanics, you can repair and possibly build your own car.

Similarly, a computer science degree will enable you to build cars, busses, trains, and more, which other people can use. A Data Scientist is like a person with a driver license, and you don’t need to be able to repair a car to drive it. That is, you only need to understand and navigate the dashboard in the car.

Data Science is the same., you need to understand the things you use, but you do not need to be able to build them yourself.

But wait! You might object. It is still programming, when I use the things I use.

Yes, but the level of programming is simple and you use the complicated things like you use a car without being a car mechanics.

Feel more comfortable?

How to Learn Python Fast?

Now you are ready and know what you want – how to get there fastest without wasting time.

Maybe one question before that .

Can everybody learn Python? Do you need special skills?

I have so far never met anyone, which could not learn Python to the level of Data Science – and honestly, also for the level of Computer Scientist. It is just a question about dedication and interest to get to the last steps.

But becoming a Data Scientist using Python is not a problem.

The question is more how to learn it fast? The best way to answer that is to look at some of the most common pitfalls that make people learn it slower and some give up on the way.

Pitfall 1: I understand the solution when I see, but why couldn’t I figure it out – am I stupid?

Did you ever learn a new language – a speaking one – like English. If you are non-native English, then you started learning English at once. Remember that?

First you started understanding a few words. Then you started to understand full sentences when people where speaking English, but you could barely express yourself in English yourself. It took time to get there.

Programming is the same – at first you can read and understand the solutions to your problem, it takes time for you to be able to express yourself in programming language.

The feeling you have while trying to solve a programming problem for long time, but not succeeding can be devastating. Then when you see the solution and it looks simple, then you start to feel stupid.

But stop there – this is normal. You learn first to understand code before you can express yourself in code. Just like learning a new speaking language.

We have all been there – and we still get there – just with different more complex problems. It will never end, you will just become comfortable about it and the challenges you face will be more and more complex.

Pitfall 2: Get distracted when it gets tough

When something gets difficult the easy exit is to quit and start something new easier.

Maybe you think, this is too difficult for me – I am not smart enough. This is more fun, so I start this now.

The truth is, that every talented programmer on planet earth has multiple times been stuck at a problem for days – not being able to solve it – if it was a bug or just a difficult problem to solve does not matter. But they have all been struggling with a problem for long time.

This can be quite difficult to deal with as a beginner. You sit with a problem, which does not seem hard and you feel like everyone else can solve it – the logical conclusion is that you are not smart enough, right?

Then you might change to another programming project – and think that is fine, you will still learn programming.

But the truth is, that solving hard problems or finding bugs is not easy. It takes time and you will learn a lot from it. Escaping to another project will not teach you as much as the difficult ones.

The best programmers are the ones that never give up when it gets tough. This is what the highly paid consultant are paid for, solving problems where other give up.

Pitfall 3: Different sources of learning

This is often difficult to understand in the beginning. But there are many styles in programming.

When you know people and been working professionally with them in a development environment for long time, you can actually see who coded it. Their style falls through.

Why does that matter?

In the beginning it does. Because, what most also fail to understand in the beginning is, that you can solve problems in endless ways. There is often no perfect solution for a problem, only different solutions which different tradeoffs.

As a beginner, you want to learn programming and you will not see the differences in styles. But if you starte learning from one person, then another one, then yet another one, then it becomes difficult.

This has never been more relevant in the age where so many people share online learning.

Again, it is like learning English with a specific dialect and different vocabulary. It is difficult in the beginning to distinguish between them, and difficult to see it matters. But in the long run you will speak English optimized for your environment.

Keep focused learning from one source. Do not change from one place to another all the time. Master the basics from one place until you are comfortable about it.

Pitfall 4: Comparing yourself to others

We often compare our learning journeys to others. You need to know if you are doing good or bad, if you need to adjust your approach or not.

This sounds good, right?

You need to keep in touch with reality and not waste time.

This is a major pitfall. You will see solutions to your problems, which are solved more elegant. There will be people that ‘just started’ and are already typing in code like you would never dream of.

This is devastating. Do you not have what it takes?

As hard as it is to accept, that you are not the fastest learner, and you need to work harder than others to reach the same. It is just as hard to realize, that the people you compare yourself with are often the top-of-the-top.

We all have our own journey. Mine is different from yours. I was good at one thing in the beginning, but you are awesome at something I never understood.

Accept that we all have our own journey – there will be times when you feel like the only one not understanding something simple (or at least I did that many times) – but other times when you actually understand something extremely complex.

We often miss these aspects, because we always compare ourselves to the brightest person in our context in any moment. That might be different persons from time to time.

Further, in the days of internet, the environment you compare yourself to is huge.

As you see, this comparison is not fair and will do you no good.

Accept that your journey is yours alone. Comparisons with others do not help you.

How long does it take to become a good Python programmer

I wish there was a simple answer to that. Unfortunately it is not that easy to answer.

First of all, what are your initial expectations and how will they evolve over time. Often people are fine with just some simple skills, but when they learn more they want to master more and it never stops.

It is natural. The problem is, your expectations to feeling successful moves along the way.

Secondly, is the dedication to it. You need to spend time on solving problems.

Experience shows, that either you need to burn for learning programming or you need it to solve you daily challenges.

It sounds like you need to keep motivated. And yes, you do. But the good news is, it is very rewarding and fulfilling to program. You are building something, you are creating something, you are the creator of something amazing. That feeling is awesome.

Does that mean it is just fun all the way from the beginning to end. Not at all, did you read the pitfalls above? Well, if you didn’t, go read them.

What I am saying is, it is a journey that will never end. The journey will sometimes feel bumpy, but the results are rewarding.

The more time you spend, the faster and better results you will get.

But how to keep motivation?

  • Remind yourself daily, that there are pitfall and all the best in the world have been there.
  • Keep it playful – the majority of the time it is joyful to program.
  • Accept it as a learning journey that will never end.

How to get started with Python for Data Science?

On this page there are a lot of resources available to get started with both Python and Data Science.

To help you further there are structured free courses you can follow with everything prepared.

Start Python for FREE

There is a full 8 hours video corse for Python.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE eBook with all the learnings from the lessons.
Get started today with Python for FREE

Start Machine Learning for FREE

Another great free resource is the 10 hours free Machine Learning course.

  • 15 video lessons – which explain Machine Learning concepts, demonstrate models on real data, introduce projects and show a solution (YouTube playlist).
  • 30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
  • 15 projects – with step guides to help you structure your solutions and solution explained in the end of video lessons (GitHub).
Get started with Machine Learning with Python for FREE

Calculate the CAPM with Python in 3 Easy Steps

What will we cover?

In this lesson we will learn about the CAPM and how to calculate it.

The objectives of this tutorial is:

  • Understand the CAPM (Capital Asset Pricing Model).
  • Beta and CAPM calculations.
  • Expected return of an investment.

Step 1: What is the CAPM?

The CAPM calculates the relationship between systematic risk and expected return. There are several assumptions behind the CAPM formula that have been shown not to hold in reality. But still, the CAPM formula is still widely used.

The formula is as follows.

Step 2: Get some data to make calculations on

Let’s get some data and calculate it.

import numpy as np
import pandas_datareader as pdr
import datetime as dt
import pandas as pd
 
tickers = ['AAPL', 'MSFT', 'TWTR', 'IBM', '^GSPC']
start = dt.datetime(2015, 12, 1)
end = dt.datetime(2021, 1, 1)
 
data = pdr.get_data_yahoo(tickers, start, end, interval="m")
 
data = data['Adj Close']
 
log_returns = np.log(data/data.shift())

Feel free to change the tickers to your choice and remember to update the dates to fit your purpose.

Step 3: How to calculate CAPM with Python (NumPy and pandas)

The calculations are done quite easily.

Again, when we look at the formula, the risk free return is often set to 0. Otherwise, the 10 years treasury note is used. Here, we use 1.38%. You can update it for more up to date value with the link.

cov = log_returns.cov()
var = log_returns['^GSPC'].var()
 
beta = cov.loc['AAPL', '^GSPC']/var
 
risk_free_return = 0.0138
market_return = .105
expected_return = risk_free_return + beta*(market_return - risk_free_return)

Notice, you can calculate it all simultaneously.

Want more?

This is part of a 2.5 hour full video course in 8 parts about Risk and Return.

Calculate the market (S&P 500) BETA with Python for any Stock

What will we cover?

In this lesson we will learn about market Beta with S&P 500 index, how to calculate it, and comparison of calculations from last lesson.

The objective of the tutorial is:

  • Understand what market Beta tells you.
  • How to calculate the market (S&P 500) Beta.
  • See how Beta is related with Linear Regression.

Step 1: What is BETA and how to interpret the value

Beta is a measure of a stock’s volatility in relation to the overall market (S&P 500). The S&P 500 index has Beta 1.

High-beta stocks are supposed to be riskier but provide higher potential return. While, low-beta stocks pose less risk but also lower returns.

Interpretation

  • Beta above 1: stock is more volatile than the market, but expects higher return.
  • Beta below 1: stock with lower volatility, and expects less return.

The formula for Beta is Covariance divided by variance.

This sound more scary than it is.

The Beta on financial pages, like Yahoo! Finance, are calculated on the monthly price.

Step 2: Get some historic stock prices with Pandas Datareader

Let’s make an example here.

import numpy as np
import pandas_datareader as pdr
import datetime as dt
import pandas as pd
from sklearn.linear_model import LinearRegression
 
tickers = ['AAPL', 'MSFT', 'TWTR', 'IBM', '^GSPC']
start = dt.datetime(2015, 12, 1)
end = dt.datetime(2021, 1, 1)
 
data = pdr.get_data_yahoo(tickers, start, end, interval="m")
 
data = data['Adj Close']
 
log_returns = np.log(data/data.shift())

Where we notice that we read data on interval=”m”, which gives the monthly data.

Step 3: Calculate the BETA

Then the Beta is calculated as follows.

cov = log_returns.cov()
var = log_returns['^GSPC'].var()
 
cov.loc['AAPL', '^GSPC']/var

For Apple, it was 1.25.

If you wonder if it is related to the Beta value from Linear Regression. Let’s check it out.

X = log_returns['^GSPC'].iloc[1:].to_numpy().reshape(-1, 1)
Y = log_returns['AAPL'].iloc[1:].to_numpy().reshape(-1, 1)
 
lin_regr = LinearRegression()
lin_regr.fit(X, Y)
 
lin_regr.coef_[0, 0]

Also giving 1.25. Hence, it is the same calculation behind it.

Want more?

This is part of a 2.5 hours in 8 lessons video course about Risk and Return.

How to use Linear Regression to Calculate the Beta to the General Market (S&P 500)

What will we cover?

In this lesson we will learn about Linear Regression, difference from Correlation and how to visualize Linear Regression.

The objective of this tutorial is.

  • Understand the difference between Linear Regression and Correlation.
  • Understand the difference between true random and correlated variables
  • Visualize linear regression.

Step 1: Similarities and differences between linear regression and correlation

Let’s first see what the similarities and difference between Linear Regression and Correlation is.

Similarities.

  • Quantify the direction and strength of the relationship between two variables, here we look at stock prices.

Differences.

  • Correlation is a single statistic. It is just a number between -1 and 1 (both inclusive).
  • Linear regression produces an equation.

Step 2: Visualize data with no correlation

A great way to learn about relationships between variables is to compare it to random variables.

Let’s start by doing that.

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt
%matplotlib notebook
 
X = np.random.randn(5000)
Y = np.random.randn(5000)
 
fig, ax = plt.subplots()
ax.scatter(X, Y, alpha=.2)

Giving the following scatter chart.

Which shows the how two non-correlated variables look like.

Step 3: How to visualize correlated stock prices

To compare that to two correlated, we need some data.

tickers = ['AAPL', 'TWTR', 'IBM', 'MSFT', '^GSPC']
start = dt.datetime(2020, 1, 1)
 
data = pdr.get_data_yahoo(tickers, start)
data = data['Adj Close']
log_returns = np.log(data/data.shift())

Let’s make a function to calculate the Liner Regression and visualize it.

def linear_regression(ticker_a, ticker_b):
    X = log_returns[ticker_a].iloc[1:].to_numpy().reshape(-1, 1)
    Y = log_returns[ticker_b].iloc[1:].to_numpy().reshape(-1, 1)
 
    lin_regr = LinearRegression()
    lin_regr.fit(X, Y)
 
    Y_pred = lin_regr.predict(X)
 
    alpha = lin_regr.intercept_[0]
    beta = lin_regr.coef_[0, 0]
 
    fig, ax = plt.subplots()
    ax.set_title("Alpha: " + str(round(alpha, 5)) + ", Beta: " + str(round(beta, 3)))
    ax.scatter(X, Y)
    ax.plot(X, Y_pred, c='r')

The function takes the two tickers and get’s the log returns in NumPy arrays. They are reshaped to fit the required format.

The the Linear Regression model (LinearRegression) is used and applied to predict values. The alpha and beta are the liner variables. Finally, we scatter plot all the points and a prediction line.

Let’s try linear_regression(“AAPL”, “^GSPC”).

Where we see the red line as the prediction line.

Step 4: A few more examples

Other examples linear_regression(“AAPL”, “MSFT”)

And linear_regression(“AAPL”, “TWTR”).

Where it visually shows that AAPL and TWTR are not as closely correlated as the other examples.

Want more?

This is part of 8 lesson and 2.5h video course with prepared Jupyter Notebooks with the Python code.

How to Calculate Correlation between Stock Price Movements with Python

What will we cover?

In this lesson we will learn about correlation of assets, calculations of correlation, and risk and coherence.

The learning objectives of this tutorial.

  • What is correlation and how to use it
  • Calculate correlation
  • Find negatively correlated assets

Step 1: What is Correlation

Correlation is a statistic that measures the degree to which two variables move in relation to each other. Correlation measures association, but doesn’t show if x causes y or vice versa.

The correlation between two stocks is a number form -1 to 1 (both inclusive).

  • A positive correlation means, when stock x goes up, we expect stock y to go up, and opposite.
  • A negative correlation means, when stock x goes up, we expect stock y to go down, and opposite.
  • A zero correlation, we cannot say anything in relation to each other.

The formula for calculating the correlation is quite a mouthful.

Step 2: Calculate the Correlation with DataFrames (pandas)

Luckily, the DataFrames can calculate it for us. Hence, we do not need to master how to do it.

Let’s get started. First, we need to load some time series of historic stock prices.

See this tutorial on how to work with portfolios.

import pandas as pd
import pandas_datareader as pdr
import datetime as dt
import numpy as np
 
tickers = ['AAPL', 'TWTR', 'IBM', 'MSFT']
start = dt.datetime(2020, 1, 1)
 
data = pdr.get_data_yahoo(tickers, start)
data = data['Adj Close']
 
log_returns = np.log(data/data.shift())

Where we also calculate the log returns.

The correlation can be calculated as follows.

log_returns.corr()

That was easy, right? Remember we do it on the log returns to keep it on the same range.

Symbols AAPL    TWTR    IBM MSFT
Symbols             
AAPL    1.000000    0.531973    0.518204    0.829547
TWTR    0.531973    1.000000    0.386493    0.563909
IBM 0.518204    0.386493    1.000000    0.583205
MSFT    0.829547    0.563909    0.583205    1.000000

We identify, that the correlation on the diagonal is 1.0. This is obvious, since the diagonal shows the correlation between itself (AAPL and AAPL, and so forth).

Other than that, we can conclude that AAPL and MSFT are correlated the most.

Step 3: Calculate the correlation to the general market

Let’s add the S&P 500 to our DataFrame.

sp500 = pdr.get_data_yahoo("^GSPC", start)
 
log_returns['SP500'] = np.log(sp500['Adj Close']/sp500['Adj Close'].shift())
 
log_returns.corr()

Resulting in this.

Where we see that AAPL and MSFT are mostly correlated to S&P 500 index. This is not surprising, as they are a big part of the weight of the market cap in the index.

Step 4: Find Negative Correlated assets when Investing using Python

We will add this helper function to help find correlations.

We are in particular interested in negative correlation here.

def test_correlation(ticker):
    df = pdr.get_data_yahoo(ticker, start)
    lr = log_returns.copy()
    lr[ticker] = np.log(df['Adj Close']/df['Adj Close'].shift())
    return lr.corr()

This can help us find assets with a negative correlation.

Why do we wan that? Well, to minimize the risk. Read my eBook on the subject if you want to learn more about that.

Now, let’s test.

test_correlation("TLT")

Resulting in this following.

The negative correlation we are looking for.

Step 5: Visualize the negative correlation

This can be visualized to get a better understanding as follows.

import matplotlib.pyplot as plt
%matplotlib notebook
 
def visualize_correlation(ticker1, ticker2):
    df = pdr.get_data_yahoo([ticker1, ticker2], start)
    df = df['Adj Close']
    df = df/df.iloc[0]
    fig, ax = plt.subplots()
    df.plot(ax=ax)

With visualize_correlation(“AAPL”, “TLT”) we get.

Where we see, when AAPL goes down, the TLT goes up.

And if we look at visualize_correlation(“^GSPC”, “TLT”) (the S&P 500 index and TLT).

What next?

Want more?

This is part of a full FREE course with all the code available on my GitHub.