Contents

Part 1: A Primer on Classical Logic

Part 2: The Real World

Part 3: An Introduction to Fuzzy Logic

Part 4: Modeling Trading Decisions

Conclusion

1- A Primer on Classical Logic

Thanks to the Abbasid empires goal of translation and preservation of Aristotles texts, we maintain the robust two thousand years old Aristotelian logic known to us today as: Categorical Logic. Essentially, it forms the rubric of classical or traditional logic.

Over the years categorical logic gave birth to formal logic — which sees heavy usage in discrete mathematics. You can brush through discrete mathematics concepts and the topic of logic in this article:

There are two important types of deductive inference in categorical logic we’d like to refer to; and they are:

  • Modus Potens
  • Modus Tollens

Let us utilize a framework of setting a rule, stating an observation, and concluding a result. Since we are using categorical logic, we have the bivariate truth values of either True or False for which every proposition can take. This is called the bivalence principle.

Also, let A be a proposition and B be a propositions.

Modus Potens

  • Rule: If A is True, then B is True.
  • Observation/Antecedent: A is True
  • Conclusion/Consequent: B is True

This rule is an implication. In essence, A implies B (but not vice versa).

Modus Tollens

  • Rule: If A is True, then B is True.
  • Observation/Antecedent: B is False
  • Conclusion/Consequent: A is False

This rule is also an implication. In essence, A implies B (but not vice versa).

It is important to keep in mind that we can numerically assign the value of 1 for True, and the value of 0 for False to any proposition . Enter Boolean logic, which is the basis of all computing. In boolean logic the two absolute truth values of True and False are represented by 1 and 0 respectively. These absolute values are called crisp values.

A crisp value is a precise/distinct value that has strict boundaries i.e. True or False, with no intermediate values.

Allow us to further expand on the idea of assigning bivariate numerical truth values 0 and 1 — such as that in Boolean logic. Instead of regarding them as just truth values, we can use them as membership values for propositions (our elements) within a set called the universe of elements X. A set in our case, is a collection of distinct listed propositions xcalled elements that are either strictly True or strictly False.

Suppose we have a set A. We can assign a membership value for each element in that set. If the element is fully a member of the set, then it has a membership value of 1. If however, it is not a member of the set, then it has a membership value of 0.

2- The Real World

The environment around us is much more complicated. Human decisions are not made due to a series of 1’s and 0’s. In addition, the world around us is vague and hence at times we cannot truly agree on what is fast or quick or slow. These descriptions of ‘facts’ are very subjective.

You’ll find the short story below an interesting read. The story talks about how two friends set aside their differences in understanding qualitative descriptions and come to common terms to objectively describe real life in quantitative terms:

Human communication is littered with vagueness in language. Humans can understand each other with varying interpretability but never truly a completely unambiguous interpretability with exact mirroring of the intended meaning.

By this I mean, what I think hot weather is, is different from how you perceive hot weather as. In Kuwait, the temperature can reach 50 degrees celsius — for me, this is hot. For you? You might call your weather hot if it started from 30 degrees and above, and therefore 50 may not even be on your scale, alternatively, you might even classify it as extremely hot.

Because of this ambiguity and vagueness in the real world, in 1965 Lotfi Zadeh recognized that there must be a consistent way (which is not been characterized by traditional logic) to capture the imprecision of the real world — hence Fuzzy Logic was born.

3- An Introduction to Fuzzy Logic

First and foremost, let us define the term Fuzzy. Fuzzy is when something is unclear or vague. Unlike boolean logic where we only have two absolute values: 1 and 0, in fuzzy logic we have intermediate values which are partially true and partially false.

In order for us to grasp a working understanding of fuzzy logic, we will go through the following:

  • what is fuzzy logic?
  • the aim of fuzzy logic
  • what is a linguistic variable?
  • the difference between a classical set and a fuzzy set
  • what is a membership function?
  • fuzzy systems and controllers
  • the difference between fuzzy logic and probability

Before we delve into the topics above, we shall rename some concepts, and provide the glossary below:

  • membership grade = truth value =degrees of membership = degrees of truth
  • universal set = universe of discourse (U)= a collection of all the fuzzy sets and subsets
  • fuzzy set = a proposition described as a set containing a set of terms with varying degrees of membership
  • A fuzzy subset of X (the universal set) = is defined as a function that maps X to the a value in the interval [0,1]
  • A predicate = “In fuzzy logic, predicates are the characteristic functions of a probability distribution. That is, the strict true/false valuation of the predicate is replaced by a quantity interpreted as the degree of truth.” — wikipedia

