You are on page 1of 37

Republic of Iraq

Ministry of Higher Education and


Scientific Research
University of Babylon
College of Information Technology
Software Department

As a graduation project to obtain a bachelor's degree in information technology /


software provider to the Department of software in the Faculty of Information
Technology for the academic year 2022-2023. “

By

Supervised By
Dr. Ameer Al-Haq Adil

July 2023
( )

:
‫األهداء‬
Acknowledgments
‫إقرار المشرف‬

‫أؤيد ان بحث مشروع التخرج الموسوم ‪:‬‬

‫((‬ ‫))‬

‫قد تم تحت اشرافي في ‪ :‬جامعة بابل ‪ /‬كلية تكنولوجيا المعلومات ‪ /‬قسم‬


‫البرمجيات للعام الدراسي ‪ 2022-2021‬ومن قبل الطالب ‪:‬‬

‫)اسم الطالب االول ‪ +‬اسم الطالب الثاني(‬

‫التوقيع ‪:‬‬
‫التوقيع ‪:‬‬
‫اسم التدريسي ‪:‬دكتور امير الحق عادل‬
‫رئيس القسم ‪ :‬د‪ .‬أحمد سليم الصفار‬
‫اللقب العلمي‪ :‬مدرس‬
‫اللقب العلمي‪ :‬استاذ‬
‫‪2023 /‬‬ ‫‪/‬‬ ‫التاريخ ‪:‬‬
‫‪2023 / 7 /‬‬ ‫التاريخ ‪:‬‬
Content
1. Chapter One ............................................................................................... 2

1.1 Introduction ................................................................................................ 2

1.2 Problem Description ................................................................................ 3

2. Chapter Two ............................................................................................... 4

2.1 What is the Haar wavelet?: .................................................................... 4

2.2 Haar functions: ....................................................................................... 5

2.3 Applications of the Haar transform: ...................................................... 6

3 Chapter Three........................................................................................... 7

3.1 block diagram .............................................................................................................. 7

3.2 The procedural steps: ............................................................................ 8

3.3 Divided Speech Signal .......................................................................... 10

3.4 scrambling stage ................................................................................. 11

3.5 Additive White Gaussian Noise (AWGN) Stage ........................................... 13

3.6 Speech Descrambling Stage ....................................................................... 14

3.7 Objective Test ................................................................................................................ :17

4 Chapter Four .......................................................................................... 26

4.1 Conclusion:........................................................................................ 26

5 References .............................................................................................. 27
Abstract

In the face of investment risk, investors generally diversify and form an investment
portfolio consisting of several assets. The problem is the fiery proportion of funds
that must be allocated to each asset in the formation of investment portfolios. This
paper aims to study the optimization of the Markowitz investment portfolio. In this
study, the Markowitz model discussed is that which considers risk tolerance. From
the study, an equation is obtained to determine the proportion (weight) of fund
allocation for each asset in the formation of investment portfolios. So by using these
equations, the determination of investment portfolio weights can be determined by
capital.
General Introduction

1.1 Introduction
Business intelligence (BI), specifically financial analysis, is the focus of this
project, which examines issue is related to portfolio management and optimization.
Financial Portfolio Optimization (PO) is a widely studied problem in mathematics,
statistics, financial and computational literature. It adheres to determining an optimal
combination of weights that are associated with financial assets held in a portfolio
[1]. In practice, portfolio optimization faces challenges by virtue of varying
mathematical formulations, parameters, business constraints and complex financial
instruments. Empirical nature of data is no longer one-sided; thereby reflecting
upside and downside trends with repeated yet unidentifiable cyclic behaviors
potentially caused due to high frequency volatile movements in asset trades.
Portfolio optimization under such circumstances is theoretically and
computationally challenging [2]. A portfolio is defined as a group of investment
assets, which are selected based on specific constraints and economic objectives to
achieve best profits for investors [3]. Portfolio management, on the other hand, refers
to the investment decision process based on tactical investment strategies that are
tailored to match maximization of return for each investment time horizon [4].

Until the 1950s, investing in SM was random process because there was no
sufficient financial data and there was a lack of people's awareness about the concept
of investment management. Investors had usually focused on studying the behavior
of each stock separately, rather than investigating the relationship between the return
and risk [5]. In 1952, however, this situation was fundamentally changed by the
Nobel Prize winner Markowitz who proposed a new portfolio selection technique
which is so-called “the modern portfolio theory” [6].
The Mean-Variance Optimization (MVO) model of Markowitz is one of the
most important theories in finance research [7]. To reduce the variance of the
investment portfolio in this model, the diversification principle is suggested. It
assumes that investing in different types of stocks can reduce the possible risks in
comparison to the investment in one stock only. The final stage of Markowitz model
is the Efficient Frontier (EF). This key notion of the EF stage is selecting the efficient
portfolio in line with the investors' preferences. The EF is represented by the curves
of risks (variance) and return (mean) to determine the efficient portfolio [8].

1.2 Problem Statement


investors in the financial market face multiple investment opportunities, in
light of a large number of stocks and the corresponding multiple entrances to
building investment portfolios. Hence, this makes the task of the investor very
complicated, unless the investor clearly defines his preferences regarding the
expected return and the extent of his avoidance of the potential risk from the
investment, as this makes the task of choosing and building the portfolio clearer.
Despite the excellent results obtained by Markowitz's approach and the simplified
methods followed based on his basic proposals and assumptions, there are some
challenges including that the PO problem is NP-complete.

1.3 The motivations


The key motivations of the project are:

1- The stock market is one of the interesting areas at the moment affecting the
country's economy.
2- Optimizing portfolio in stock market is a very complex task.
3- where and how people can invest their money is a problem that many individuals
face.
Theoretical Background

2.1 Introduction
the difficulty of developing and managing an effective investment portfolio is
one of the most pressing issues facing the financial sector today, especially in light
of the increasing competition and global economic changes at the national and
international levels [2]. Thus, portfolio management and optimization is an
important part of the trading system. This chapter introduces the theoretical concepts
by presenting the concept of the PO model based on Markowitz's theory is discussed,
along with its associated restrictions.

2.2 The Portfolio management problem


This section discusses the portfolio management issue and more precisely Harry
Markowitz's mean–variance model (MVO) [9]. PO was first conceptualized as a
quadratic mathematical programming issue. Since then, this discipline has advanced
rapidly, as other scientists have been worked to enhance and cure this methodology's
flaws [10].

This section provides an overview of the fundamental formulae that comprise the
issue. The most important concepts, like as return and risk, are defined both for a
single investment and for a portfolio of securities in general. Additionally, the basic
notion of diversification is discussed, and the research will explain how diversity
impacts portfolio risk by examining the variables that necessitate the adoption of a
diversified approach. Finally, the research will present the ideas of efficient
portfolios and efficient frontiers.

2.2.1 Portfolio concept


The business field is full of many investment and financial activities, including
those of an investment nature, which contain multiple and diversified methods,
including in particular investment portfolios in the financial markets. The concept
of an investment portfolio is defined as a variety of financial and non-financial assets
held by an investor. It can be defined more precisely as "that a group of assets created
for the purpose of achieving one or more investment objectives" [11].

Although the investment portfolio is a subject to many factors such as


preferences, wealth, and experience, it takes contexts that are accustomed to when
building it, starting from the process of selecting stocks, defining the portfolio,
managing the portfolio and its style, and then evaluating it [12].

Although many elements influence an investment portfolio, such as preferences,


wealth, and experience, it is built in settings that they are familiar with, beginning
with stock selection, creating the portfolio, maintaining and styling the portfolio, and
finally evaluating it [12].

2.2.2 Portfolio theory

The investment portfolio and its technical procedures did not appear overnight
but rather developed over time in tandem with the formation of financial markets
and the development of investing strategies [13]. But Markowitz's suggestions in
1952, which are credited with building the foundations of Modern Portfolio Theory
(MPT) [14], were the most significant turning point. The period before it was known
as the period of Traditional Portfolio Theory dominance; here's more information
about them:

2.2.2.1 Traditional portfolio theory (TPT)

The Traditional Portfolio Theory (TPT) is a descriptive technique that relies on


balancing the portfolio by acquiring a diverse range of equities or bonds for various
industries and sectors. TPT emphasis on adding large and well-known firms' shares
because they perform well and so reduce the risk. Moreover, because their securities
are highly liquid, the transaction costs can be lowered as well. It is worth noting that
this is also due to huge investors buying these shares, which are then sought by small
investors in the same way due to the herd effect. This technique is naive because it
follows the adage principle "don't put all your eggs in one basket". It does not place
as much emphasis on the link between stocks [15].

2.2.2.2 Modern portfolio theory

The Modern Portfolio Theory (MPT) is a quantitative and scholarly approach


developed by Markowitz [14], who is considered the originator and forefather of this
theory, even though Roy is the owner of the entry (Safety first). Roy was provide
recommendations that were comparable to his first work, did not continue to submit
his study, unlike Markowitz, who went on to earn the Nobel Prize in Economics in
1991 [14].

Markowitz invented the MVO model [16]. This theory clarified the way of
measuring risk through the use of variance returns. It is a moral and appropriate
method and this, in turn, leads to its use not only to demonstrate portfolio
diversification efficiency, but also to diversify the portfolio efficiently in the first
place. According to Markowitz's suggestions, the strategy return-risk or mean-
variance is based on "appropriate diversification", which is the practice of investing
in stocks or assets with low joint variance (correlation) [16].

The model is predicated on a set of assumptions that account for investor


behavior:

a) Investors evaluate each investment option based on the probability distribution


