Why do we use logarithmic returns in Quantitative Finance?

The answer is several reasons, each of whose individual importance varies by problem domain.

we begin by defining a return:  r_i at time i, where p_i is the price at time i and j \equiv (i - 1):
   
 r_i = \frac{p_i - p_j}{ p_j }
Benefit of using returns, versus prices, is normalization: measuring all variables in a comparable metric, 
thus enabling evaluation of analytic relationships amongst two or more variables despite originating from price 
series of unequal values. This is a requirement for many multidimensional statistical analysis and machine 
learning techniques. For example, interpreting an equity covariance matrix is made sane when the variables 
are both measured in percentage.
Several benefits of using log returns, both theoretic and algorithmic.
First, log-normality: if we assume that prices are distributed log normally (which, in practice, may or may not be true for any given price series), then log(1 + r_i) is conveniently normally distributed, because:
    1 + r_i = \frac{p_i}{p_j} = \exp^{\log(\frac{p_i}{p_j})}
This is handy given much of classic statistics presumes normality.
Second, approximate raw-log equality: when returns are very small (common for trades with short holding durations), the following approximation ensures they are close in value to raw returns:
    \log(1 + r) \approx r , r \ll 1
Third, time-additivity: consider an ordered sequence of n trades. A statistic frequently calculated from this sequence is the compounding return, which is the running return of this sequence of trades over time:
    \displaystyle (1 + r_1)(1 + r_2)  \cdots (1 + r_n) = \prod_i (1+r_i)
This formula is fairly unpleasant, as probability theory reminds us the product of normally-distributed variables is not normal. Instead, the sum of normally-distributed variables is normal (important technicality: only when all variables are uncorrelated), which is useful when we recall the following logarithmic identity:
    \log(1 + r_i) = log(\frac{p_i}{p_j}) = \log(p_i) - log(p_j)
Thus, compounding returns are normally distributed. Finally, this identity leads us to a pleasant algorithmic benefit; a simple formula for calculating compound returns:
    \displaystyle \sum_i \log(1+r_i) = \log(1 + r_1) + \log(1 + r_2)  + \cdots + \log(1 + r_n) = \log(p_n) - \log(p_0)
Thus, the compound return over n periods is merely the difference in log between initial and final periods. In terms of algorithmic complexity, this simplification reduces O(n) multiplications to O(1) additions. This is a huge win for moderate to large n. Further, this sum is useful for cases in which returns diverge from normal, as the central limit theorem reminds us that the sample average of this sum will converge to normality (presuming finite first and second moments).
Fourth, mathematical ease: from calculus, we are reminded (ignoring the constant of integration):
    e^x = \int e^x dx = \frac{d}{dx} e^x = e^x
This identity is tremendously useful, as much of financial mathematics is built upon continuous time stochastic processes which rely heavily upon integration and differentiation.
Fifth, numerical stability: addition of small numbers is numerically safe, while multiplying small numbers is not as it is subject to arithmetic underflow. For many interesting problems, this is a serious potential problem. To solve this, either the algorithm must be modified to be numerically robust or it can be transformed into a numerically safe summation via logs.

As suggested by John Hall, there are downsides to using log returns. Here are two recent papers to consider (along with their references):

1. Intro to the Filesystem

Whether you're going to be developing web services and deploying them on a Linux Container, or doing systems work and troubleshooting servers, having a good grasp on the file system is pretty essential.
In this lesson, you'll practice working with files and directories, and using a range of shell commands that operate on them.

2. Quiz: The Filesystem Tree

The two most important kinds of objects in a Linux server's filesystem are files and directories.




The reason for this is that these characters all have special meanings to the shell, and putting a backslash in front of them tells the shell to treat them as ordinary characters.

Directories are nested inside each other, with the outermost or topmost being the file system root or root directory.
It's very common for there to be multiple discs or disc partitions on a Linux system.
Unlike on Windows, there aren't separate roots for each disc, like C: or  D: drives.
This means that no matter what directory a file is in, you can unambiguously refer to that file by giving the full path which starts at the root and lists each directory on the way down to the individual file.

3. Quiz: The Working Directory


If you want to know the path of that directory, you can use the pwd command which stands for print working directory, and this says that here the working directory .
You can change the working directory with the CD command for change directory.


4. Absolute and Relative Paths

The full path is called the absolute path, and that means that it tells every step that has to be taken from the root, or the absolute begging of the file system.