3.1 What is fuzzy logic?

It is a multivalued logic with truth represented by a value on the closed interval [0,1]. It involves intermediate possibilities between the boolean True and False. It allows for flexible reasoning.

3.2 The aim of fuzzy logic

The original aim was to compute linguistic variables which were words and sentences in a natural language. The purpose of this was to bridge the gap between words and numbers which would then allow for the modeling of human thinking/reasoning. The goal was to come up with acceptable reasoning, if not accurate reasoning.

3.3 What is a linguistic variable?

Inevitably even when you use linguistic variables, subjectivity seeps in.

The dependancies:

  • To assign the linguistic variable to the set of terms, we need syntax
  • To assign the terms to the values we need semantic

3.4 The difference between a classical set and a fuzzy set

Fuzzy logic is closely related to sets, just like traditional bivariate logic. In fact, classical sets are indeed subsets of fuzzy sets. This means that bivariate logic are special cases of fuzzy logic.

In traditional logic, every predicate creates a clear distinction between the objects (terms) they apply to, and those that they don’t apply to. On the other hand predicates in fuzzy logic are vague because they showcase borderline cases.

In effect, fuzzy sets allow for elements to belong to a given set with a certain degree — partial membership.

Let us first define a classical set B, in a non-empty space X (universe of discourse), we can hence define it as a membership function MB (characterizing function) as such:

Let there be a fuzzy set A in a non-empty space X .

Also let a μA be a membership function in the interval [0,1].

Membership function μA quantifies the degree of membership of X to A.

We graphically show the membership functions of both fuzzy and classical sets, which also illustrates their imprecise and precise properties of membership respectively:

To solidify our understanding of the difference between the two types of sets, consider the following example; the classical set of all natural numbers, call it N:

N = {1, 2, 3, 4, 5, 6, 7, …}

Elements in set N are called members. Elements not in set N are called non-members. In a classical set such as this, membership is an absolute property, an element is either a member or not a member — no partial membership.

Lets ask a questions and answer them:

  • Is 1 a member? Yes
  • Is 10,000 a member? Yes
  • Is 0 a member? No
  • Is -7 a member? No
  • How many members are there (cardinality)? Infinite number of members

There are two types of fuzzy sets:

  • a fuzzy set A where the universe of discourse U is discrete and finite
  • a fuzzy set A where the universe of discourse U is continuous and infinite

We represent each of these types as so:

Operations on fuzzy sets differ from those on classical sets.

We can further categorize fuzzy sets into two kinds:

  • Normal fuzzy set: is a fuzzy set where its membership function has at least one element x in the universe of discourse equal to 1.
  • Subnormal fuzzy set: is a fuzzy set where its membership function has no element x in the universe of discourse equal to 1.

For two fuzzy sets A and B, the universe of discourse U and an element x of the universe:

  • Union: μA AND μB = max(μA, μB)
  • Intersection: μA OR μB = min(μA, μB)
  • Complement (NOT μA) = 1 — μA

3.5 What is a membership function

Membership functions characterize fuzziness, whether discrete or continuous. The membership function (fuzzy subsets) is a graph that defines how each point in the input space is mapped to a membership value in the closed interval [0,1]. It allows for the quantification of linguistic terms and represents a fuzzy set graphically.

A membership function A on the universe of discourse X is defines as μA mapped to [0,1]. This means that they are essentially normalized functions.

There are three key situations we need to be aware of:

  • μA(x)=1 means full membership of the element x to the fuzzy set A
  • μA(x)=0 means that no element x belongs to the fuzzy set A (non-member)
  • 0<μA(x)<1 means that the element x has a partial membership/truth value to the fuzzy set A

Each truth value is a term that has a membership value, and each term has a membership function.

Below are examples of ways we can identify terms and hence create a membership function for them:

Key features of membership functions

These regions are synonymous are graphical representations of the three key situations — reiterated as follows:

  • Core: is a region that comprises of elements x of the universe of discourse U such that μA(x)=1
  • Support: is a region that comprises of elements x of the universe of discourse U such that μA(x)≥0
  • Boundary: is a region that comprises of elements x of the universe of discourse U such that 0<μA(x)<1