of its returns over a certain holding time.
b) Over the time, investors maximize their utility. Their utility curves show a
declining marginal utility of wealth.

c) Investors are calculating portfolio risk based on changes in projected returns.

d) The investor makes decisions based only on the expected return and the expected
risk (standard deviation) of returns.

e) At a given level of risk, the investor chooses the greatest return over the lowest,
and at a given level of return. The investor prefers the lowest risk over the highest.

2.2.3 Diversification and portfolio principle

Diversification is the principle of work. The key concept of this principle is


that the investor eliminates or at least limits excessive levels of risk in the assets
which represents the shared concern of all types of investors. Moreover, although
the risk is distinct from uncertainty, the financial literature uses the terms
interchangeably. Thus, the risk was defined for the majority of investors as the
condition of uncertainty about future returns, or the chance of earning returns that
are less than anticipated and undesired [17].

There are two parts to investment risks. The first one is the systemic risk, it is also
known as non-diversifying or market risk. This type represents the amount of risk
that all assets in the market face at the same time, but at different rates in which it
cannot be eliminated through diversification such as global financial crises and wars
[18]. The second one is an unsystematic risk which it is also called private, unique,
or diversified risk. It affects the assets of certain organizations in the market as a
result of emergency conditions such as worker strikes, technical failures, and other
uncontrollable events. This type of risk can be avoided by diversifying across a
group of assets within the portfolio. In such a case, the total risk will be the sum of
the two risks as shown in Figure 2.1 [18].
Figure 2.1. Total portfolio risk

