(x)slates
https://xslates.com/
Recent content on (x)slates
Hugo  gohugo.io
2019 (x)slates © All Rights Reserved.
Mon, 19 Aug 2019 00:00:00 +0000

Applications of Nonlinear Functions
https://xslates.com/post/applicationsofnonlinearfunctions/
Mon, 19 Aug 2019 00:00:00 +0000
https://xslates.com/post/applicationsofnonlinearfunctions/
import matplotlib.pyplot as plt Let’s look at some textbook applications of nonlinear functions (and models).
Stock Prices and Prime Rate It is theorized that the price per share of a stock is inversely proportional to the prime (interest) rate. In January 2010, the price per share \(S\) of Apple Inc. stock was $205.93, and the prime rate \(R\) was 3.25%. The prime rate rose to 4.75% in March 2010. What was the price per share in March 2010 if the assumption of inverse proportionality is correct?

Mathematical Modeling and Curve Fitting in Python
https://xslates.com/post/mathematicalmodelingandcurvefitting/
Sun, 18 Aug 2019 00:00:00 +0000
https://xslates.com/post/mathematicalmodelingandcurvefitting/
There are a handful of families of functions that form the basic toolkit that we use to model data. Curve fitting is the process of constructing a curve, or mathematical function, that best fits a series of data points.
The simplest way to decide which, if any, type of function fits a dataset is to examine a scatterplot of the data. If we can spot a general pattern that looks like any of the functions we would like to model with, then we can try to fit the data with it.

Nonlinear Models, Polynomial Functions
https://xslates.com/post/nonlinearmodelspolynomialfunctions/
Fri, 16 Aug 2019 00:00:00 +0000
https://xslates.com/post/nonlinearmodelspolynomialfunctions/
import matplotlib.pyplot as plt import numpy as np import math Linear and quadratic functions are part of a general class of polynomial functions. A polynomial function is given by
\[f(x) = a_nx^n + a_{n1}x^{n1}, + \dots + a_2x^2 + a_1x^1 + a_0\]
where \(n\) is a nonnegative integer and \(a_n, a_{n1}, \dots, a_1, a_0\) are real numbers, called the coefficients. The number \(a_0\), which is not multiplied by a variable, is called a constant.

Nonlinear Models, Quadratic Functions
https://xslates.com/post/quadraticfunctions/
Thu, 15 Aug 2019 00:00:00 +0000
https://xslates.com/post/quadraticfunctions/
A quadratic function is given by
\[f(x) = ax^2 + bx + c\]
where \(a \neq 0\). The graph of a quadratic function is called a parabola such that:
it always has a cupshaped curve it opens upward if \(a > 0\) or opens downwards if \(a < 0\) it has a turning point, or vertex, whose coordinate is \(x =  \frac{b}{2a}\) the vertical line \(x = \frac{b}{2a}\) (which is not part of the graph) is the line of symmetry You could think of the vertex as the first \(y\)value.

Determining the Most Frequent Items in a Sequence
https://xslates.com/post/determiningthemostfrequentitemsinasequence/
Tue, 13 Aug 2019 00:00:00 +0000
https://xslates.com/post/determiningthemostfrequentitemsinasequence/
If you have a sequence of items, and want to determine the most frequently occurring items in it, you can use the collections.Counter class.
words = [ "look", "into", "my", "eyes", "look", "into", "my", "soul", "it's", "not", "uncommon", "to", "see", "my", "mood", "in", "my", "eyes" ] from collections import Counter wcount = Counter(words) wcount.most_common(3) ## [('my', 4), ('look', 2), ('into', 2)] You can also check how many times a given item appears in the sequence.

Naming Slices
https://xslates.com/post/namingslices/
Mon, 12 Aug 2019 00:00:00 +0000
https://xslates.com/post/namingslices/
The builtin slice function creates a slice object tha can be used anywhere a slice is allowed. There are three arguments to to fill in, start, end, and step. The object returns the values at the indices specified in the arguments. If you only provide one argument, by default, slice will interpret it as the end.
items = [0, 1, 2, 3, 4, 5, 6] section = slice(2, 4, 1) initial = slice(4) items[section] ## [2, 3] items[initial] ## [0, 1, 2, 3] More on slices can be found here.

Applications of Linear Functions
https://xslates.com/post/applicationsoflinearfunctions/
Sun, 11 Aug 2019 00:00:00 +0000
https://xslates.com/post/applicationsoflinearfunctions/
import matplotlib.pyplot as plt In this post, we’ll look at some textbook applications of linear functions. Although these are contrived problems taken from a book, they shed some light on how to think about using linear functions to solve real business problems.
Highway tolls Since heavier vehicles are responsible for more of the wear and tear on highways, drivers should pay tolls in direct proportion to the weight of their vehicles.