3.6 Fuzzy systems and controllers

Fuzzy logic systems use membership functions, fuzzy sets, and varying degrees of truth to model vague environments (many systems in the real world) by approximating the relationship between inputs and outputs via interpolation. A fuzzy controller just takes it one step further and utilizes the output as a decision to carry out on the system.

There are four steps in the process of a fuzzy logic system/controller:

  • Fuzzification: converts crisp values to fuzzy sets
  • Constructing the rules: either from experts or from data via clustering
  • Inference: Uses the rule base to imply the rules from the rules base to output a fuzzy set
  • Defuzzification: takes fuzzy sets as inputs then converts the resulting functions back into scalars — crisp values. (We can use the centroid method to do so).

Step 1: Fuzzification

We convert human natural language describing a certain system into linguistic variables. Then we convert the scalar crisp values which are our inputs and output them as fuzzy sets.

Step 2: Constructing the rules

The first thing we do is we ask how do we get the rules? Simple, there are two major ways to do so:

  • we get them from experts in certain domains which lapse with our system to be modeled
  • or, we can use existing data;

We can make use of data via clustering. Essentially, we get the center of classes and from there we define the rules.

Suppose we have an ever evolving system that changes over time (financial data?) this would imply that the rules might gradually change, no? We deal this phenomenon in evolving fuzzing rules, utilizing concept drift.

These rules can be aptly presented in a table that does not necessarily contain all the possible rules, merely only the possible.

You can also determine every rules weight (0 to 1). Usually the weight is one, so it has no effect on the implication process in the inference engine.

Once the rules are set, then we insert them into the rule base, so that we may use them when the fuzzy sets are inputed into the inference engine.

Step 3: Inference

After the crisp values are fuzzified, the fuzzy set is inputed into an inference engine. The inference engine helps evaluate the degrees of match in between the fuzzy input set and the set of rules in the rule base.

The outline of the inference process is as follows:

  • Input the fuzzy sets (the crisp values that have been fuzzified)
  • determine degree of match between the antecedent (single crisp number) and the rules
  • If the antecedent of a rule has more than one part, then logical operators (set operators) are used to obtain one number
  • each number (each antecedent) has an associated function
  • these associated membership functions are then used as an input for the implication process
  • this process, simply: uses logical operators on the membership functions of the antecedents to produce a consequent which has its graph reshaped; the output is a fuzzy set
  • then all these fuzzy set outputs (which are outputs of each rule) are combined into one fuzzy set for each output variable.

Step 4: Defuzzification

In the defuzzification process, the input is a fuzzy set. Its output however, is a single number. The defuzzification method worth mentioning is the centroid method (centre of mass).

Suppose we have a union of two fuzzy sets, A and B, denoted as Z :

In the centroid method, the defuzzified value Z* (which is the centre of mass) is given by:

3.7 The difference between fuzzy logic and probability

Probability is associated with events occurring or not occurring, whilst fuzzy logic is about the vagueness of the truth value of facts. Unlike probability, fuzzy logic is about partial truths rather than partial knowledge.

4- Modeling Trading Decisions

There are two primary ways human traders make trading decisions. The first way is called fundamental analysis. It is when a trader analyzes company centered events, industry conditions, and economic indicators/news. It is well suited for long-term buy and hold strategies.

The second way is called technical analysis. As the name suggests, it is a collection methods used to analyze historical price charts and market statistics to determine patterns (and ratios) and then form a prediction of the future price trajectories. It is more an art than a science.

We will model trading decisions based on some indicators in technical analysis.

4.1 Technical Analysis

There are important assumptions implicitly considered in technical analysis, and they are:

  • Market action (in terms of price and volume) discounts everything else
  • Prices move in trends
  • Trends persist
  • History repeats itself

4.2 Technical Indicators

Technical analysis is home to a multitude of different indicators. A combination of these indicators can be used to form a strategy that effectively signal a buy or sell signal.

We will select three technical indicators to generate trading signals which will help us create rules which will be placed in our rule base:

  • Short-term moving average (MAt(s))
  • Long-term moving average (MAt(t))
  • Relative Strength Index (RSI): which is a type of momentum oscillator

We will use the two moving averages to generate a single trading signal using the Double Crossover Method (DCM).