2.2.4 Risk and return in MPT

The process of assessing the portfolio's return and risk begins with an
examination of the return and risk of individual securities and concludes with the
portfolio as a whole. As a result, after fulfilling the criteria for the portfolio model's
inputs, the research will refer to how calculating the return and risk of individual
assets before moving on to the computation of the return and risk of the portfolio
[19].

2.2.5 Asset return

The return is defined as the amount received by the investor during the period in
which the asset is held, and it is the summation of the capital and periodic returns.
As a result, it is computed based on a single holding period, and this form of return
is known as the realized return [20], and it is calculated using Eq. 2.1:
𝑷𝟏 −𝑷𝟎 +𝑫𝟏
𝑹𝒊 = ( ) (2.1)
𝑷𝟎

Where:
𝑃0 : The share's previous price when purchased

𝑃1 : The current price or liquidation price.

𝐷1 : The dividend profit at the end of the term.

The return utilized in portfolio accounts is the expected return and indicates the
expected return to be achieved from the asset based on current knowledge of the
asset's price behavior. The expected return was computed in two ways: the first
technique is done by analyzing historical data for the asset's returns, in which the
arithmetic mean of the asset's previous returns is calculated, Eq. 2.2, which reflects
the value of the expected return [21]:
∑𝒏
𝒊=𝟏 𝑹𝒊
𝑬(𝑹) = ( ) (2.2)
𝒏

Where 𝑅𝑖 is the return on the retention period, and 𝑛 is the number of holding
periods used to calculate the expected return.

The second technique [22] use discrete probability distributions based on


expectations or predictive information about the occurrence of the return, with the
expected return being the weighted rate of the likelihood of obtaining returns, as
indicated in Eq. 2.3:

𝑬(𝑹) = ∑𝒏𝒊=𝟏 𝑹𝒊 ∗ 𝑷𝒊 (2.3)

Where 𝑅𝑖 is the possible return, 𝑃𝑖 represents the probability that the return will
occur i, and 𝑛 is the expected number of outputs.

2.2.6 Asset risk

As previously stated, the risk is the degree of uncertainty about the expected
output of the investment, and so its metrics vary, with some measuring the
magnitude of the return and others measuring the possibility of dropping below the
projected value. Moreover, the standard deviation measure is the most prominent
and extensively used metric in the portfolio and the rest of the asset pricing models
[23]. It is one of the measures of statistical dispersion, since it reflects the level of
dispersion of results from the anticipated value.

The standard deviation of expected returns is obtained in two methods, as in


computing the return. The first technique for historical data, and it is calculated using
Eq. 2.4 [24]:

𝟐 ∑𝒏 ̅ 𝟐
𝒊=𝟏(𝑹𝒊 −𝑹)
𝝈 = ( ) (2.4)
𝒏−𝟏

Where: 𝜎 2 is asset variance (square of the standard deviation of returns), 𝑅𝑖 is


historical returns of the asset, 𝑅̅ is Historical average returns, and 𝑛 is the number of
historical outputs measured by year, month, or day.

In the second technique, the inputs are based on the probability distribution of
returns skew, as shown in Eq. 2.5 [24]:

̅ )𝟐 . 𝑷𝒊
𝝈𝟐 = ∑𝒏𝒊=𝟏(𝑹𝒊 − 𝑹 (2.5)

Where 𝜎 2 is asset variance (square of the standard deviation of returns), 𝑅𝑖 is


historical returns of the asset, 𝑅̅ is Historical average returns, and 𝑃𝑖 is the probability
of each output outcome.

2.2.7 Portfolio risk and return

The portfolio's return determined using the weighted rate of expected returns for
its various assets or investments, as shown in Eq. 2.6 [25]:

𝑬(𝑹𝑷 ) = ∑𝒏𝒊=𝟏(𝑬(𝑹𝒊 ). 𝑾𝒊 ) (2.6)

Where: 𝐸(𝑅𝑖 ) is the return asset i in the portfolio, and 𝑊𝑖 is the weight of asset i in
the portfolio.
The research should mention that while constructing the portfolio, each asset is
assigned a certain weight, and the weight symbolizes the value of the invested
portion. For example, concerning the entire value of the portfolio. As demonstrated
in Eq. 2.7, the total weights of the assets = 1. The weight of each asset is also
calculated by the ratio of the value of the asset to the total value of the portfolio [26].

∑𝒏𝒊=𝟏 𝑾𝒊 = 𝟏 (2.7)

In contrast to the method of calculating portfolio return, the total risk of the
portfolio varies greatly because it was including not only the weighted rate of risk
(variance) of the assets, but also the common variance (Covariance) between assets,
which plays a significant role in reducing risk and making it much less than the risk
of the same assets if combined directly together, and this method of calculating risk
is the most important product of MPT. The portfolio risk is computed by determining
the variance of a portfolio of n assets, as shown in Eq. 2.8 [26]:

𝝈𝟐𝑷 = ∑𝒏𝒊=𝟏 𝑾𝟐𝒊 𝝈𝟐𝒊 + ∑𝒏𝒊=𝟏 ∑𝒏𝒋=𝟏 𝑾𝒊 𝑾𝒋 𝑪𝒐𝒗𝒊𝒋 (2.8)

Where: 𝜎𝑃2 is the portfolio variance, 𝑊𝑖 is the weight of asset i in the portfolio.
𝜎𝑖2 variance of asset i, and 𝐶𝑜𝑣𝑖𝑗 is the covariance between returns.

The significance of Markowitz's propositions in calculating risk based on


variance is that he clarified the role of the most important component, which is (the
covariance), which indicates the degree to which two variables move together
relative to the single values of their averages over time and is calculated in two ways:
beginning through the use of historical data, and then it is extracted by Eq. 2.9 [26]:
̅̅̅𝒊 )(𝑹𝒋 −𝑹
∑(𝑹𝒊 −𝑹 ̅̅̅̅
𝒋)
𝑪𝒐𝒗𝒊𝒋 = ( ) (2.9)
𝒏−𝟏

The covariance is also calculated using Eq. 2.10:

𝑪𝒐𝒗𝒊𝒋 = 𝝈𝒊 𝝈𝒋 𝝆𝒊𝒋 (2.10)


Where: 𝜎𝑖 𝜎𝑗 is The standard deviation of asset i and j respectively, and 𝜌𝑖𝑗 is the
correlation coefficient of the assets i and j.

As a result, the research concludes that the correlation (the correlation


coefficient) is the most essential component in the covariance combination since, it
measures the degree of a link between two things. The correlation coefficient plays
a role in the variance law of the portfolio by lowering the covariance limit (it affects
the effectiveness of diversification in the portfolio and the correlation ranges
between (+1 and -1)). When its value is (+1), indicating perfect correlation, both
assets or assets are travelling in the same direction in terms of return movement,
rendering diversification ineffective and wasteful, because it will not help risk
reduction. When it is (-1), the returns are inversely proportional, and diversification
appears in its most efficient form. In principle, the portfolio's risk is (zero), as
illustrated in Figure 2.2 [27].

Figure 2.2. The correlation coefficient is between +1 and -1

2.2.8 Efficient frontier and optimal portfolio

In final stages, the efficient portfolio theory is embodied by selecting the efficient
portfolio in light of the investor's preferences represented by the curves of
indifference and benefits functions, and determining the efficient portfolio, which
can be represented in the space (return - risk) by drawing the horizontal axis
representing risk and the vertical axis representing expected return. In this case, the
research used several assets, and it is clear from Figure 2.3 that the curve (BYOZC)
represents the EF, which contains efficient portfolios and various asset
combinations, all of which provide the highest possible return at a certain level of
return or the lowest risk at a certain level of risk [28].

Figure 2.3. Efficient frontier and optimal portfolio

2.2.9 Limitations of the MVO model

Portfolio theory faces criticism as well, which results when applying the theory
in reality to reach the optimal portfolio, as follows:

1- The MVO approach implies that investors concentrate on a single time horizon
and will never modify their asset allocation once it is decided.
2- It is difficult to determine the return of the market portfolio or even to represent
it accurately.
3- Even if the efficient portfolio is determined, the transaction cost will be very high
and small investors cannot afford it.
4- The market's structure and forces change dynamically, causing the risk-free
return to fluctuate, followed by the market return, and finally the efficient limit,
making the efficient portfolio unstable and thus difficult to obtain. This is why
some refer to it as the (Holy Grail), implying that everyone wants it but it is
difficult to obtain.
5- The statistical methodologies employed to calculate the return and risk have a
statistical error rate that renders the findings completely inaccurate.
6- With large portfolios, the portfolio input under Markowitz's propositions
becomes extremely complicated, as owning a portfolio of N assets necessarily
requires N returns, N standard deviations, and (N(N-1)/2) common variances,
resulting in data entry confusion and the occurrence of clearly incorrect results.
2.3 The New York Stock Exchange (NYSE) dataset
The NYSE is a stock exchange in New York city that is the world's biggest
by market capitalization of its listed stocks. Previously a private company, the NYSE
became a public company on March 8, 2006, when it acquired electronic trading
exchange Archipelago. In 2007, a merger with Euronext—biggest Europe's stock
exchange—created NYSE Euronext, which was eventually bought by
Intercontinental Exchange, Inc. (ICE), the New York Stock Exchange's current
parent company. There are 82 % of S&P 500 companies listed on the New York
Stock Exchange (NYSE), as well as 70 of the top companies on the planet. There
are over nine million business equities and investments exchanged every day on
these publicly listed corporations [29] [30].

2.4 Portfolio metric evaluation


The Sharpe ratio, a risk-return evaluation metric, was utilized in this work to
provide a unified measure based on a risk rate. In certain circumstances, it is also
known as the reward-volatility measure since its composition is based on calculating
the excess return compared to the total risk unit. Eq. 2.11 [10] is used to determine
the Sharpe ratio:
𝑹𝒑−𝑹𝒇
𝑺𝒉𝒂𝒓𝒑𝒆 𝑹𝒂𝒕𝒊𝒐 = (2.11)
𝝈𝒑

where 𝑅𝑝 is the return on the portfolio under consideration and 𝑅𝑓 is the risk-
free rate of return. The standard deviation of the portfolio to be assessed is denoted
by 𝜎𝑝.
The Proposed System

3.1 Introduction
In this thesis, the research tries to build an investment system that can help investors
and fund managers alike to find the best solutions in times of uncertainty. Because
the research can contribute to the expansion of local capital markets through
increased investment in portfolios, the research can help improve their efficiency
and liquidity.

In the field of financial economics, to address the problem of portfolio


optimization, a variety of statistical and mathematical approaches have been used.
Although there is still a lack of effective trading strategies that correctly integrate
the investor's risk appetite with market behavior, there is still a dearth of powerful
trading. Figure 3.1 shows an overview of the possible functions that the proposed
model fulfills,

Figure 3.1. The architecture of proposed system


3.2 Programming Portfolio Optimization

In this section, we will explain the programming steps for building and optimizing a
portfolio using the Python language based on a special online editor for Google called
(Colaboratory). Colaboratory, or “Colab” for short, is a product from Google
Research. Colab allows anybody to write and execute arbitrary python code through
the browser, and is especially well suited to machine learning, data analysis and
education. More technically, Colab is a hosted Jupyter notebook service that requires
no setup to use, while providing access free of charge to computing resources
including GPUs.

The code steps are as follows:


# Description: This program attempts to optimize a user’s portfolio
using the Efficient Frontier & Python.

• Import the libraries.


# Import the python libraries
from pandas_datareader import data as web
import pandas as pd
import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')

• Create The Fictional Portfolio

Get the stock symbols / tickers for the fictional portfolio. I am going to use the
twenty-five most popular and best performing American technology companies.
assets = ["AAPL","AIG","AMZN","BA","CAT","COF","EBAY","F","FDX","GE","G
M","GOOG","HD","IBM","JNJ","JPM","KO","MSFT","NKE","ORCL","PEP","T","WM
T","XOM","XRX"]
• Next I will assign equivalent weights to each stock within the portfolio,
meaning 1/25% of this portfolio will have shares.
# Assign weights to the stocks. Weights must = 1 so 0.2 for each
weights = np.array([0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0
.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.04,0.
04])

• Time to create the data frame that will hold the stocks Adjusted Close price.
#Create a dataframe to store the adjusted close price of the stocks

df = pd.DataFrame()

pip install xlrd

from google.colab import files

uploaded = files.upload()

for fn in uploaded.keys():

print('User uploaded file "{name}" with length {length} bytes'.format


(name=fn, length=len(uploaded[fn])))

df=pd.read_excel('NYSE.xlsx')

df=df.set_index(pd.DatetimeIndex(df['date'].values))

df.drop(columns=['date'],axis=1,inplace=True)

• Show the data frame and the adjusted close price of each stock.
# Create the title 'Portfolio Adj Close Price History

title = 'Portfolio Adj. Close Price History '

#Get the stocks

my_stocks = df

#Create and plot the graph

plt.figure(figsize=(20.2,8.5)) #width = 12.2in, height = 4.5

# Loop through each stock and plot the Adj Close for each day

for c in my_stocks.columns.values:
plt.plot( my_stocks[c], label=c)#plt.plot( X-Axis , Y-
Axis, line_width, alpha_for_blending, label)

plt.title(title)

plt.xlabel('Date',fontsize=18)

plt.ylabel('Adj. Price USD ($)',fontsize=18)

plt.legend(my_stocks.columns.values, loc='upper left')

plt.show()

• Financial Calculations

I’m done creating the fictional portfolio. Now I want to show the daily simple returns
which is a calculation of the (new_price + -old_price)/ old_price or (new_price /
old_price)-1.
#Show the daily simple returns, NOTE: Formula = new_price/old_price
- 1
returns = df.pct_change()
returns

• Create and show the annualized co-variance matrix. The co-variance matrix is
a mathematical concept which is commonly used in statistics when comparing
data samples from different populations and is used to determine how much two
random variables vary or move together (so it’s the directional relationship
between two asset prices).
• The diagonal of the matrix are the variances and the other entries are the co-
variances. Variance is a measure of how much a set of observations differ from
each other. If you take the square root of variance you get the volatility also
known as the standard deviation.
• To show the annualized co-variance matrix we must multiply the co-variance
matrix by the number of trading days for the current year. In this case the number
of trading days will be 252 for this year.
cov_matrix_annual = returns.cov() * 252
cov_matrix_annual

• Now calculate and show the portfolio variance using the formula :
Expected portfolio variance= WT * (Covariance Matrix) * W
port_variance = np.dot(weights.T, np.dot(cov_matrix_annual,
weights))
port_variance

0.021788282278697002

• Now calculate and show the portfolio volatility using the formula :
Expected portfolio volatility= SQRT (WT * (Covariance Matrix) * W)
• Don’t forget the volatility (standard deviation) is just the square root of the
variance.
port_volatility = np.sqrt(port_variance)
port_volatility

0.14760854405723606

• Last but least not I’m going to show and calculate the portfolio annual simple
return.
portfolioSimpleAnnualReturn = np.sum(returns.mean()*weights) *
252
portfolioSimpleAnnualReturn

0.11036027922319926

• Show the expected annual return, volatility or risk, and variance.


percent_var = str(round(port_variance, 2) * 100) + '%'
percent_vols = str(round(port_volatility, 2) * 100) + '%'
percent_ret = str(round(portfolioSimpleAnnualReturn,
2)*100)+'%'print("Expected annual return : "+ percent_ret)
print('Annual volatility/standard deviation/risk :
'+percent_vols)

print('Annual variance : '+percent_var)


Expected annual return : 11.0%
Annual volatility/standard deviation/risk : 15.0%
Annual variance : 2.0%
Sharpe Ratio : 7.5%

• Optimize The Portfolio

It’s now time to optimize this portfolio, meaning I want to optimize for the
maximum return with the least amount of risk . Luckily their is a very nice
package that can help with this created by Robert Ansrew Martin.
• I will install the package that he created called pyportfolioopt.
pip install PyPortfolioOpt

• Next, I will import the necessary libraries.


from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt import risk_models
from pypfopt import expected_returns

• Calculate the expected returns and the annualised sample covariance matrix
of daily asset returns.
mu = expected_returns.mean_historical_return(df)
#returns.mean() * 252
S = risk_models.sample_cov(df)
#Get the sample covariance matrix

• Optimize for maximal Sharpe ratio.


ef = EfficientFrontier(mu, S)weights = ef.max_sharpe()
#Maximize the Sharpe ratio, and get the raw weights
cleaned_weights = ef.clean_weights()
print(cleaned_weights)
#Note the weights may have some rounding error, meaning they may
not add up exactly to 1 but should be close
ef.portfolio_performance(verbose=True)
OrderedDict([('AAPL', 0.0), ('AIG', 0.0), ('AMZN', 0.20301),
('BA', 0.0), ('CAT', 0.13476), ('COF', 0.0), ('EBAY', 0.0), ('F',
0.0), ('FDX', 0.0), ('GE', 0.0), ('GM', 0.0), ('GOOG', 0.0), ('HD',
0.66223), ('IBM', 0.0), ('JNJ', 0.0), ('JPM', 0.0), ('KO', 0.0),
('MSFT', 0.0), ('NKE', 0.0), ('ORCL', 0.0), ('PEP', 0.0), ('T',
0.0), ('WMT', 0.0), ('XOM', 0.0), ('XRX', 0.0)])
Expected annual return: 24.0%
Annual volatility: 17.8%
Sharpe Ratio: 1.24
(0.240310586887385, 0.1780703012862249, 1.2372112884408744)
The Experimental Results

4.1 Introduction
The effectiveness of the proposed system illustrated in the previous chapter
has been tested with different parameters values, and the results will be analyzed in
this chapter. A real global dataset has been applied as an employment case study to
determine the behavior of this model. The experimental results stages of the
proposed system are described and shown in this chapter.
The proposed model has characterized as follows:
1- It deals with real, numeric, complex, and huge dataset of SM.
2- It builds rules for making decision.
3- It constructs active portfolio.
4- It deals with multi-objective function (Trade-off between conflicting goals
[max Return and min Risk]).
4.2 System Requirement
Hardware: Processor Intel i7, Ram 16 GB, Storage 1 TB SSD, Freq. 2.9 GHz.
Operating System Windows 10 64 bit.
Programming Language JAVA
IDE Programming is done in the Eclipse IDE environment.
4.3 Description of Stock Market Dataset
4.3.1 The New York Stock Exchange (NYSE) dataset
The New York Stock Exchange (NYSE) dataset is used in this research. An updated
version of the dataset can be found at (https://www.kaggle.com/dgawlik/nyse ).
More than 450 stocks from 1/1/2010 to 31/12/2016 are included in the dataset.
Twenty-five well-known stocks were chosen in this study for comparison and
evaluation of the prediction. The rationale behind the selection of these twenty-five
stocks is that the performance of our proposed model was compared with earlier
literature such as [31] [32], which used the same stocks as well. However, the
proposed model can be applied to any SM. The Twenty-five selected stocks are
shown in Table 4.1. The dataset includes five technical features namely, date (the
trading day), open (stock’s opening price for a specific day), low (the lowest value
of the stock price during the day), high (the highest value of the stock price during
the day), close (a closing stock's price on any given day), and finally, volume
(volume of stock trades (buy/sell)). Figure 4.1 shows the historical closing values of
the twenty-five stocks during the study period.
Table 4.1 The New York Stock Exchange (NYSE) dataset.
No. Company name Symbol No. Company name Symbol
1 American International Group AIG 14 IBM IBM
2 Apple AAPL 15 Johnson & Johnson JNJ
3 Amazon AMZN 16 JPMorgan Chase & Co. JPM
4 Boeing BA 17 Coca-Cola KO
5 Caterpillar CAT 18 Microsoft MSFT
6 Capital One COF 19 Nike NKE
7 EBAY EBAY 20 Oracle ORCL
8 Ford F 21 PepsiCo PEP
9 FedEx FDX 22 AT&T T
10 General Electric GE 23 Walmart WMT
11 General Motors GM 24 ExxonMobil XOM
12 Google Alphabet Inc. GOOG 25 Xerox XRX
13 The Home Depot HD
Figure 4.1. The NYSE historical close prices
4.4 Evaluating the optimization of portfolio
4.4.1 The results of the NYSE
To obtain an optimal allocation of assets, two models were implemented
namely, MVO and EQ models. This is to highlight the strength of our proposed
model as shown in Table 4.2. To obtain the Sharpe ratio, the weights of the two
models were used. Based on the results presented in Table 4.2, it is clear that our
proposed model outperformed the EW model. The proposed model produced the
largest Sharpe ratio, maximized profits, and reduced risks in the portfolio. In another
research [32], the fuzzy logic model was used to build the portfolio in which it was
close to but not better than MVO, whereas our results were better than MVO. Figure
4.2 shows the weights of stocks within the portfolio for the two models.
Table 4.2. Weights for stocks after applying two models (EW AND MVO)

Expected
Stocks MVO EW Volatility
Return
AAPL 0 0.04 0.000399 0.02624
AIG 0 0.04 0.000677 0.021613
AMZN 0.20301 0.04 0.001186 0.020395
BA 0 0.04 0.000694 0.015165
CAT 0.13476 0.04 0.000787 0.02357
COF 0 0.04 0.000621 0.018091
EBAY 0 0.04 0.000452 0.023107
F 0 0.04 0.000261 0.018235
FDX 0 0.04 0.000576 0.015513
GE 0 0.04 0.0005 0.013725
GM 0 0.04 0.000116 0.017901
GOOG 0 0.04 0.000354 0.019843
HD 0.66223 0.04 0.000959 0.012856
IBM 0 0.04 0.000201 0.012075
JNJ 0 0.04 0.000367 0.008889
JPM 0 0.04 0.000544 0.017126
KO 0 0.04 -2.52E-05 0.015289
MSFT 0 0.04 0.000502 0.014607
NKE 0 0.04 0.000199 0.022715
ORCL 0 0.04 0.000364 0.015195
PEP 0 0.04 0.000344 0.008884
T 0 0.04 0.000274 0.009831
WMT 0 0.04 0.000191 0.01031
XOM 0 0.04 0.000223 0.011993
XRX 0 0.04 0.000182 0.01864
Sharpe Ratio 1.24 0.747867
Figure 4.2. The weights of the assets in a portfolio
To obtain an optimal allocation of assets, two models were implemented namely,
MVO and EQ models on the NYSE dataset. Based on the results presented in Table
4.12, it is clear that our proposed MVO model outperformed the EW model. The
proposed model produced the largest Sharpe ratio, maximized profits, and reduced
risks in the portfolio. In another research [32], the fuzzy logic model was used to
build the portfolio in which it was close to but not better than MVO, whereas our
results were better than MVO. As it is clear in Table 4.2, the MVO model chose
three stocks (HD, AMZN, and CAT) out of Twenty-five stocks with weights
(0.66223, 0.20301, and 0.13476) respectively, to form the portfolio with a Sharpe
ratio of (1.24).

References
[1] U. Zaman, R. D. Nadeem, and S. Nawaz, “Cross-country evidence on project
portfolio success in the Asia-Pacific region: Role of CEO transformational
leadership, portfolio governance and strategic innovation orientation,” Cogent
Bus. Manag., vol. 7, no. 1, p. 1727681, 2020.

[2] E. Oztemel and S. Gursev, “Literature review of Industry 4.0 and related
technologies,” J. Intell. Manuf., vol. 31, no. 1, pp. 127–182, 2020.

[3] S. Almahdi and S. Y. Yang, “An adaptive portfolio trading system: A risk-
return portfolio optimization using recurrent reinforcement learning with
expected maximum drawdown,” Expert Syst. Appl., vol. 87, pp. 267–279,
2017.

[4] T. Zellweger, “Time horizon, costs of equity capital, and generic investment
strategies of firms,” Fam. Bus. Rev., vol. 20, no. 1, pp. 1–15, 2007.

[5] N. M. Waweru, E. Munyoki, and E. Uliana, “The effects of behavioural factors


in investment decision-making: a survey of institutional investors operating at
the Nairobi Stock Exchange,” Int. J. Bus. Emerg. Mark., vol. 1, no. 1, pp. 24–
41, 2008.

[6] R. O. Michaud and R. O. Michaud, Efficient asset management: a practical


guide to stock portfolio optimization and asset allocation. Oxford University
Press, 2008.

[7] H. Levy and M. Levy, “The benefits of differential variance-based constraints


in portfolio optimization,” Eur. J. Oper. Res., vol. 234, no. 2, pp. 372–381,
2014.

[8] D. H. Bailey and M. Lopez de Prado, “The Sharpe ratio efficient frontier,” J.
Risk, vol. 15, no. 2, p. 13, 2012.

[9] H. M. Markowitz, “Portfolio Selection/Harry Markowitz,” J. Finance, vol. 7,


1952.

[10] W. F. Sharpe, “The sharpe ratio,” J. Portf. Manag., vol. 21, no. 1, pp. 49–58,
1994.

[11] E. Norton and B. H. Tenenbaum, “Specialization versus diversification as a


venture capital investment strategy,” J. Bus. Ventur., vol. 8, no. 5, pp. 431–
442, 1993.

[12] R. C. Grinold and R. N. Kahn, “Active portfolio management,” 2000.

[13] H. M. Markowitz, “Portfolio theory: as I still see it,” Annu. Rev. Financ. Econ.,
vol. 2, no. 1, pp. 1–23, 2010.

[14] E. J. Elton and M. J. Gruber, “Modern portfolio theory, 1950 to date,” J. Bank.
Financ., vol. 21, no. 11–12, pp. 1743–1759, 1997.

[15] E. Platanakis and A. Urquhart, “Should investors include bitcoin in their


portfolios? A portfolio theory approach,” Br. Account. Rev., vol. 52, no. 4, p.
100837, 2020.

[16] J. Blanchet, L. Chen, and X. Y. Zhou, “Distributionally robust mean-variance


portfolio selection with Wasserstein distances,” Manage. Sci., 2021.

[17] M. Abreu and V. Mendes, “Financial literacy and portfolio diversification,”


Quant. Financ., vol. 10, no. 5, pp. 515–528, 2010.

[18] S. Démurger, M. Fournier, and W. Yang, “Rural households’ decisions


towards income diversification: Evidence from a township in northern China,”
China Econ. Rev., vol. 21, pp. S32–S44, 2010.

[19] I. Omisore, M. Yusuf, and N. Christopher, “The modern portfolio theory as an


investment decision tool,” J. Account. Tax., vol. 4, no. 2, pp. 19–28, 2011.

[20] F. X. Diebold and K. Yilmaz, “Measuring financial asset return and volatility
spillovers, with application to global equity markets,” Econ. J., vol. 119, no.
534, pp. 158–171, 2009.

[21] G. T. Friedlob and F. J. Plewa Jr, Understanding return on investment. John


Wiley & Sons, 1996.

[22] W. Van Harlow, “Asset allocation in a downside-risk framework,” Financ.


Anal. J., vol. 47, no. 5, pp. 28–40, 1991.

[23] M. Chesney, J. Stromberg, A. F. Wagner, and V. Wolff, “Managerial


incentives to take asset risk,” J. Corp. Financ., vol. 65, p. 101758, 2020.

[24] M. Ogneva, “Accrual quality, realized returns, and expected returns: The
importance of controlling for cash flow shocks,” Account. Rev., vol. 87, no. 4,
pp. 1415–1444, 2012.

[25] E. F. Fama and K. R. French, Multifactor explanations of asset pricing


anomalies. University of Chicago Press, 2021.

[26] M. Norton, V. Khokhlov, and S. Uryasev, “Calculating CVaR and bPOE for
common probability distributions with application to portfolio optimization
and density estimation,” Ann. Oper. Res., pp. 1–35, 2019.
[27] A. Robertson, “The sampling variance of the genetic correlation coefficient,”
Biometrics, vol. 15, no. 3, pp. 469–485, 1959.

[28] X. Y. Zhou and D. Li, “Continuous-time mean-variance portfolio selection: A


stochastic LQ framework,” Appl. Math. Optim., vol. 42, no. 1, pp. 19–33, 2000.

[29] R. A. Wood, T. H. McInish, and J. K. Ord, “An investigation of transactions


data for NYSE stocks,” J. Finance, vol. 40, no. 3, pp. 723–739, 1985.

[30] D. K. Pandey and V. Kumari, “Effects of merger and acquisition


announcements on stock returns: an empirical study of banks listed on NSE &
NYSE,” Rev. Financ. Bank., vol. 12, no. 1, 2020.

[31] Yuan Song, “Stock Trend Prediction:Based on Machine Learning Methods,”


University of California, os Angeles, 2018.

[32] H. NekoeiQachkanloo, B. Ghojogh, A. S. Pasand, and M. Crowley, “Artificial


counselor system for stock investment,” in Proceedings of the AAAI
Conference on Artificial Intelligence, 2019, vol. 33, no. 01, pp. 9558–9564.

You might also like