Slope and Linear Functions
https://xslates.com/post/slopeandlinearfunctions/
Sat, 10 Aug 2019 00:00:00 +0000
https://xslates.com/post/slopeandlinearfunctions/
The graph of \(y = c\), or
\[f(x) = c\]
a horizontal line is the graph of a function. We call such a function a constant function. A constant function is one whose output value is the same for every input value. The graph of \(x = a\) is a vertical line, and \(x = a\) is not a function.
import matplotlib.pyplot as plt def constant(x): return 4 y_values = [constant(x) for x in range(1,10)] plt.

Calculating with Dictionaries in Python
https://xslates.com/post/calculatingwithdictionaries/
Thu, 08 Aug 2019 00:00:00 +0000
https://xslates.com/post/calculatingwithdictionaries/
To perform useful calculations on the contents of a dictionary, it is often useful to invert the keys and values of the dictionary using zip(), which creates an iterable tuple of a dictionary’s keys and values.
prices = { "ACME": 45.23, "AAPL": 612.78, "IBM": 205.55, "HPQ": 37.20 } prices ## {'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.2} zipped = zip(prices.values(), prices.keys()) for v, k in zipped: print(v, k) ## 45.

Finding Commonalities in Dictionaries in Python
https://xslates.com/post/findingcommonalitiesindictionaries/
Wed, 07 Aug 2019 00:00:00 +0000
https://xslates.com/post/findingcommonalitiesindictionaries/
A Python dictionary is a mapping between a set of keys and values. The keys() method supports common set operations such as unions, intersections, and differences. Same goes for the items() method. However, that’s not the case with the values() method since the values of a dictionary are not guaranteed to be unique. Based on these notions, we can compare dictionaries and see what they have in common with basic set operations.

Cumulative & Annualized Returns
https://xslates.com/post/cumulativeannualizedreturns/
Tue, 06 Aug 2019 00:00:00 +0000
https://xslates.com/post/cumulativeannualizedreturns/
The cumulative return of an investment is the aggregate return that an investment has gained or lost over time (it can be both positive or negative). If you have OpenHighLowClose stock data, you can compute cumulative returns on its adjusted price as dividends, and stock splits will lead to incorrect results.
\[R_c = \frac{P_c}{P_i}  1\]
where \(R_c\) is the cumulative return, \(P_c\) is the current price, and \(P_i\) is the initial price.

Keeping Python Dictionaries in Order
https://xslates.com/post/keepingpythondictionariesinorder/
Mon, 05 Aug 2019 00:00:00 +0000
https://xslates.com/post/keepingpythondictionariesinorder/
If you want to control the order of items in a dictionary, you can use an OrderedDict. It preserves the original insertion order of data. It’s a useful construct for when you want to seriealize or encode in different formats. As a note, the structure of an OrderedDict (a doubly linked list), means that these dictionaries are at least twice as heavy as normal dictionaries, meaning they require more memory.

Mapping Keys to Multiple Dictionary Values in Python
https://xslates.com/post/mappingkeystomultipledictionaryvalues/
Fri, 02 Aug 2019 00:00:00 +0000
https://xslates.com/post/mappingkeystomultipledictionaryvalues/
If you want to create a dictionary where you map keys to more than one value (a “multidict”), you must store these values into another container like a list or set. Use lists if you want to preserve the order of insertions, use sets if you don’t want to keep duplicates. It all depends on your use case, use the container with the characteristics that fit your needs.
d = { "a" : [1, 2, 3], "b" : [4, 5] } d ## {'a': [1, 2, 3], 'b': [4, 5]} e = { "a" : {1, 2, 3}, "b" : {4, 5} } e ## {'a': {1, 2, 3}, 'b': {4, 5}} You can also use defaultdict which allows you to write cleaner code.

Control Limits in Analytics
https://xslates.com/post/controllimitsinanalytics/
Wed, 31 Jul 2019 00:00:00 +0000
https://xslates.com/post/controllimitsinanalytics/
Control limits are visual references that help you detect if a statistic (or time series) is getting “out of control.” I first saw them referenced on Avinash Kaushik’s blog.
You can use a metric’s standard deviation to plot a bounded region, (\(\pm 3 \sigma\)), within which the statistic is assumed to behave normally. It’s not wandering too far off from its mean. The reason why this is useful in analytics is that more often than not, people will latch onto meaningless fluctuations and believe them to be worthy of attention.

Notes on Trading #2
https://xslates.com/post/notesontrading2/
Tue, 30 Jul 2019 00:00:00 +0000
https://xslates.com/post/notesontrading2/
StopLosses and TakeProfits Having StopLoss Orders (S/L) and TakeProfit Orders (T/P) allows you to compute a Risk/Reward Ratio for you trades. If you’re trading at the weekly level, these types of orders might let you ride upward trends more reliably than if you did it intraday. Volatile assets might hit a T/P target way earlier than you’d hope, which might be a good thing if you want to dip in and out.

Lambda Functions in Python
https://xslates.com/post/lambdafunctionsinpython/
Mon, 29 Jul 2019 00:00:00 +0000
https://xslates.com/post/lambdafunctionsinpython/
In Python, lambda functions are anonymous functions (meaning functions without a name).
The syntax for lambda functions is lambda arguments: expression.
half = lambda x: x / 2 half(14) ## 7.0 split = lambda s: list(s) split("hi") ## ['h', 'i'] pow = lambda x, y: x ** y pow(2, 3) ## 8 Lambda functions come in handy when you want to create a function that you won’t reuse. You can avoid defining the function and use a lambda function instead.