4.3 Setting Up Our Strategy

As mentioned above we will utilize three indicators to create trading rules.

Caveats

Our strategy is only applicable to a stock that holds these two properties:

  • Highly volatile
  • Liquid

To be more specific we need to quantify what stocks possess these properties.

By highly volatile it means we’re looking for high beta stocks. The beta of the market is 1. If a stock has a beta above 1 then it is a highly volatile stock.

Rough Rule 1: If a stock has a beta greater than 1, then it is a highly volatile stock.

One of the best measures for liquidity is the bid-ask spread. The bid price is the highest price a buyer will pay for a stock. The ask price is the lowest price a seller will accept to sell the stock. The difference between the bid and ask price is the bid-ask spread. A low bid-ask spread means a stock is liquid, whilst a high bid-ask spread means a stock is illiquid.

To construct a rule of thumb for what constitutes as a large or small bid-ask spread, we shall normalize it into a number between 0 and 1. Basically we will take the bid-ask price of a single share of a stock and divide it by its ask price.

Mathematically speaking,

Let the normalized bid-ask spread n at time t be n(t)=(a(t) — b(t))/a(t), where n(t)∈[0, 1].

Let us based on hypothetical experience create the following rule:

Rough Rule 2: If a stock has a n(t)≤ 0.015, then it is a liquid stock.

The DCM

Let k be the long-term averaging period and s be the short-term averaging period, k>s, Indicatort(s,k) = MAt(s) — MAt(k),

Also, let t be days so that k=200 days and s=50 days.

With this in mind, we can create the following two rules,

Rough Rule 3: If Indicatort(50,200)>1, then it indicates a buy signal.

Rough Rule 4: If Indicatort(50,200)<1, then it indicates a sell signal.

The RSI

The Relative Strength Index is a metric for price changes and the speed/rate at which they change. RSI(t)∈[0, 100], we’ll compare t=14;

Rough Rule 5: If RSI(t)>50, then there is an uptrend momentum (buy signal).

Rough Rule 6: If RSI(t)<50, then there is a downtrend momentum (sell signal).

Rough Rule 7: If RSI(t)≥70, then the stock is overbought (neutral).

Rough Rule 8: If RSI(t)≤30, then the stock is oversold (neutral).

4.4 Modeling Our Strategy

The model we will build is essentially called a MISO, Multiple Inputs Single Output. Our output will be a number between 0 and 1 which is mapped onto an output membership function. Here we will also make our rules much more concrete.

Now,

Each stock will be described by the following set of input variables:

  • beta as X₁
  • normalized bid-ask spread n(t) as X₂
  • double crossover method value Indicatort(50,200) as X₃
  • relative strength index RSI(t) as X₄

Then we will construct three rule blocks:

  • Rule block 1: Validity of strategy
  • Rule block 2: Strength of signals
  • Rule block 3: Confidence and trading decision

Threshold Values for Membership Function of Input Variables:

Set of Rules for Rules Block 1:

If X₁ is Highly volatile and X₂ is Illiquid then Validity of strategy is Slight

If X₁ is Highly volatile and X₂ is Liquid then Validity of strategy is Very

If X₁ is Lowly volatile and X₂ is Illiquid then Validity of strategy is Not

If X₁ is Lowly volatile and X₂ is Liquid then Validity of strategy is Slight

Set of Rules for Rules Block 2:

If X₃ is Buy signal and X₄ is Overbought then Strength of signals is Average buy

If X₃ is Sell signal and X₄ is Overbought then Strength of signals is Neutral

If X₃ is Buy signal and X₄ is Upward trend then Strength of signals is Strong buy

If X₃ is Sell signal and X₄ is Upward trend then Strength of signals is Neutral

If X₃ is Buy signal and X₄ is Downward trend then Strength of signals is Neutral

If X₃ is Sell signal and X₄ is Downward trend then Strength of signals is Strong sell

If X₃ is Buy signal and X₄ is Oversold then Strength of signals is Neutral

If X₃ is Sell signal and X₄ is Oversold then Strength of signals is Average sell

Set of Rules for Rules Block 3:

If Validity of Strategy is Slight and Strength of signals is Average buy then Low confidence buy decision

If Validity of Strategy is Slight and Strength of signals is Strong buy then Moderate confidence buy decision