To make things simpler, we can use relative paths instead.
A file's relative path, is its location relative to the current working directory.



Notice that, unlike a full path, the relative path does not start with a slash.
That's how you can tell a relative path from an absolute one.

The special directory entry '..' points from a directory to its parent.
So, if you're in /home/philip/ocean, and you refer to '..', you're talking about the directory /home/philp
And '.' points from each directory to itself.
Another handy shortcut is ~. ~ is an abbreviation for your own home directory.

5. Quiz: Absolute and Relative Paths


6. Quiz: cd without args


7. Quiz: cd to not a directory


8. Quiz: Navigating Directories


9. Tab Completion

If you press tab and then tab again it'll list all of the possible completions for what you've typed.

10. Quiz: Moving and Copying Files

The shell command to move files is mv, which is short for move.
And the command cp is for copy.

11. Quiz: Making and Removing Directories

You can create new directories using the mkdir command.
All you have to give mkdir is a path to where the new directory should be.



And if you're done with a directory, and want to get rid of it, the command for that rmdir.
You can't use just plain rm on a directory.

But if a directory has files in it, you can't rmdir that directory.
There is a way to recursively remove a directory and all the files inside.



Curl is for downloading from a web URL and empty isn't one of those.
The r stands for recursive.

And this is the standard way to erase an entire directory and all of its contents.

And mv junk trash will do one of two different things.
If there is a directory called trash, it'll move junk to inside of it.
And if there isn't a directory called trash, it'll rename junk to trash.

12. Quiz: mv and directories


13. Globbing

Any time you want to operate on a bunch of files that have similar names, you can use a glob pattern to do it. Globbing is the real, actual, technical term for matching files by name in the Unix shell.
Globbing is a kind of pattern matching for file names.
When you write glob pattern in a shell command, the shell turns that pattern into a list of file names that exist to match the pattern.


Follow these following explanations one by one in order.

For instance, a star matches any string of characters.

You can use a star at the beginning or at the end of a pattern.

You can use two stars in the same pattern.
For instance, matching every file whose name contains pp.

A star can appear in the middle of a pattern.
Matching all the files that start with B and end with png.

To match files that end in either CSS or html, a list of strings in curly braces will match any of the alternatives.

A single question mark matches any one character.

Two question marks matches two characters, and so on.

List of character inside square brackets matches any one of the characters inside those brackets, so be and then square brackets.



Something to watch out for is that file names in Linux are always case sensitive, and that applies to globbing too.

14. Quiz: Globbing Quiz


15. Quiz: Applying Globbing


cp doesn't create directories on its own.

16. Course Outro

Now you're ready to go on to any number of subjects that depends on the command line interface.
Another command line tool to learn is Git.
A very popular distributed version control system.


1. L2 Intro


You've used sevral shell commnds and you've seen their results.
In this lesson, you'll learn some more shell commands and get practice using them.

2. Quiz: Filenames and Contents


3. Command History

If you type a command and you don't get it right, or if you just want to recall something that you've used before, try using the up arrow key.
The shell keeps a history of commands that you've run so you can walk back through them using the arrow keys. Then you can edit a previous command line that you've used and maybe get more success.

The 'history' command prints out the whole history.
Another is the keystroke control R.
This is a special function called reverse i search, which lets you search your shell history.
This is awfully handy if you used some command long ago or just yesterday and it's long command you want to recall it now.

4. Some Common Commands

For example, ls lists the files in the current directory.

If you didn't download things.zip earlier you can do it now by running this command inside your VM's shell:
curl -o things.zip http://udacity.github.io/ud595-shell/stuff.zip

Let's go ahead and unzip that so we have more files to work with.
The unzip command typically takes one argument.



The file name of the file to unzip.
As it works, unzip lists the files and directories it's uncompressing.

Another useful command is cat, give cat a list of file names and it'll print out the contents of the files.
It's an abbreviation for concatenate.
Cat, concatenate files, and outputs the result.
You can also use cat to read a single files.

I was just using one of the shells built in time saving features, tab completion.
If you press the tab key while typing a word, the shell will try to autocomplete it for you.
If i hit tab again, the shells will tell me all the files that match.

There are also commands for analyzing files.
wc is the word count program.
It tells us how many lines, words, and byte are in a file.



Another command for analyzing files is diff.
Diff compares files and shows you how they differ.
This can be useful if you have multiple versions or editions of the same file and you want to know what changes have been made.

5. Manual Pages