Heapq Algorithm
https://xslates.com/post/heapqalgorithm/
Thu, 25 Jul 2019 00:00:00 +0000
https://xslates.com/post/heapqalgorithm/
You can use the heapq module to perform operations like finding the nlargest and nsmallest items in a collection. A heap is a treebased data structure for which every parent node has a value less than or equal to any of its children. You can use it to make queues.
import heapq nums = [3, 4, 7, 1, 4, 9, 0, 2, 4, 6, 7, 1] print(heapq.nlargest(3, nums)) ## [9, 7, 6] print(heapq.

Assignment Operators in Python
https://xslates.com/post/assignmentoperatorsinpython/
Tue, 23 Jul 2019 00:00:00 +0000
https://xslates.com/post/assignmentoperatorsinpython/
Python’s assignment operators are used to store data into variables. The convention +=, = and the likes are used to update the value of a set variable with the value in the right operand.
a = 5 a += 2 a ## 7 a =2 a ## 5 a **= 2 a ## 25 Here’s a useful reference.

Deque Iterables in Python
https://xslates.com/post/operationsoniterablesinpython/
Tue, 23 Jul 2019 00:00:00 +0000
https://xslates.com/post/operationsoniterablesinpython/
If you want to keep a limited history of the last few items seen during iteration or other processes, you can use a deque which is a faster container, \(O(1)\), than a list, \(O(N)\). A deque allows you to keep a limited history of items, as in a queue.
from collections import deque import numpy as np d = deque(maxlen=3) d.append(1) d.append(2) d.append("text") d ## deque([1, 2, 'text'], maxlen=3) If we add more items, the earlier ones get bumped.

Unforgiving Math of Stock Value Loss
https://xslates.com/post/painfulmathofstockvalueloss/
Sun, 21 Jul 2019 00:00:00 +0000
https://xslates.com/post/painfulmathofstockvalueloss/
You buy a product for $25.00. The store has a sale and drops the price by 30%. Then they raise the price again by 30%, so now the product costs $23.43. What happened? Well, it’s a typical case of misleading percentages. The store applied the price increase to the discounted price.
Percentage calculations What is 4% of 10? 20 is what percentage of 30?
Questions like these can be answered by the following equations.

Unpacking Iterables into Variables in Python
https://xslates.com/post/unpackingiterablesintovariablesinpython/
Fri, 19 Jul 2019 00:00:00 +0000
https://xslates.com/post/unpackingiterablesintovariablesinpython/
You can unpack sequences and iterables into collections of variables by using an assignment operator.
p = (4, 5) x, y = p x ## 4 y ## 5 For this to work, the number of variables must match the number of elements in the iterable. Unpacking works on lists, tuples, strings, dictionaries, iterators, generators, files, and more data types.
data = ["BIGINC", 50, 355.6, (2019, 5, 12)] name, shares, price, date = data name ## 'BIGINC' shares ## 50 price ## 355.

Notes on Documentation
https://xslates.com/post/notesondocumentation/
Sun, 14 Jul 2019 00:00:00 +0000
https://xslates.com/post/notesondocumentation/
I’ve been trying to understand Backtrader recently, and I was a bit taken aback by its poor documentation; which made me reflect on how I should not document my projects in the future. These are just a few of the issues I’ve encountered. While I didn’t explore the whole documentation, these problems were enough to turn me off to the project entirely because, unfortunately, I’m to busy to try and figure out the platform right now.

Notes on Trading
https://xslates.com/post/notesontrading/
Fri, 05 Jul 2019 00:00:00 +0000
https://xslates.com/post/notesontrading/
Over the past few months, I’ve been listening to a podcast called Chat with Traders where you get to listen to professional traders share how they work.
Having no experience in the trading world, I thought it would be a good idea to try and pick up the topic by listening to conversations about it, rather than books. I’m not sure I’ll listen to all episodes but here are some lessons I’ve picked up from the podcast so far.

Notes on Work
https://xslates.com/post/notesonwork/
Thu, 23 May 2019 00:00:00 +0000
https://xslates.com/post/notesonwork/
Here are some thoughts on my experience working in the tech industry.
You can’t manufacture team culture. It’s the result of organic habits and behaviors that a group of people develops over time, which is also the reason why workshops and topdown change initiatives meet so much resistance.
Don’t have a meeting if you don’t have an agenda.
If you can end a meeting early, do it.
Don’t use meetings for status updates.

About
https://xslates.com/about/
Tue, 01 Jan 2019 00:00:00 +0000
https://xslates.com/about/
My name is Will. This is my site, there are many like it, but this one is mine.
I make my living as an analyst and I mostly write about technology, business, and mathematics.
For speaking, consulting, or just to get in touch, you can contact me here.