+ All Categories
Home > Documents > Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization...

Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization...

Date post: 15-Aug-2020
Category:
Upload: others
View: 13 times
Download: 0 times
Share this document with a friend
76
Kecerdasan Bisnis Terapan Prescriptive Analytics Optimization and Simulation Husni Lab. Riset JTIF UTM Sumber awal: http://mail.tku.edu.tw/myday/teaching/1071/BI/1071BI08_Business_Intelligence.pptx
Transcript
Page 1: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Kecerdasan Bisnis Terapan

Prescriptive Analytics

Optimization and Simulation

HusniLab Riset JTIF UTM

Sumber awal httpmailtkuedutwmydayteaching1071BI1071BI08_Business_Intelligencepptx

Business Intelligence (BI)

2

Introduction to BI and Data Science

Descriptive Analytics

Predictive Analytics

Prescriptive Analytics

1

3

5

4

Big Data Analytics

2

6 Future Trends

Prescriptive Analytics Optimization and

Simulation

3

Outline

4

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

5

Business Analytics and Prescriptive Analytics

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Model Examples

bull Modeling is a key element for prescriptive analytics

bull Employ a mathematical model to be able to recommend a decision for any realistic problem

bull Building a probability-based response maximization model with the budget as a constraint would give us the information we are seeking

6Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Identification of the Problem and Environmental Analysis

bull Environmental scanning and analysis

ndash Monitoring scanning and interpretation of collected information

bull Business intelligencebusiness analytics (BIBA) tools can help identify problems by scanning for them

bull Variable Identification

bull Forecasting (Predictive Analytics)

7Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 2: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Business Intelligence (BI)

2

Introduction to BI and Data Science

Descriptive Analytics

Predictive Analytics

Prescriptive Analytics

1

3

5

4

Big Data Analytics

2

6 Future Trends

Prescriptive Analytics Optimization and

Simulation

3

Outline

4

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

5

Business Analytics and Prescriptive Analytics

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Model Examples

bull Modeling is a key element for prescriptive analytics

bull Employ a mathematical model to be able to recommend a decision for any realistic problem

bull Building a probability-based response maximization model with the budget as a constraint would give us the information we are seeking

6Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Identification of the Problem and Environmental Analysis

bull Environmental scanning and analysis

ndash Monitoring scanning and interpretation of collected information

bull Business intelligencebusiness analytics (BIBA) tools can help identify problems by scanning for them

bull Variable Identification

bull Forecasting (Predictive Analytics)

7Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 3: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Prescriptive Analytics Optimization and

Simulation

3

Outline

4

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

5

Business Analytics and Prescriptive Analytics

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Model Examples

bull Modeling is a key element for prescriptive analytics

bull Employ a mathematical model to be able to recommend a decision for any realistic problem

bull Building a probability-based response maximization model with the budget as a constraint would give us the information we are seeking

6Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Identification of the Problem and Environmental Analysis

bull Environmental scanning and analysis

ndash Monitoring scanning and interpretation of collected information

bull Business intelligencebusiness analytics (BIBA) tools can help identify problems by scanning for them

bull Variable Identification

bull Forecasting (Predictive Analytics)

7Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 4: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Outline

4

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

5

Business Analytics and Prescriptive Analytics

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Model Examples

bull Modeling is a key element for prescriptive analytics

bull Employ a mathematical model to be able to recommend a decision for any realistic problem

bull Building a probability-based response maximization model with the budget as a constraint would give us the information we are seeking

6Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Identification of the Problem and Environmental Analysis

bull Environmental scanning and analysis

ndash Monitoring scanning and interpretation of collected information

bull Business intelligencebusiness analytics (BIBA) tools can help identify problems by scanning for them

bull Variable Identification

bull Forecasting (Predictive Analytics)

7Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 5: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

5

Business Analytics and Prescriptive Analytics

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Model Examples

bull Modeling is a key element for prescriptive analytics

bull Employ a mathematical model to be able to recommend a decision for any realistic problem

bull Building a probability-based response maximization model with the budget as a constraint would give us the information we are seeking

6Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Identification of the Problem and Environmental Analysis