Manual entries are called man pages, and this is cowsay's man page.



Man pages follow a common format.
The first section includes the command's name and a brief description of the command.

The next section is the synopsis.
The synopsis explains how to the command's flags and arguments.
Options surrounded by square brackets are optional.
Some options require additional information.



The description describes all of the options in detail.

The later sections of the man page include more detailed information about the command.

When you're done referencing the manual, you can press q to exit.

6. Quiz: Manual Pages and cowsay


7. Quiz: Using the Manual for Serious Purpose

In fact, ls by default doesn't list any files whose names start with a dot.
This is because files with names starting with dot are often used on Linux for caching and configuration and other things that you don't normally care about.
So the shell will hide them by default because they're not usually interesting.


8. Quiz: Options to ls

You can find out whether a file is a file or directory by looking at this left most column.
There's going to be a d for each directory and a dash for every regular file.



9. Quiz: Researching Commands 


Really, don't run this command. This isn't a joke. Look it up on a search engine and using the manual, and think about what it does.

Another useful tool for researching commands is the apropos command. You can use apropos to find commands relevant to particular keywords.
For example apropos working directory provides a list of commands that somehow work with the current working directory.
apropos is good for refreshing your memory, or for finding new commands to explore!

10. Line Based Programs

Ping lets you test whether another machine on the internet is alive, and how long it takes to send a message to that machine and back.


Ping start and it prints out a line for each successful echo, but it doesn't stop on its own and give you your shell back.

One of the most common ways to tell a program like this to stop to Ctrl+C which sends the interrupt signal.

But some programs have a different behavior.

This allows programs to be chained together on to a pipeline which is a little bit beyond the scope of this course.



When you run a program like this from the terminal, it'll read from your keyboard input and write back to the terminal screen. And very often when your input is done you want to send it and end of file character which you do by typing Ctrl+D.


You can usually use them on files as well as on text that you type in or paste into the terminal.

11. Quiz: Waiting for Input

bc is a simple calculator program, you can use it to do arithmetic like this.
It knows about order of operations and parenthesis and things like that.


12. Full Screen Interactive Programs: less

You can use less to display any file like not just man pages.
Scrolling up and down with arrow keys works the same way as it did before, and man you can also scroll down a page at a time by using the D key or  hitting the Space Bar.
You can scroll up one page at a time with a U key.
If you wan to skip to very last line of the file, you can use the right angle bracket for that.
To get back up to the first line use the left angle bracket.



Less also has built-in search.
Type slash and then the string you'd like to search for.
You can find the next occurrence of the search term by pressing n.
To go back to the previous occurrence, use capital N.
Search terms are case sensitive.

If you're familiar with regular expressions you can use them here unless the search is actually looking for lines that match your pattern.
The regular expression library will interpret other characters in ways you might not expect.

13. Quiz: Editing Files in nano

You can also edit files in the terminal.
This is really handy if you're logged into a remote system and you want to change a configuration file or something.
Instead of pulling the file back to your desktop and editing it with Sublime or another graphically based text editor, you can use an editor that runs right in the terminal.

Like vim and emacs and joe, and many others.
But one of the easiest ones is built into ubuntu, and it's called nano.
It takes the file to edit as a command line parameter.



14. Lesson Outro


In this lesson, you'll work with a number of different programs to interact with a terminal in different ways. Some that display output and immediately exit, some that work with input one line at a time, and some that are fully interactive.



1. About this course

This is an introduction to the Linux command line interface. (Udacity)
This goal of this course is to give you the basic command line tools you'll need to start working with Linux servers.

2. What's a Virtual Machine

Virtual Machine

A virtual machine (VM) is a computer program that simulates a computer. The VM software we're using in this course is called VirtualBox. When you set up your virtual machine you installed Linux on the VM, making Linux the guest operating system. The operating system (OS) that's installed directly on your physical computer is called the host OS.
We use a virtual machine in this course to ensure that everyone is working in an identical environment with the correct programs installed, but there are many other reasons programmers use VMs.
VMs isolate programming projects from everything else on a programmer's computer. The programmer can configure the guest OS by installing programs and customizing settings without disrupting their day-to-day environment.
VMs are also used to simulate the environment that software will be deployed to. Most developers use Windows or Mac OS, but often deploy their code to servers running Linux. Using a Linux VM lets programmers run code on their target platform, without leaving the comfort of their preferred host OS.

Vagrant