If Validity of Strategy is Slight and Strength of signals is Neutral then No confidence trade decision

If Validity of Strategy is Slight and Strength of signals is Strong sell then Moderate confidence sell decision

If Validity of Strategy is Slight and Strength of signals is Average Sell then Low confidence sell decision

If Validity of Strategy is Very and Strength of signals is Average buy then Moderate confidence buy decision

If Validity of Strategy is Very and Strength of signals is Strong buy then High confidence buy decision

If Validity of Strategy is Very and Strength of signals is Neutral then No confidence trade decision

If Validity of Strategy is Very and Strength of signals is Strong sell then High confidence sell decision

If Validity of Strategy is Very and Strength of signals is Average Sell then Moderate confidence sell decision

If Validity of Strategy is Not and Strength of signals is Average buy then No confidence trade decision

If Validity of Strategy is Not and Strength of signals is Strong buy then No confidence trade decision

If Validity of Strategy is Not and Strength of signals is Neutral then No confidence trade decision then No confidence trade decision

If Validity of Strategy is Not and Strength of signals is Strong sell then No confidence trade decision

If Validity of Strategy is Not and Strength of signals is Average Sell then No confidence trade decision

4.5 Using Matlab to Simulate Our Model

Once again faithful Matlab comes to the rescue. We will utilize a very important built-in app within Matlab called Fuzzy Logic Toolbox. It basically provides a UI to create the model, rather than writing up code — hence saving time.

The order in which we built our model:

Step 1: Specified the input variables

Notice in the bottom left box we can select our methods

Here we just created four inputs linking to the inference engine and finally to the output where it’ll be defuzzified. Notice here we did not create 3 rule blocks.

Step 2: Specified the associated membership functions for each input and output variable

An example of one of the inputs (beta) membership function graph

Step 3: Set up the rules for the inference engine to use

We had a set of 29 rules

Remember what we said about not creating 3 rule bases? Well, it wasn’t necessary as we can simple create a series of ANDs, where each pair are an output of rule block 1 and 2.

After building the model, we decided to use AAPL stock data as our input to determine a trading decision.

The raw data for AAPL stock as of 10th of July 2020:

  • MA(50)=334.38
  • MA(200)=287.72
  • RSI(14)=73.21
  • Beta (5Y Monthly)=1.18
  • Bid price=383.75
  • Ask price=383.90

The processed data:

  • Normalized Bid-Ask Spread (Magnitude)=0.00039
  • Indicatort(50,200)=46.66

So, the data we will input (in our linguistic variable):

  • Beta (X₁)=1.18
  • Normalized Bid-Ask Spread (X₂)=0.00039
  • Indicatort(50,200) (X₃)=46.66
  • RSI(14)(X₄)=73.21

Lets rewrite it in the proper notation and simulate it in our model:

[1.18, 0.00039, 46.66, 73.21]

And viola! We get a decision = 0.603; keep in mind our range is from 0 to 1. Linguistically, this decision is a: Moderate confidence buy decision.

According to marketbeat.com AAPL was rated by 43 analysts as a ‘Buy’ rating:

Part of a chart from marketbeat.com depicting data (10th July 2020)

Interesting? It would seem that our simple fuzzy logic model did indeed use human reasoning with some technical analysis knowledge to come up with a decision that is similar if not identical to that of the average consensus rating of the 43 analysts.

Conclusion

We saw how from classical logic and set theory, fuzzy logic and sets emerged out of the real world.

Once we got to grips of how the framework worked, we managed to build our own model theoretically then recreate it in Matlab (with laxity).

Upon completing our model and simulating a decision via existing data, we realized that using fuzzy logic and feeding our rule base with some expert knowledge in technical analysis, we were able to come with a very similar rating as Wall Street analysts did.

It is quite evident that fuzzy logic is simply an incredible logical system that can be used to model systems and controllers. Its effectiveness in simulating human reasoning is impressive.

In this article I tried to put what I learnt out of the curiosity of seeing how I could decide whether a stock was fit for choosing. It goes without saying that this would be much more effective if we were to scale it up to sift through a large number of stocks constituting say the S&P500, then ranking them according to our ratings — from 0 to 1. Also creating a system with a large number of technical indicators and a combination of the most useful strategies could indeed prove useful. I hope to further explore and learn more regarding this topic.