bull Environmental scanning and analysis

ndash Monitoring scanning and interpretation of collected information

bull Business intelligencebusiness analytics (BIBA) tools can help identify problems by scanning for them

bull Variable Identification

bull Forecasting (Predictive Analytics)

7Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 6: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Prescriptive Analytics Model Examples

bull Modeling is a key element for prescriptive analytics

bull Employ a mathematical model to be able to recommend a decision for any realistic problem

bull Building a probability-based response maximization model with the budget as a constraint would give us the information we are seeking

6Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Identification of the Problem and Environmental Analysis

bull Environmental scanning and analysis

ndash Monitoring scanning and interpretation of collected information

bull Business intelligencebusiness analytics (BIBA) tools can help identify problems by scanning for them

bull Variable Identification

bull Forecasting (Predictive Analytics)

7Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 7: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Identification of the Problem and Environmental Analysis

bull Environmental scanning and analysis

ndash Monitoring scanning and interpretation of collected information

bull Business intelligencebusiness analytics (BIBA) tools can help identify problems by scanning for them

bull Variable Identification

bull Forecasting (Predictive Analytics)

7Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 8: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Categories of Models

8Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 9: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Structure of Mathematical Models for Decision Support

9

bull The Components of Decision Support Mathematical Models

bull The Structure of Mathematical Models

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 10: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

The General Structure of a Quantitative Model

10Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 11: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Examples of Components of Models

11Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 12: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

The Structure of Mathematical Models

12

A very simple financial model is

P = R - C

where P = profit R = revenue and C = cost

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 13: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

The Structure of Mathematical Models

13

Financial model (present-value cash flow model)

P = F (1 + i)n

where P = present value F = a future single

payment in dollars i = interest rate (percentage)

and n = number of years

P = 100000 (1 + 01)5 = 62092Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 14: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Certainty Uncertainty and Risk

14

bull Decision Making under Certainty

bull Decision Making under Uncertainty

bull Decision Making under Risk (Risk Analysis)

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 15: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

The Zones of Decision Making

15Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 16: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Mathematical Programming Optimization

16

bull Mathematical programming

ndash a family of tools designed to help solve managerial problems in which the decision maker must allocate scarce resources among competing activities to optimize a measurable goal

bull Linear programming (LP) is the best-known technique in a family of optimization tools called mathematical programming

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 17: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Mathematical Model of a Product-Mix Example

17Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 18: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Prescriptive Analytics Operations researchManagement Science

18

bull Multiple Goals

bull Sensitivity Analysis

bull What-If Analysis

bull Goal Seeking

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 19: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Decision Analysis with Decision Tables and Decision Trees

19

bull Decision tables

ndash conveniently organize information and knowledge in a systematic tabular manner to prepare it for analysis

bull Decision tree

ndash shows the relationships of the problem graphically and can handle complex situations in a compact form

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 20: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Investment Problem Decision Table Model

20Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 21: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Multiple Goals

21Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 22: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

The Process of Simulation

22Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 23: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Discrete versus Continuous Probability Distributions

23Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 24: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Simulation Types

24

bull Monte Carlo Simulation

bull Discrete Event Simulation

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 25: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Visual Interactive Simulation

25

bull Conventional Simulation Inadequacies

bull Visual Interactive Simulation (VIS)

ndashVisual Interactive Modeling (VIM)

bull Visual Interactive Models and DSS

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 26: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Simulation Software

26

bull Analytica (Lumina Decision Systems luminacom)

bull Excel add-ins Crystal Ball

bull RISK (Palisade Corp palisade com)

bull Arena

bull Simio (simiocom)

bull ExtendSim (extendsimcom)

bull SAS JMP

Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 27: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Monte Carlo Simulation

27Source Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 28: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

28

Monte Carlo Simulation to Forecast Stock Prices

httpscolabresearchgooglecomdrive1lwXt0TlidwF2jhPPE7tte2TH-JWb9QFk

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 29: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Python Pandas for Finance

29Source httpsmapattackwordpresscom20170212using-python-for-stocks-1

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 30: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

The Quant Finance PyData Stack