Vagrant is a program that makes VMs more convenient to use. For example when you ran vagrant up Vagrant created a VM, installed a guest OS, and configured the guest OS. Vagrant did all of this automatically by following instructions in the Vagrantfile. Automating this process saves time and ensures consistent results.
Vagrant also makes it easy to edit files that are in the VM from programs installed on the host OS. We won't use this feature in this class, but it's very helpful in other Udacity courses and on the job.

Command Line Interface

Programmers encounter many different command line interfaces (CLIs) in their work. Any computer interface where the user enters textual commands and gets textual responses is a CLI. While CLIs vary significantly, proficiency in one will give you a head start learning another. In this class we mostly work with the Linux command line interface in a VM, but in order to access that you need to use your host OS's command line interface. Other command line interfaces you might encounter as a developer are your browser's tools and Python's interactive interpreter.

3. Setting Up Your Own (Virtual) Linux Box

Your own Linux box

To learn the Linux shell, you need a Linux machine to run it on. But we can't really ship a new Linux computer to every one of you. So instead you will set up a Linux virtual machine (VM) on your own computer.
You'll be using the VirtualBox application to run the virtual machine, and the vagrant software to configure it.
This virtual-machine setup is very similar to the ones you will use in later Udacity courses on the Linux platform. So when you get to those courses, you will not need to re-install this software.
Setting the virtual machine up is not complicated, but it will take some time when your computer downloads the Linux OS. Follow the instructions below to set it up before proceeding on in this course.

What's a virtual machine?

A virtual machine is a program that runs on your Windows or Mac computer, and that can run a different operating system inside it. In this case, you'll be running an Ubuntu Linux server system.

1. Install Git

You can skip this step if you are not running Windows, but many other courses use Git, so you may want to install it now.
Download Git from git-scm.com. Install the version for your operating system.
On Windows, Git will provide you with the Git Bash terminal program, which you will use to run and connect to your Linux VM.

2. Find your terminal program

To take this course you will need to use a terminal program, which presents the shell user interface and lets you log in to your Linux VM.
  • Windows: Use the Git Bash program, which is installed with Git (above).
  • Mac OS X: Use the Terminal program, located in your Applications/Utilities folder.
  • Linux: Use any terminal program (e.g. xterm or gnome-terminal).

3. Install VirtualBox

VirtualBox is the software that actually runs the VM. You can download it from virtualbox.org, here. Install the platform package for your operating system. You do not need the extension pack or the SDK. You do not need to launch VirtualBox after installing it.
Ubuntu 14.04 Note: If you are running Ubuntu 14.04, install VirtualBox using the Ubuntu Software Center, not the virtualbox.org web site. Due to a reported bug, installing VirtualBox from the site may uninstall other software you need.

4. Install Vagrant

Vagrant is the software that configures the VM and lets you share files between your host computer and the VM's filesystem. You can download it from vagrantup.com. Install the version for your operating system.
Windows Note: The Installer may ask you to grant network permissions to Vagrant or make a firewall exception. Be sure to allow this.

5. Download the VM configuration file

Make a new folder to keep your workspace for this course. You might call it Shell, but whatever name you pick is OK. Keep track of what folder you created it in (for instance, Desktop).
In the Supporting Materials section of this page, below, you'll find a link to the configuration file, called Vagrantfile. Download this file into the new folder you just created.

6. Run the virtual machine!

Open your terminal program. Type this shell command and press Enter:
cd Desktop/Shell
(If your new folder is called something other than "Shell", or is located somewhere other than "Desktop", change those.)
Then start the VM by running the command vagrant up.
This will make your system download the Linux OS and start up the virtual machine. Unfortunately, this will take a long time on most network connections. Fortunately, you only have to do it once, and the same Linux OS image will work for later Udacity courses too.
Once it is done, run the command vagrant ssh.
And you will be logged in to the virtual machine and ready to do the course exercises!
The Udacity VM is the official shell for this class, but if your computer already has a Unix* shell you can use it if you prefer.
Caveat: Your computer's own shell may differ from the VM in unanticipated ways, and may not have all the programs installed which the VM provides. The recommended environment is the VM.
* if you're running Linux or Mac OS X for instance

지원 자료

4. Quiz: Log In and Break Stuff

You've got a screen full of stuff, and at the bottom you've got a line with a $ at the end.
That last line is the shell prompt, and it means that the system is waiting for you give it a command.

