# -*- coding: utf-8 -*-
import pandas_datareader.data as web
import datetime
import numpy as np
import matplotlib.pyplot as plt
import pickle
import os
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)
def make_train_data():
start = datetime.datetime(2015,1,2)
end = datetime.datetime(2016,7,22)
f1 = web.DataReader("^DJI",'yahoo',start,end)
f2 = web.DataReader("^KS11",'yahoo',start,end)
dates = []
x_data = []
y_data = []
for single_date in daterange(start, end):
date_str = single_date.strftime('%Y-%m-%d')
try:
f1_close = f1.ix[date_str]['Close']
except:
continue
try:
f2_close = f2.ix[date_str]['Close']
except:
continue
x_data.append(f1_close)
y_data.append(f2_close)
dates.append(date_str)
return dates, x_data, y_data
def make_poly_x(pn, x_data):
x_datas = []
for n in range(pn+1):
x_datas.append([ x ** n for x in x_data ])
return x_datas
def print_hypothesis(pn, theta):
print('h(X) = ')
for i, element in enumerate(theta):
if i == len(theta) - 1:
print(element.item(0), " X ** ", i)
else:
print(element.item(0), " X ** ", i, " + ")
def get_hypothesis(pn, theta, x):
y = 0
for i, element in enumerate(theta):
y += ( element.item(0) * ( x ** i) )
return y
def get_sumerror(pn, theta, x_data, y_data):
s = 0
for i, x in enumerate(x_data):
s += ( (y_data[i] - get_hypothesis(pn, theta, x)) ** 2)
return s
# 프로그램 시작
PICKLE_FILE = 'data.pck'
import os.path
if os.path.isfile(PICKLE_FILE):
f = open('data.pck', 'rb')
dates, x_data, y_data = pickle.load(f)
else:
dates, x_data, y_data = make_train_data()
f = open('data.pck', 'wb')
pickle.dump([dates, x_data, y_data], f)
f.close()
for i in range(len(dates)):
print(dates[i], x_data[i], y_data[i])
# 정규방정식(normal equation) 알고리즘을 통해 cost 최소가 되는 가설함수를 찾습니다.
print()
POLY = 1
x_datas = make_poly_x(POLY, x_data)
X = np.matrix(x_datas).transpose()
Xt = np.matrix(x_datas)
Y = np.matrix(y_data).transpose()
theta = np.dot(np.dot(np.linalg.pinv(np.dot(Xt, X)), Xt), Y)
print_hypothesis(POLY, theta)
print('get_sumerror : ', get_sumerror(POLY, theta, x_data, y_data))
# 다우존스지수가 18313.77일 때 코스피지수 값을 예측합니다.
print()
x_test = 18313.77
y_test = get_hypothesis(POLY, theta, x_test)
print('2016-08-02 DOW30 : ', x_test)
print('2016-08-02 estimated KOSPI : ', y_test)
print('2016-08-02 KOSPI : ', 2019.03)
# 그래프를 그립니다.
plt.plot(x_data, y_data, 'ro')
line_x = [ x for x in range( int(min(x_data)), int(max(x_data)) ) ]
line_y = [ get_hypothesis(POLY, theta, x) for x in range( int(min(x_data)), int(max(x_data)) ) ]
plt.plot(line_x, line_y)
plt.show()
2015-01-02 17832.990234 1926.439941 2015-01-05 17501.650391 1915.75 2015-01-06 17371.640625 1882.449951 2015-01-07 17584.519531 1883.829956 2015-01-08 17907.869141 1904.650024 2015-01-09 17737.369141 1924.699951 2015-01-12 17640.839844 1920.949951 2015-01-13 17613.679688 1917.140015 2015-01-14 17427.089844 1913.660034 2015-01-15 17320.710938 1914.140015 2015-01-16 17511.570312 1888.130005 2015-01-20 17515.230469 1918.310059 2015-01-21 17554.279297 1921.22998 2015-01-22 17813.980469 1920.819946 2015-01-23 17672.599609 1936.089966 2015-01-26 17678.699219 1935.680054 2015-01-27 17387.210938 1952.400024 2015-01-28 17191.369141 1961.579956 2015-01-29 17416.849609 1951.02002 2015-01-30 17164.949219 1949.26001 2015-02-02 17361.039062 1952.680054 2015-02-03 17666.400391 1951.959961 2015-02-04 17673.019531 1962.790039 2015-02-05 17884.880859 1952.839966 2015-02-06 17824.289062 1955.52002 2015-02-09 17729.210938 1947.0 2015-02-10 17868.759766 1935.859985 2015-02-11 17862.140625 1945.699951 2015-02-12 17972.380859 1941.630005 2015-02-13 18019.349609 1957.5 2015-02-17 18047.580078 1961.449951 2015-02-18 18029.849609 1961.449951 2015-02-20 18140.439453 1961.449951 2015-02-23 18116.839844 1968.390015 2015-02-24 18209.189453 1976.119995 2015-02-25 18224.570312 1990.469971 2015-02-26 18214.419922 1993.079956 2015-02-27 18132.699219 1985.800049 2015-03-02 18288.630859 1996.810059 2015-03-03 18203.369141 2001.380005 2015-03-04 18096.900391 1998.290039 2015-03-05 18135.720703 1998.380005 2015-03-06 17856.779297 2012.939941 2015-03-09 17995.720703 1992.819946 2015-03-10 17662.939453 1984.77002 2015-03-11 17635.390625 1980.829956 2015-03-12 17895.220703 1970.589966 2015-03-13 17749.310547 1985.790039 2015-03-16 17977.419922 1987.329956 2015-03-17 17849.080078 2029.910034 2015-03-18 18076.189453 2028.449951 2015-03-19 17959.029297 2037.890015 2015-03-20 18127.650391 2037.23999 2015-03-23 18116.039062 2036.589966 2015-03-24 18011.140625 2041.369995 2015-03-25 17718.539062 2042.810059 2015-03-26 17678.230469 2022.560059 2015-03-27 17712.660156 2019.800049 2015-03-30 17976.310547 2030.040039 2015-03-31 17776.119141 2041.030029 2015-04-01 17698.179688 2028.449951 2015-04-02 17763.240234 2029.069946 2015-04-06 17880.849609 2046.430054 2015-04-07 17875.419922 2047.030029 2015-04-08 17902.509766 2059.26001 2015-04-09 17958.730469 2058.870117 2015-04-10 18057.650391 2087.76001 2015-04-13 17977.039062 2098.919922 2015-04-14 18036.699219 2111.719971 2015-04-15 18112.609375 2119.959961 2015-04-16 18105.769531 2139.899902 2015-04-17 17826.300781 2143.5 2015-04-20 18034.929688 2146.709961 2015-04-21 17949.589844 2144.790039 2015-04-22 18038.269531 2143.889893 2015-04-23 18058.689453 2173.409912 2015-04-24 18080.140625 2159.800049 2015-04-27 18037.970703 2157.540039 2015-04-28 18110.140625 2147.669922 2015-04-29 18035.529297 2142.629883 2015-04-30 17840.519531 2127.169922 2015-05-01 18024.060547 2127.169922 2015-05-04 18070.400391 2132.22998 2015-05-06 17841.980469 2104.580078 2015-05-07 17924.060547 2091.0 2015-05-08 18191.109375 2085.52002 2015-05-11 18105.169922 2097.379883 2015-05-12 18068.230469 2096.77002 2015-05-13 18060.490234 2114.159912 2015-05-14 18252.240234 2120.330078 2015-05-15 18272.560547 2106.5 2015-05-18 18298.880859 2113.719971 2015-05-19 18312.390625 2120.850098 2015-05-20 18285.400391 2139.540039 2015-05-21 18285.740234 2122.810059 2015-05-22 18232.019531 2146.100098 2015-05-26 18041.539062 2143.5 2015-05-27 18162.990234 2107.5 2015-05-28 18126.119141 2110.889893 2015-05-29 18010.679688 2114.800049 2015-06-01 18040.369141 2102.370117 2015-06-02 18011.939453 2078.639893 2015-06-03 18076.269531 2063.159912 2015-06-04 17905.580078 2072.860107 2015-06-05 17849.460938 2068.100098 2015-06-08 17766.550781 2065.189941 2015-06-09 17764.039062 2064.030029 2015-06-10 18000.400391 2051.320068 2015-06-11 18039.369141 2056.610107 2015-06-12 17898.839844 2052.169922 2015-06-15 17791.169922 2042.319946 2015-06-16 17904.480469 2028.719971 2015-06-17 17935.740234 2034.859985 2015-06-18 18115.839844 2041.880005 2015-06-19 18015.949219 2046.959961 2015-06-22 18119.779297 2055.159912 2015-06-23 18144.070312 2081.199951 2015-06-24 17966.070312 2085.530029 2015-06-25 17890.359375 2085.060059 2015-06-26 17946.679688 2090.26001 2015-06-29 17596.349609 2060.48999 2015-06-30 17619.509766 2074.199951 2015-07-01 17757.910156 2097.889893 2015-07-02 17730.109375 2107.330078 2015-07-06 17683.580078 2053.929932 2015-07-07 17776.910156 2040.290039 2015-07-08 17515.419922 2016.209961 2015-07-09 17548.619141 2027.810059 2015-07-10 17760.410156 2031.170044 2015-07-13 17977.679688 2061.52002 2015-07-14 18053.580078 2059.22998 2015-07-15 18050.169922 2072.909912 2015-07-16 18120.25 2087.889893 2015-07-17 18086.449219 2076.790039 2015-07-20 18100.410156 2073.310059 2015-07-21 17919.289062 2083.620117 2015-07-22 17851.039062 2064.72998 2015-07-23 17731.919922 2065.070068 2015-07-24 17568.529297 2045.959961 2015-07-27 17440.589844 2038.810059 2015-07-28 17630.269531 2039.099976 2015-07-29 17751.390625 2037.619995 2015-07-30 17745.980469 2019.030029 2015-07-31 17689.859375 2030.160034 2015-08-03 17598.199219 2008.48999 2015-08-04 17550.689453 2027.98999 2015-08-05 17540.470703 2029.76001 2015-08-06 17419.75 2013.290039 2015-08-07 17373.380859 2010.22998 2015-08-10 17615.169922 2003.170044 2015-08-11 17402.839844 1986.650024 2015-08-12 17402.509766 1975.469971 2015-08-13 17408.25 1983.459961 2015-08-17 17545.179688 1968.52002 2015-08-18 17511.339844 1956.26001 2015-08-19 17348.730469 1939.380005 2015-08-20 16990.689453 1914.550049 2015-08-21 16459.75 1876.069946 2015-08-24 15871.349609 1829.810059 2015-08-25 15666.44043 1846.630005 2015-08-26 16285.509766 1894.089966 2015-08-27 16654.769531 1908.0 2015-08-28 16643.009766 1937.670044 2015-08-31 16528.029297 1941.48999 2015-09-01 16058.349609 1914.22998 2015-09-02 16351.379883 1915.219971 2015-09-03 16374.759766 1915.530029 2015-09-04 16102.379883 1886.040039 2015-09-08 16492.679688 1878.680054 2015-09-09 16253.570312 1934.199951 2015-09-10 16330.400391 1962.109985 2015-09-11 16433.089844 1941.369995 2015-09-14 16370.959961 1931.459961 2015-09-15 16599.849609 1937.560059 2015-09-16 16739.949219 1975.449951 2015-09-17 16674.740234 1976.48999 2015-09-18 16384.580078 1995.949951 2015-09-21 16510.189453 1964.680054 2015-09-22 16330.469727 1982.060059 2015-09-23 16279.889648 1944.640015 2015-09-24 16201.320312 1947.099976 2015-09-25 16314.669922 1942.849976 2015-09-30 16284.700195 1962.810059 2015-10-01 16272.009766 1979.319946 2015-10-02 16472.369141 1969.680054 2015-10-05 16776.429688 1978.25 2015-10-06 16790.189453 1990.650024 2015-10-07 16912.289062 2005.839966 2015-10-08 17050.75 2019.530029 2015-10-12 17131.859375 2021.630005 2015-10-13 17081.890625 2019.050049 2015-10-14 16924.75 2009.550049 2015-10-15 17141.75 2033.27002 2015-10-16 17215.970703 2030.26001 2015-10-19 17230.539062 2030.27002 2015-10-20 17217.109375 2039.359985 2015-10-21 17168.609375 2042.97998 2015-10-22 17489.160156 2023.0 2015-10-23 17646.699219 2040.400024 2015-10-26 17623.050781 2048.080078 2015-10-27 17581.429688 2044.650024 2015-10-28 17779.519531 2042.51001 2015-10-29 17755.800781 2034.160034 2015-10-30 17663.539062 2029.469971 2015-11-02 17828.759766 2035.23999 2015-11-03 17918.150391 2048.399902 2015-11-04 17867.580078 2052.77002 2015-11-05 17863.429688 2049.409912 2015-11-06 17910.330078 2041.069946 2015-11-09 17730.480469 2025.699951 2015-11-10 17758.210938 1996.589966 2015-11-11 17702.220703 1997.27002 2015-11-12 17448.070312 1993.359985 2015-11-13 17245.240234 1973.290039 2015-11-16 17483.009766 1943.02002 2015-11-17 17489.5 1963.579956 2015-11-18 17737.160156 1962.880005 2015-11-19 17732.75 1988.910034 2015-11-20 17823.810547 1989.859985 2015-11-23 17792.679688 2003.699951 2015-11-24 17812.189453 2016.290039 2015-11-25 17813.390625 2009.420044 2015-11-27 17813.390625 2028.98999 2015-11-30 17719.919922 1991.969971 2015-12-01 17888.349609 2023.930054 2015-12-02 17729.679688 2009.290039 2015-12-03 17477.669922 1994.069946 2015-12-04 17847.630859 1974.400024 2015-12-07 17730.509766 1963.670044 2015-12-08 17568.0 1949.040039 2015-12-09 17492.300781 1948.23999 2015-12-10 17574.75 1952.069946 2015-12-11 17265.210938 1948.619995 2015-12-14 17368.5 1927.819946 2015-12-15 17524.910156 1932.969971 2015-12-16 17749.089844 1969.400024 2015-12-17 17495.839844 1977.959961 2015-12-18 17128.550781 1975.319946 2015-12-21 17251.619141 1981.189941 2015-12-22 17417.269531 1992.560059 2015-12-23 17602.609375 1999.219971 2015-12-24 17552.169922 1990.650024 2015-12-28 17528.269531 1964.060059 2015-12-29 17720.980469 1966.310059 2015-12-30 17603.869141 1961.310059 2016-01-04 17148.939453 1918.76001 2016-01-05 17158.660156 1930.530029 2016-01-06 16906.509766 1925.430054 2016-01-07 16514.099609 1904.329956 2016-01-08 16346.450195 1917.619995 2016-01-11 16398.570312 1894.839966 2016-01-12 16516.220703 1890.859985 2016-01-13 16151.410156 1916.280029 2016-01-14 16379.049805 1900.01001 2016-01-15 15988.080078 1878.869995 2016-01-19 16016.019531 1889.640015 2016-01-20 15766.740234 1845.449951 2016-01-21 15882.679688 1840.530029 2016-01-22 16093.509766 1879.430054 2016-01-25 15885.219727 1893.430054 2016-01-26 16167.230469 1871.689941 2016-01-27 15944.459961 1897.869995 2016-01-28 16069.639648 1906.939941 2016-01-29 16466.300781 1912.060059 2016-02-01 16449.179688 1924.819946 2016-02-02 16153.540039 1906.599976 2016-02-03 16336.660156 1890.670044 2016-02-04 16416.580078 1916.26001 2016-02-05 16204.969727 1917.790039 2016-02-11 15660.179688 1861.540039 2016-02-12 15973.839844 1835.280029 2016-02-16 16196.410156 1888.300049 2016-02-17 16453.830078 1883.939941 2016-02-18 16413.429688 1908.839966 2016-02-19 16391.990234 1916.23999 2016-02-22 16620.660156 1916.359985 2016-02-23 16431.779297 1914.219971 2016-02-24 16484.990234 1912.530029 2016-02-25 16697.289062 1918.569946 2016-02-26 16639.970703 1920.160034 2016-02-29 16516.5 1916.660034 2016-03-02 16899.320312 1947.420044 2016-03-03 16943.900391 1958.170044 2016-03-04 17006.769531 1955.630005 2016-03-07 17073.949219 1957.869995 2016-03-08 16964.099609 1946.119995 2016-03-09 17000.359375 1952.949951 2016-03-10 16995.130859 1969.329956 2016-03-11 17213.310547 1971.410034 2016-03-14 17229.130859 1972.27002 2016-03-15 17251.529297 1969.969971 2016-03-16 17325.759766 1974.900024 2016-03-17 17481.490234 1987.98999 2016-03-18 17602.300781 1992.119995 2016-03-21 17623.869141 1989.76001 2016-03-22 17582.570312 1996.810059 2016-03-23 17502.589844 1995.119995 2016-03-24 17515.730469 1985.969971 2016-03-28 17535.390625 1982.540039 2016-03-29 17633.109375 1994.910034 2016-03-30 17716.660156 2002.140015 2016-03-31 17685.089844 1995.849976 2016-04-01 17792.75 1973.569946 2016-04-04 17737.0 1978.969971 2016-04-05 17603.320312 1962.73999 2016-04-06 17716.050781 1971.319946 2016-04-07 17541.960938 1973.890015 2016-04-08 17576.960938 1972.050049 2016-04-11 17556.410156 1970.369995 2016-04-12 17721.25 1981.319946 2016-04-14 17926.429688 2015.930054 2016-04-15 17897.460938 2014.709961 2016-04-18 18004.160156 2009.099976 2016-04-19 18053.599609 2011.359985 2016-04-20 18096.269531 2005.829956 2016-04-21 17982.519531 2022.099976 2016-04-22 18003.75 2015.48999 2016-04-25 17977.240234 2014.550049 2016-04-26 17990.320312 2019.630005 2016-04-27 18041.550781 2015.400024 2016-04-28 17830.759766 2000.930054 2016-04-29 17773.640625 1994.150024 2016-05-02 17891.160156 1978.150024 2016-05-03 17750.910156 1986.410034 2016-05-04 17651.259766 1976.709961 2016-05-09 17705.910156 1967.810059 2016-05-10 17928.349609 1982.5 2016-05-11 17711.119141 1980.099976 2016-05-12 17720.5 1977.48999 2016-05-13 17535.320312 1966.98999 2016-05-16 17710.710938 1967.910034 2016-05-17 17529.980469 1968.060059 2016-05-18 17526.619141 1956.72998 2016-05-19 17435.400391 1946.780029 2016-05-20 17500.939453 1947.670044 2016-05-23 17492.929688 1955.25 2016-05-24 17706.050781 1937.680054 2016-05-25 17851.509766 1960.51001 2016-05-26 17828.289062 1957.060059 2016-05-27 17873.220703 1969.170044 2016-05-31 17787.199219 1983.400024 2016-06-01 17789.669922 1982.719971 2016-06-02 17838.560547 1985.109985 2016-06-03 17807.060547 1985.839966 2016-06-07 17938.279297 2011.630005 2016-06-08 18005.050781 2027.079956 2016-06-09 17985.189453 2024.170044 2016-06-10 17865.339844 2017.630005 2016-06-13 17732.480469 1979.060059 2016-06-14 17674.820312 1972.030029 2016-06-15 17640.169922 1968.829956 2016-06-16 17733.099609 1951.98999 2016-06-17 17675.160156 1953.400024 2016-06-20 17804.869141 1981.119995 2016-06-21 17829.730469 1982.699951 2016-06-22 17780.830078 1992.579956 2016-06-23 18011.070312 1986.709961 2016-06-24 17400.75 1925.23999 2016-06-27 17140.240234 1926.849976 2016-06-28 17409.720703 1936.219971 2016-06-29 17694.679688 1956.359985 2016-06-30 17929.990234 1970.349976 2016-07-01 17949.369141 1987.319946 2016-07-05 17840.619141 1989.849976 2016-07-06 17918.619141 1953.119995 2016-07-07 17895.880859 1974.079956 2016-07-08 18146.740234 1963.099976 2016-07-11 18226.929688 1988.540039 2016-07-12 18347.669922 1991.22998 2016-07-13 18372.119141 2005.550049 2016-07-14 18506.410156 2008.77002 2016-07-15 18516.550781 2017.26001 2016-07-18 18533.050781 2021.109985 2016-07-19 18559.009766 2016.890015 2016-07-20 18595.029297 2015.459961 2016-07-21 18517.230469 2012.219971 h(X) = 737.058366257889 X ** 0 + 0.07175867222677387 X ** 1 get_sumerror : 884608.869684 2016-08-02 DOW30 : 18313.77 2016-08-02 estimated KOSPI : 2051.2301849244136 2016-08-02 KOSPI : 2019.03