30Source httpnbviewerjupyterorgformatslidesgithubquantopianpyfolioblobmasterpyfolioexamplesoverview_slidesipynb5

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 31: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

31

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 32: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

32

Python in Google Colabhttpscolabresearchgooglecomdrive1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 33: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

pip install pandas_datareader

33

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 34: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

conda install pandas-datareader

34

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 35: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

35

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 36: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

36

pip install pandas_datareader

import pandas_datareaderdata as web

import datetime as dt

Read Stock Data from Yahoo Finance

end = dtdatetime(2017 12 31)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Yahoo Finance

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 37: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

37

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth

endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 38: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

38

df[Adj Close]plot(legend=True figsize=(12

8) title=AAPLrsquo label=Adj Close)

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 39: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

39

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0)

rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100)

rowspan=2 colspan=9)

topplot(dfindex df[Adj Close]

color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 40: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

40

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 41: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

41

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna()

bins=50 color=purple)

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 42: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

42

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 43: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

43

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean()

5 days

df[MA20] = df[Adj

Close]rolling(20)mean() 20 days

df[MA60] = df[Adj

Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj

Close]MA05 df[MA05]MA20 df[MA20]

MA60 df[MA60])

df2plot(figsize=(12 9) legend=True

title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 44: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

44

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 45: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

45

pip install pandas_datareader

import pandas as pd

import pandas_datareaderdata as web

import matplotlibpyplot as plt

import seaborn as sns

import datetime as dt

matplotlib inline

Read Stock Data from Yahoo Finance

end = dtdatetimenow()

start = dtdatetime(endyear-2 endmonth endday)

start = dtdatetime(2016 1 1)

df = webDataReader(AAPL yahoo start end)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

df[Adj Close]plot(legend=True figsize=(12 8) title=AAPL label=Adj Close)

pltfigure(figsize=(129))

top = pltsubplot2grid((129) (0 0) rowspan=10 colspan=9)

bottom = pltsubplot2grid((129) (100) rowspan=2 colspan=9)

topplot(dfindex df[Adj Close] color=blue) dfindex gives the dates

bottombar(dfindex df[Volume])

set the labels

topaxesget_xaxis()set_visible(False)

topset_title(AAPL)

topset_ylabel(Adj Close)

bottomset_ylabel(Volume)

pltfigure(figsize=(129))

snsdistplot(df[Adj Close]dropna() bins=50 color=purple)

simple moving averages

df[MA05] = df[Adj Close]rolling(5)mean() 5 days

df[MA20] = df[Adj Close]rolling(20)mean() 20 days

df[MA60] = df[Adj Close]rolling(60)mean() 60 days

df2 = pdDataFrame(Adj Close df[Adj Close]MA05 df[MA05]MA20 df[MA20] MA60 df[MA60])

df2plot(figsize=(12 9) legend=True title=AAPL)

df2to_csv(AAPL_MAcsv)

fig = pltgcf()

figset_size_inches(12 9)

figsavefig(AAPL_plotpng dpi=300)

pltshow()

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 46: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

46

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 47: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

47Source httpswwwquandlcomtoolspython

pip install quandl

import quandl quandlApiConfigapi_key = YOURAPIKEY

df = quandlget(WIKIAAPL start_date=2016-01-

01 end_date=2017-12-31)

dfto_csv(AAPLcsv)

dffrom_csv(AAPLcsv)

dftail()

Finance Data from Quandl

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 48: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

48httpfinanceyahoocomqs=AAPL

Yahoo Finance Symbols AAPL Apple Inc (AAPL)

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 49: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Dow Jones Industrial Average (^DJI)

49httpfinanceyahoocomchart^DJI

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 50: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

TSEC weighted index (^TWII) - Taiwan

50httpfinanceyahoocomchart^TWII

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 51: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

51httpfinanceyahoocomchart2330TW

Yahoo Finance Charts TSMC (2330TW)

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 52: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

52

import pandas as pd

import pandas_datareaderdata as web

df = webDataReader(AAPL data_source=yahoo

start=112010 end=3212017)

dfto_csv(AAPLcsv)

dftail()

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 53: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

53

df = webDataReader(GOOG

data_source=yahoo start=111980

end=3212017)

dfhead(10)

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 54: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

54

dftail(10)

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 55: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

55

dfcount()

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 56: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

56

dfix[2015-12-31]

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 57: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

57

dfto_csv(2330TWYahooFinanceDatacsv)

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 58: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

58

import fix_yahoo_finance as yf

data = yfdownload(^TWII start=2017-07-01 end=2017-11-15)

datato_csv(TWII_201707_201711csv)

datatail()

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 59: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

dfloc[startend]

59

df = dfloc[2017-10-012017-11-15]

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 60: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

60

import matplotlibpyplot as plt

matplotlib inline

import fix_yahoo_finance as yf

df = yfdownload(^TWII start=2000-01-01 end=2017-11-15)dfto_csv(YF_TWII_2000_2017csv)

print(dfhead())

fig = pltfigure(figsize=(169))df[Adj Close]plot()

figshow()

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 61: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

import matplotlibpyplot as plt

from matplotlibfinance

import candlestick_ohlc

61Source httpsmatplotliborgexamplespylab_examplesfinance_demohtml

candlestick_ohlc

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 62: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

62Source httpsntguardianwordpresscom20160919introduction-stock-market-data-python-1

import matplotlibpyplot as plt

from matplotlibfinance import candlestick_ohlc

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 63: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

daily_to_weekly

63

Convert Daily Data to Weekly Data

def daily_to_weekly(df)

dfWeekly = daily_to_weekly(df)dfsort_index(axis=0 level=None ascending=True inplace=True)

Open = dfOpenresample(W-Fri)first() W W-MON W-Fri

High = dfHighresample(W-Fri)max()

Low = dfLowresample(W-Fri)min()

Close = dfCloseresample(W-Fri)last()

Volume = dfVolumeresample(W-Fri)sum()

Adj_Close = df[Adj Close]resample(W-Fri)last()

dfWeekly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfWeekly = dfWeekly[pdnotnull(dfWeekly[Adj Close])]

return dfWeekly

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 64: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

daily_to_monthly

64

Convert Daily Data to Monthly Data

def daily_to_monthly(df)

dfMonthly = daily_to_monthly(df)

Open = dfOpenresample(M)first()

High = dfHighresample(M)max()

Low = dfLowresample(M)min()

Close = dfCloseresample(M)last()

Volume = dfVolumeresample(M)sum()

Adj_Close = df[Adj Close]resample(M)last()dfMonthly = pdconcat([Open High Low Close Volume Adj_Close] axis=1)

dfMonthly = dfMonthly[pdnotnull(dfMonthly[Adj Close])]

return dfMonthly

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 65: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

TA-LibTechnical Analysis Library

65httpta-liborg

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 66: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Stochastic Oscillator (KD)

66

Stochastic oscillator D

def KDJ(df n m1 m2)

KDJ(df 9 3 3)

KDJ_n = n

KDJ_m1 = m1

KDJ_m2 = m2

df[Low_n] = pdrolling_min(df[Low] KDJ_n)

df[Low_n]fillna(value=pdexpanding_min(df[Low]) inplace=True)

df[High_n] = pdrolling_max(df[High] KDJ_n)

df[High_n]fillna(value=pdexpanding_max(df[High]) inplace=True)

df[RSV] = (df[Close] - df[Low_n]) (df[High_n] - df[Low_n]) 100

df[KDJ_K] = pdewma(df[RSV] KDJ_m1)

df[KDJ_D] = pdewma(df[KDJ_K] KDJ_m2)

df[KDJ_J] = 3 df[KDJ_K] - 2 df[KDJ_D]

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 67: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Bollinger Bands

67

Bollinger Bands

def BBANDS20(df n)

MA = pdSeries(pdrolling_mean(df[Close] n))

MSD = pdSeries(pdrolling_std(df[Close] n))

b1 = 4 MSD MA

B1 = pdSeries(b1 name = BollingerB_ + str(n))

df = dfjoin(B1)

b2 = (df[Close] - MA + 2 MSD) (4 MSD)

B2 = pdSeries(b2 name = Bollingerb_ + str(n))

df = dfjoin(B2)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 68: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Bollinger Bands

68

BB Bollinger Bands BB_20

def BB_20(df)

df[BB_MA20] = pdstatsmomentsrolling_mean(df[Adj Close] 20)

df[BB_SD20] = pdstatsmomentsrolling_std(df[Adj Close]20)

df[BB_UpperBand] = df[BB_MA20] + (df[BB_SD20]2) Default 2SD

df[BB_LowerBand] = df[BB_MA20] - (df[BB_SD20]2)

df[BB_PB] = (df[Adj Close] - df[BB_LowerBand]) (df[BB_UpperBand] -

df[BB_LowerBand])

df[BB_BW] = (df[BB_UpperBand] - df[BB_LowerBand]) df[BB_MA20]

df[BB_UpperBand_1SD] = df[BB_MA20] + (df[BB_SD20])

df[BB_LowerBand_1SD] = df[BB_MA20] - (df[BB_SD20])

BB_PB Bollinger Band Percent b (PB)

BB_BW Bollinger Band Band Width (BW)

return df

Source httpswwwquantopiancompoststechnical-analysis-indicators-without-talib-code

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 69: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

AI + VDI POC

69

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 70: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

AI + VDI POSTensorFlow Models

bull M1 Basic Classification (Image Classification) (65 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_classificationipynb

bull M2 Basic Text Classification (Text Classification) (46 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_text_classificationipynb

bull M3 Basic Regression (Predict House Prices) (43 Seconds)ndash httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorials

kerasbasic_regressionipynb

bull M4 Pix2Pix Eager (Option) (7-8 Hours)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplespix2pixpix2pix_eageripynb

bull M5 NMT with Attention (Option) (20-30 minutes)ndash httpscolabresearchgooglecomgithubtensorflowtensorflowblobmastertensorflow

contribeagerpythonexamplesnmt_with_attentionnmt_with_attentionipynb

70

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 71: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Basic Classification Fashion MNIST Image Classification

71

httpscolabresearchgooglecomdrive19PJOJi1vn1kjcutlzNHjRSLbeVl4kd5z

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_classificationipynb

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 72: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Text ClassificationIMDB Movie Reviews

72

httpscolabresearchgooglecomdrive1x16h1GhHsLIrLYtPCvCHaoO1W-i_gror

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_text_classificationipynb

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 73: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Basic RegressionPredict House Prices

73

httpscolabresearchgooglecomdrive1v4c8ZHTnRtgd2_25K_AURjR6SCVBRdlj

Source httpscolabresearchgooglecomgithubtensorflowdocsblobmastersiteentutorialskerasbasic_regressionipynb

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 74: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

AI+VDI POC ISAC+TKU Test

bull AI+VDI POC Folder (3+1 ipynb) (v3020181120)ndash httpsdrivegooglecomopenid=1qHOemktbEmUz-ot8eFxIKbGwJvXlrjtc

bull run3modelsipynbndash httpscolabresearchgooglecomdrive1HQ1GrIqQUUPCct7_AVgoMwMrh0UqMm0f

bull AI+VDI POC ISAC+TKU Test Reportndash httpsdocsgooglecomspreadsheetsd1meMwqn15PSuTk6d5TgendDpdDX6L3OfHM4E0

SIkq1Zkeditusp=sharing

74

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 75: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

Summary

75

bull Prescriptive Analytics

bull Model-Based Decision Making

bull Structure of Mathematical Models for Decision Support

bull Certainty Uncertainty and Risk

bull Mathematical Programming Optimization

bull Simulation

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76

Page 76: Optimization and Simulation - komputasi.files.wordpress.com · Prescriptive Analytics: Optimization and Simulation 3. Outline 4 • Prescriptive Analytics • Model-Based Decision

References

bull Ramesh Sharda Dursun Delen and Efraim Turban (2017) Business Intelligence Analytics and Data Science A Managerial Perspective 4th Edition Pearson

bull Jake VanderPlas (2016) Python Data Science Handbook Essential Tools for Working with Data OReilly Media

76


Recommended