Now did you get something maybe like this, with a right angle bracket?
That''ll happen if you type something with a single quote mark, or with a parenthesis, or brace. In order to get your shell prompt back, with an arrow message, you have to type the matching quote mark, parenthesis, or brace.
Then you'll get a command not found and you get your show prompt back.
Now, if you typed exit, or if you type Ctrl+D, you logged out.

And if you got into some other sort of weird state where you're not sure what's going on, you can usually type Ctrl+C look for

5. In the VM or out of the VM?

In the VM or out of the VM?

We've set this course's exercises up to work in the virtual machine (VM) that you set up using the vagrant program. If you get logged out of the VM, you may end up typing shell commands in to your regular operating system instead of to the Linux system that we've set up for the course. Some commands won't work, and some files probably won't be where the course expects them to be.

Getting logged out

If you type the command exit into the shell, or if you type Control-D, you will see a message like this:
logout  
Connection to 127.0.0.1 closed.
This just means that you got logged out. After logging out, you won't be in the VM any more.
To get back into the VM, use the command vagrant ssh.

If vagrant ssh doesn't work

If you get a message like this:
VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.
This means that the VM program is not running, for instance because you rebooted your computer. This is just fine and it doesn't mean you've lost any work. Just run vagrant up to bring the VM back up, then vagrant ssh to log in.
This will not take as long as the first time you ran it, because it won't need to download the Linux OS.

If vagrant up doesn't work

If you get a message like this:
A Vagrant environment or target machine is required to run this
command. Run `vagrant init` to create a new Vagrant environment. Or,
get an ID of a target machine from `vagrant global-status` to run
this command on. A final option is to change to a directory with a
Vagrantfile and to try again.
That means that vagrant can't find the configuration file you downloaded. Go back to the instructions, check to be sure that you did step 5, and then do step 6 again.

Multiple terminals

If you open up more than one terminal window, only the one(s) that you ran vagrant ssh in will be connected to your Linux OS for this course. The others will be connected to your regular OS.
(It's actually really normal for Linux users to have to carefully keep track of which terminal windows are connected to which machines. Don't panic. Just look for whether "vagrant" appears on the command line.)

6. Quiz: Commands That Work

Well, here is probably one of the best known Unix shell commands, ls.
is an abbreviation for list, and it gives you a list of items in your current directory.
You can think of this as listing the files in the folder that the shell is currently looking at.


curl : Download a file from the web


7. Quiz: What can you do in the terminal?



8. Quiz: The Terminal Interface

It typically shows informational messages, output from previously run commands, and a prompt for entering new commands.


If you'd like to use the cowsay program outside of the VM, on your own computer, you can install it like this:
Ubuntu and Debian users: sudo apt-get install cowsay
Redhat and CentOS users: sudo yum install cowsay
OS X users: brew install cowsay
(This requires the homebrew, a third party package manager for OS X, http://brew.sh/)
Arch Linux users: sudo pacman -S cowsay
Note: You typically need to be a “superuser” to install new software, that’s why these install commands begin with the sudo command . You can learn more about sudo in our Configuring Linux Web Servers course or on Wikipedia.


9. The Terminal vs The Shell

The terminal is a program that draws text in a window, and lets you type things in on a keyboard.
Technically it's called a terminal emulator, since it acts like one of those old school hardware terminals. It displays output on the screen and it accepts input from.
But the terminal itself doesn't know what to do that input.
It needs another program to do that.

In this case, that program is the shell.
When you type things in the terminal just sends what you type to that separate program.

Different shells

Unix and Linux programmers over the years have written many different shell programs. You can read more about them on Wikipedia: the original Bourne shell or sh; the C shell or csh; the Korn shell or ksh; the Z shell or zsh; as well as the bash shell that this course uses.
Different systems may have different shells installed by default. Most Linux systems, and Mac OS X, default to bashfor interactive shells. However, the most common default shell for scripting (shell programming) is classic sh. BSD Unix systems usually default to sh or ksh.
Almost everything in this course will work the same in any of these shell programs. The exception is one of the file matching (globbing) syntaxes at the end of Lesson 3.


10. Quiz: Try More Commands




11. Quiz: What is a Shell Command Anyway

Functions are used to organize a program while Shell Commands are used to run programs.


12. Quiz: Reading the Output of a Command



13. Quiz: Identify User Input



14. Lesson Outro

You've learned a few shell commands, and you've gotten some practice using the terminal and learning what sorts of things to expect from it.