Example #1
0
        def proceed_with_method():

            weather_dict_raw = Chateau(self.address).weather_dict(category)
            price_dict_raw = Chateau_data(self.address).get_price_data()

            weather_dict = dict_unpacker(weather_dict_raw)
            price_dict = dict_unpacker(price_dict_raw)

            correl_dict = {}

            def correl_calculator(chosen_month):
                monthly_weather_dict = all_monthly_weather_dict(
                    weather_dict, chosen_month)

                #find the average price
                px_list = []
                for px_date, price in price_dict.items():
                    if px_date > datetime(1970, 12, 31):
                        px_list.append(price)

                av = statistics.mean(px_list)
                sd = statistics.stdev(px_list)

                #add prices above average to the selected list
                selected_price_dict = {}
                y_values = []
                for px_date, price in price_dict.items():
                    if price > av + 0.5 * sd and px_date > datetime(
                            1970, 12, 31):
                        selected_price_dict[px_date] = price
                        y_values.append(price)

                #find weather data for price year
                x_values = []
                for px_date, price in selected_price_dict.items():
                    y = px_date.year
                    m = chosen_month
                    relevant_date = eomonth(y, m)
                    addition = monthly_weather_dict[relevant_date]
                    x_values.append(addition)

                #calculate best fit line
                x = x_values
                y = y_values
                z = np.polyfit(x, y, 2)
                p = np.poly1d(z)
                xp = np.linspace(min(x_values), max(x_values), 100)

                #calculate correlation coefficient
                correl_y = p(x)
                R = np.corrcoef(y, correl_y)
                cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
                #print(str(chosen_month) + ": " + str(cor))
                return [cor, z.item(0), z.item(1), z.item(2)]

            for i in range(1, 13):
                correl_dict[i] = correl_calculator(i)

            return correl_dict
Example #2
0
        def proceed_with_method():

            weather_dict_raw = Chateau(self.address).weather_dict(category)
            price_dict_raw = Chateau_data(self.address).get_price_data()

            #unpack the dictionaries so the dates have the correct format
            def dict_unpacker(sample_dict):
                dict_unpacked = {}
                for p_date, price in sample_dict.items():
                    formattedas_date = datetime.strptime(p_date, "%Y-%m-%d")
                    dict_unpacked[formattedas_date] = float(price)
                return dict_unpacked

            weather_dict = dict_unpacker(weather_dict_raw)
            price_dict = dict_unpacker(price_dict_raw)

            def eomonth(y, m):
                """returns eomonth for a year and month"""
                year = y
                if m == 12:
                    month = 1
                else:
                    month = int(m) + 1
                given_day = datetime(year, month, 1)
                required_day = given_day - timedelta(days=1)
                return required_day

            def seasonal_weather_dict(dict_sample):
                """returns dict of average daily weather data aggregated by month"""
                monthly_weather_dict = {}
                for p_date, precip in dict_sample.items():
                    formattedas_date = p_date
                    y = formattedas_date.year
                    m = formattedas_date.month
                    needed_day = eomonth(y, m)
                    if needed_day in monthly_weather_dict.keys():
                        x = monthly_weather_dict[needed_day][0] + float(precip)
                        y = monthly_weather_dict[needed_day][1] + 1
                        monthly_weather_dict[needed_day] = [x, y]
                    else:
                        monthly_weather_dict[needed_day] = [float(precip), 1]

                monthly_weather_dict_av = {}
                for needed_day in monthly_weather_dict.keys():
                    d = monthly_weather_dict[needed_day][
                        0] / monthly_weather_dict[needed_day][1]
                    monthly_weather_dict_av[needed_day] = d
                return monthly_weather_dict_av

            def all_monthly_weather_dict(dict_sample):
                """returns dict of mean, stdev for every month"""
                monthly_weather_dict = seasonal_weather_dict(dict_sample)
                all_monthly_weather_dict = {}
                for chosen_month in range(1, 13):
                    new_list = []
                    for p_date, precip in monthly_weather_dict.items():
                        if p_date.month == int(
                                chosen_month) and p_date > datetime(
                                    1970, 12, 31):
                            new_list.append(precip)
                    av = statistics.mean(new_list)
                    sd = statistics.stdev(new_list)
                    all_monthly_weather_dict[chosen_month] = [av, sd]
                return all_monthly_weather_dict

        # monthly_weather_dict = all_monthly_weather_dict(weather_dict, chosen_month)

        #find the list of top prices
            prices, prices_dates, top_prices, lower_prices, top_prices_dates, lower_prices_dates = [], [], [], [], [], []
            for px_date, price in price_dict.items():
                if px_date > datetime(1980, 12, 31):
                    prices.append(price)
                    prices_dates.append(px_date)

            av = statistics.mean(prices)
            sd = statistics.stdev(prices)

            print("\nAverage/Stdev price is: " + str(av) + "/ " + str(sd) +
                  "\n")

            for i in range(0, len(prices) - 1):
                if prices[i] > av + 0.5 * sd:
                    top_prices.append(prices[i])
                    top_prices_dates.append(prices_dates[i])
                elif prices[i] < av - 0.5 * sd:
                    lower_prices.append(prices[i])
                    lower_prices_dates.append(prices_dates[i])

            poor_criteria_dict = {}

            for i in range(1, 13):
                poor_criteria_dict[i] = []

            weather_stats = all_monthly_weather_dict(weather_dict)
            print(weather_stats)
            specific_weather_dict = seasonal_weather_dict(weather_dict)
            #find poor price criteria
            for lower_price_date in lower_prices_dates:
                y = lower_price_date.year
                for i in range(1, 13):
                    m = i
                    weather_date = eomonth(y, m)
                    weather_stat = specific_weather_dict[weather_date]
                    av = weather_stats[i][0]
                    sd = weather_stats[i][1]

                    if weather_stat > av + 1 * sd:
                        title = "sig over av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

                    elif weather_stat > av + 0.5 * sd:
                        title = "slight over av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

                    elif weather_stat < av - 1 * sd:
                        title = "slight under av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

                    elif weather_stat < av - 0.5 * sd:
                        title = "sig under av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

            #find top price criteria

            top_criteria_dict = {}

            for i in range(1, 13):
                top_criteria_dict[i] = []

            for top_price_date in top_prices_dates:
                y = top_price_date.year
                for i in range(1, 13):
                    m = i
                    weather_date = eomonth(y, m)
                    weather_stat = specific_weather_dict[weather_date]
                    av = weather_stats[i][0]
                    sd = weather_stats[i][1]

                    if weather_stat > av + 1 * sd:
                        title = "sig over av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

                    elif weather_stat > av + 0.5 * sd:
                        title = "slight over av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

                    elif weather_stat < av - 1 * sd:
                        title = "sig under av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

                    elif weather_stat < av - 0.5 * sd:
                        title = "slight under av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

            bad_criteria_dict = {}

            for i in range(1, 13):
                bad_criteria_dict[i] = []

            for i in range(1, 13):
                y = [
                    x for x in poor_criteria_dict[i]
                    if x not in top_criteria_dict[i]
                ]
                bad_criteria_dict[i] = y

            good_criteria_dict = {}

            for i in range(1, 13):
                good_criteria_dict[i] = []

            for i in range(1, 13):
                y = [
                    x for x in top_criteria_dict[i]
                    if x not in poor_criteria_dict[i]
                ]
                good_criteria_dict[i] = y

            print(
                "\n" + str(self.address) +
                " great vintages have had these seasonal weather anomalies: " +
                str(top_criteria_dict))
            print(
                "\n" + str(self.address) +
                " standard vintages have had these seasonal weather anomalies: "
                + str(poor_criteria_dict))
            print(
                "\n" + str(self.address) +
                " we will use the following criteria as bad vintage indicators: "
                + str(bad_criteria_dict))
            print(
                "\n" + str(self.address) +
                " we will use the following criteria as good vintage indicators: "
                + str(good_criteria_dict))
Example #3
0
        def proceed_with_method():

            weather_dict_raw = Chateau(self.address).weather_dict(category)
            price_dict_raw = Chateau_data(self.address).get_price_data()

            #unpack the dictionaries so the dates have the correct format
            def dict_unpacker(sample_dict):
                dict_unpacked = {}
                for p_date, price in sample_dict.items():
                    formattedas_date = datetime.strptime(p_date, "%Y-%m-%d")
                    dict_unpacked[formattedas_date] = float(price)
                return dict_unpacked

            weather_dict = dict_unpacker(weather_dict_raw)
            price_dict = dict_unpacker(price_dict_raw)

            def eomonth(y, m):
                """returns eomonth for a year and month"""
                year = y
                if m == 12:
                    month = 1
                else:
                    month = int(m) + 1
                given_day = datetime(year, month, 1)
                required_day = given_day - timedelta(days=1)
                return required_day

            def seasonal_weather_dict(dict_sample):
                """returns dict of average daily weather data aggregated by month"""
                monthly_weather_dict = {}
                for p_date, precip in dict_sample.items():
                    formattedas_date = p_date
                    y = formattedas_date.year
                    m = formattedas_date.month
                    needed_day = eomonth(y, m)
                    if needed_day in monthly_weather_dict.keys():
                        x = monthly_weather_dict[needed_day][0] + float(precip)
                        y = monthly_weather_dict[needed_day][1] + 1
                        monthly_weather_dict[needed_day] = [x, y]
                    else:
                        monthly_weather_dict[needed_day] = [float(precip), 1]

                monthly_weather_dict_av = {}
                for needed_day in monthly_weather_dict.keys():
                    d = monthly_weather_dict[needed_day][
                        0] / monthly_weather_dict[needed_day][1]
                    monthly_weather_dict_av[needed_day] = d
                return monthly_weather_dict_av

            def all_monthly_weather_dict(dict_sample, chosen_month):
                """returns dict of weather data aggregated by month for all years"""
                monthly_weather_dict = seasonal_weather_dict(dict_sample)
                all_monthly_weather_dict_final = {}
                for p_date, precip in monthly_weather_dict.items():
                    if p_date.month == int(chosen_month):
                        all_monthly_weather_dict_final[p_date] = precip
                return all_monthly_weather_dict_final

            monthly_weather_dict = all_monthly_weather_dict(
                weather_dict, chosen_month)

            #find the average price
            px_list = []
            for px_date, price in price_dict.items():
                if px_date > datetime(1960, 12, 31):
                    px_list.append(price)

            av = statistics.mean(px_list)
            sd = statistics.stdev(px_list)

            print(str(av) + " is the average price for " + str(self.address))

            #add prices above average to the selected list
            selected_price_dict = {}
            y_values = []
            for px_date, price in price_dict.items():
                if price > av + 0.5 * sd and px_date > datetime(1960, 12, 31):
                    selected_price_dict[px_date] = price
                    y_values.append(price)

            #find weather data for price year
            x_values = []
            for px_date, price in selected_price_dict.items():
                y = px_date.year
                m = chosen_month
                relevant_date = eomonth(y, m)
                addition = monthly_weather_dict[relevant_date]
                x_values.append(addition)

            #calculate best fit line
            x = x_values
            y = y_values
            z = np.polyfit(x, y, 2)
            p = np.poly1d(z)
            xp = np.linspace(min(x_values), max(x_values), 100)

            #calculate correlation coefficient
            correl_y = p(x)
            #A = np.vstack([x, np.ones(len(x))]).T
            #m, c = np.linalg.lstsq(A, correl_y, rcond=None)[0]
            #print(m, c)
            R = np.corrcoef(y, correl_y)
            cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
            print("\nCorrelation coefficient: " + str(cor))

            print("\nSuggested polynomial a*x^2 + bx + c has [a, b, c]: " +
                  str(z))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel(str(category) + " avg daily", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Price", color='black', fontsize=12)
            plt.scatter(x_values, y_values, color=color)
            plt.plot(xp, p(xp), color='red')
            plt.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Weather Pattern for Month:  " +
                      str(chosen_month),
                      fontsize=14)

            #Show chart
            plt.show()
Example #4
0
        def proceed_with_method():

            weather_dict = Chateau(self.address).weather_dict(category)

            def eomonth(y, m):
                year = y
                if m == 12:
                    month = 1
                else:
                    month = int(m) + 1
                given_day = datetime(year, month, 1)
                required_day = given_day - timedelta(days=1)
                return required_day

            def seasonal_weather_dict(dict_sample):
                """returns dict of average daily weather data aggregated by month"""
                monthly_weather_dict = {}
                for p_date, precip in dict_sample.items():
                    formattedas_date = datetime.strptime(p_date, "%Y-%m-%d")
                    y = formattedas_date.year
                    m = formattedas_date.month
                    needed_day = eomonth(y, m)
                    if needed_day in monthly_weather_dict.keys():
                        x = monthly_weather_dict[needed_day][0] + float(precip)
                        y = monthly_weather_dict[needed_day][1] + 1
                        monthly_weather_dict[needed_day] = [x, y]
                    else:
                        monthly_weather_dict[needed_day] = [float(precip), 1]

                monthly_weather_dict_av = {}
                for needed_day in monthly_weather_dict.keys():
                    d = monthly_weather_dict[needed_day][
                        0] / monthly_weather_dict[needed_day][1]
                    monthly_weather_dict_av[needed_day] = d
                return monthly_weather_dict_av

            def all_monthly_weather_dict(dict_sample, chosen_month):
                """returns dict of weather data aggregated by month for vintage year"""
                monthly_weather_dict = seasonal_weather_dict(dict_sample)
                all_monthly_weather_dict_final = {}
                for p_date, precip in monthly_weather_dict.items():
                    if p_date.month == int(chosen_month):
                        all_monthly_weather_dict_final[p_date] = precip
                return all_monthly_weather_dict_final

            monthly_weather_dict = all_monthly_weather_dict(
                weather_dict, chosen_month)
            price_dict = Chateau_data(self.address).get_price_data()

            # start chart
            x_values, y_values = [], []

            for key in monthly_weather_dict.keys():
                x_date = key
                #datetime.strptime(key, '%Y-%m-%d')
                if x_date > datetime(1970, 12, 31):
                    try:
                        y = int(x_date.year)
                        x_date_eoy = date(y, 12, 31)
                        y_values.append(price_dict[str(x_date_eoy)])
                        x_values.append(float(str(monthly_weather_dict[key])))
                    except KeyError:
                        None
                    else:
                        None

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel(str(category) + " avg daily", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Price", color='black', fontsize=12)
            plt.scatter(x_values, y_values, color=color)
            plt.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Weather Pattern for Month:  " +
                      str(chosen_month),
                      fontsize=14)

            #Show chart
            plt.show()
Example #5
0
        def proceed_with_method():

            weather_dict_p = Chateau(self.address).weather_dict('p')
            weather_dict_v = Chateau(self.address).weather_dict('v')

            #turn weather_dict into annual rainfall

            def eomonth(y, m):
                year = y
                if m == 12:
                    month = 1
                else:
                    month = int(m) + 1
                given_day = datetime(year, month, 1)
                required_day = given_day - timedelta(days=1)
                return required_day

            def seasonal_weather_dict(dict_sample):
                """returns dict of average daily weather data aggregated by month"""
                monthly_weather_dict = {}
                for p_date, precip in dict_sample.items():
                    formattedas_date = datetime.strptime(p_date, "%Y-%m-%d")
                    y = formattedas_date.year
                    m = formattedas_date.month
                    needed_day = eomonth(y, m)
                    if needed_day in monthly_weather_dict.keys():
                        x = monthly_weather_dict[needed_day][0] + float(precip)
                        y = monthly_weather_dict[needed_day][1] + 1
                        monthly_weather_dict[needed_day] = [x, y]
                    else:
                        monthly_weather_dict[needed_day] = [float(precip), 1]

                monthly_weather_dict_av = {}
                for needed_day in monthly_weather_dict.keys():
                    d = monthly_weather_dict[needed_day][
                        0] / monthly_weather_dict[needed_day][1]
                    monthly_weather_dict_av[needed_day] = d
                return monthly_weather_dict_av

            def average_seasonal_weather_dict(dict_sample):
                """returns average seasonal weather dictionary"""
                monthly_weather_dict = seasonal_weather_dict(dict_sample)
                average_seasonal_weather_dict = {}
                for p_date, precip in monthly_weather_dict.items():
                    if p_date.month in average_seasonal_weather_dict.keys():
                        x = average_seasonal_weather_dict[
                            p_date.month][0] + float(precip)
                        y = average_seasonal_weather_dict[p_date.month][1] + 1
                        average_seasonal_weather_dict[p_date.month] = [x, y]
                    else:
                        average_seasonal_weather_dict[p_date.month] = [
                            float(precip), 1
                        ]

                average_seasonal_weather_dict_final = {}
                for month, list_element in average_seasonal_weather_dict.items(
                ):
                    d = (list_element[0] / list_element[1])
                    average_seasonal_weather_dict_final[month] = d

                return average_seasonal_weather_dict_final

            def vintage_monthly_weather_dict(dict_sample, vintage):
                """returns dict of weather data aggregated by month for vintage year"""
                monthly_weather_dict = seasonal_weather_dict(dict_sample)
                vintage_monthly_weather_dict_final = {}
                for p_date, precip in monthly_weather_dict.items():
                    if p_date.year == vintage:
                        vintage_monthly_weather_dict_final[p_date] = precip
                return vintage_monthly_weather_dict_final

            monthly_weather_dict_p = vintage_monthly_weather_dict(
                weather_dict_p, vintage)
            monthly_weather_dict_v = vintage_monthly_weather_dict(
                weather_dict_v, vintage)
            seasonal_weather_dict_p = average_seasonal_weather_dict(
                weather_dict_p)
            seasonal_weather_dict_v = average_seasonal_weather_dict(
                weather_dict_v)

            # start chart
            x_values, y_values, z_values, sy_values, sz_values = [], [], [], [], []

            for key in monthly_weather_dict_p.keys():
                x_date = key
                #datetime.strptime(key, '%Y-%m-%d')
                if x_date > datetime(1980, 12, 31):
                    x_values.append(key)
                    y_values.append(float(str(monthly_weather_dict_p[key])))
                    z_values.append(monthly_weather_dict_v[x_date])
                    #y_values.append(float(str(monthly_weather_dict_p[key]))-float(str(seasonal_weather_dict_p[key.month])))
                    #z_values.append(monthly_weather_dict_v[x_date]-seasonal_weather_dict_v[x_date.month])
                    sy_values.append(
                        float(str(seasonal_weather_dict_p[key.month])))
                    sz_values.append(seasonal_weather_dict_v[x_date.month])

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Date", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Precip", color='black', fontsize=12)
            plt.plot(x_values, y_values, color=color)
            plt.plot(x_values, sy_values, color=color, linestyle='dashed')
            plt.tick_params(axis='y', labelcolor=color)

            ax2 = plt.twinx(
            )  # instantiate a second axes that shares the same x-axis

            #axis 2
            color = 'tab:red'
            ax2.set_ylabel(
                "Temp", color='black',
                fontsize=12)  # we already handled the x-label with ax1
            ax2.plot(x_values, z_values, color=color)
            ax2.plot(x_values, sz_values, color=color, linestyle='dashed')
            ax2.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Weather Pattern for Vintage:  " +
                      str(vintage),
                      fontsize=14)

            #Show chart
            plt.show()
Example #6
0
        def proceed_with_method():

            weather_dict_raw = Chateau(self.address).weather_dict(category)
            price_dict_raw = Chateau_data(self.address).get_price_data()

            weather_dict = dict_unpacker(weather_dict_raw)
            price_dict = dict_unpacker(price_dict_raw)

            monthly_weather_dict = all_monthly_weather_dict(
                weather_dict, chosen_month)

            # start chart
            x_values, y_values = [], []

            for key in monthly_weather_dict.keys():
                x_date = key
                #datetime.strptime(key, '%Y-%m-%d')
                if x_date > datetime(1970, 12, 31):
                    try:
                        y = int(x_date.year)
                        x_date_eoy = date(y, 12, 31)
                        y_values.append(price_dict[str(x_date_eoy)])
                        x_values.append(float(str(monthly_weather_dict[key])))
                    except KeyError:
                        None
                    else:
                        None

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel(str(category) + " avg daily", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Price", color='black', fontsize=12)
            plt.scatter(x_values, y_values, color=color)
            plt.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Weather Pattern for Month:  " +
                      str(chosen_month),
                      fontsize=14)

            #Show chart
            plt.show()
Example #7
0
        def proceed_with_method():

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            rating_dict_raw = Chateau_rating(self.address).get_rating_data()
            rating_dict = dict_unpacker(rating_dict_raw)

            seasonal_weather_dict_p = seasonal_weather_dict(weather_dict_p)
            seasonal_weather_dict_v = seasonal_weather_dict(weather_dict_v)

            av_seasonal_weather_dict_p = average_seasonal_weather_dict(
                weather_dict_p)
            av_seasonal_weather_dict_v = average_seasonal_weather_dict(
                weather_dict_v)

            x_values_train, y_values_train, n_values_train = [], [], []
            x_values_test, y_values_test, n_values_test = [], [], []

            s_values_train, r_values_train, d_values_train = [], [], []
            s_values_test, r_values_test, d_values_test = [], [], []

            def func_p(x):
                f = -0.57 * x * x + 2.23 * x + 92.78
                return f

            def func_v(x):
                f = -0.29 * x * x + 12.85 * x - 43.96
                return f

            for key, rating in rating_dict.items():
                if key > datetime(1970, 12, 31) and key < datetime(
                        2000, 12, 31) and int(key.year) > 1970:
                    for i in range(6, 7):
                        try:
                            av_v = seasonal_weather_dict_v[eomonth(
                                key.year, i)]
                            av_p = seasonal_weather_dict_p[eomonth(
                                key.year, i)]

                            x_values_train.append([func_v(av_v), func_p(av_p)])
                            y_values_train.append(rating)
                            n_values_train.append(key.year)

                        except Exception:
                            None

                if key >= datetime(2000, 12, 31) and int(key.year) > 1970:
                    for i in range(6, 7):
                        try:
                            av_v = seasonal_weather_dict_v[eomonth(
                                key.year, i)]
                            av_p = seasonal_weather_dict_p[eomonth(
                                key.year, i)]

                            x_values_test.append([func_v(av_v), func_p(av_p)])
                            y_values_test.append(rating)
                            n_values_test.append(key.year)

                        except Exception:
                            None

                if key > datetime(1970, 12, 31) and key < datetime(
                        2000, 12, 31) and int(key.year) > 1970:

                    strike_v = 0
                    strike_p = 0

                    for i in range(4, 10):
                        try:
                            if seasonal_weather_dict_v[eomonth(
                                    key.year,
                                    i)] < av_seasonal_weather_dict_v[i]:

                                if i in range(7, 10):
                                    a = 0.5
                                else:
                                    a = 1

                                strike_v = strike_v + 1

                        except Exception:
                            None

                    for i in range(5, 10):
                        try:
                            if seasonal_weather_dict_p[eomonth(
                                    key.year,
                                    i)] > 1.5 * av_seasonal_weather_dict_p[i]:
                                strike_p = strike_p + 1
                        except Exception:
                            None

                    s_values_train.append(strike_v + strike_p)
                    r_values_train.append(rating)
                    d_values_train.append(key.year)

                if key >= datetime(2000, 12, 31) and int(key.year) > 1970:

                    strike_v = 0
                    strike_p = 0

                    for i in range(4, 10):
                        try:
                            if seasonal_weather_dict_v[eomonth(
                                    key.year,
                                    i)] < av_seasonal_weather_dict_v[i]:

                                if i in range(7, 10):
                                    a = 0.5
                                else:
                                    a = 1

                                strike_v = strike_v + 1

                        except Exception:
                            None

                    for i in range(5, 10):
                        try:
                            if seasonal_weather_dict_p[eomonth(
                                    key.year,
                                    i)] > 1.5 * av_seasonal_weather_dict_p[i]:
                                strike_p = strike_p + 1
                        except Exception:
                            None

                    s_values_test.append(strike_v + strike_p)
                    r_values_test.append(rating)
                    d_values_test.append(key.year)

            j_dict_train = {}
            for i in range(0, len(n_values_train) - 1):
                j_dict_train[n_values_train[i]] = [
                    x_values_train[i], y_values_train[i]
                ]

            j_dict_test = {}
            for i in range(0, len(n_values_test) - 1):
                j_dict_test[n_values_test[i]] = [
                    x_values_test[i], y_values_test[i]
                ]

            s_dict_train = {}
            for i in range(0, len(d_values_train) - 1):
                s_dict_train[d_values_train[i]] = [
                    s_values_train[i], r_values_train[i]
                ]

            s_dict_test = {}
            for i in range(0, len(d_values_test) - 1):
                s_dict_test[d_values_test[i]] = [
                    s_values_test[i], r_values_test[i]
                ]

            train_dict = {}
            for key in j_dict_train.keys():
                if key in s_dict_train.keys():
                    new_list = j_dict_train[key][0]
                    strike = s_dict_train[key][0]
                    new_list.append(int(strike))
                    rating = j_dict_train[key][1]
                    train_dict[key] = [new_list, rating]

            test_dict = {}
            for key in j_dict_test.keys():
                if key in s_dict_test.keys():
                    new_list = j_dict_test[key][0]
                    strike = s_dict_test[key][0]
                    new_list.append(int(strike))
                    rating = j_dict_test[key][1]
                    test_dict[key] = [new_list, rating]

            x_values_train, y_values_train, n_values_train = [], [], []
            x_values_test, y_values_test, n_values_test = [], [], []

            for key in train_dict.keys():
                x_values_train.append(train_dict[key][0])
                y_values_train.append(train_dict[key][1])
                n_values_train.append(key)

            for key in test_dict.keys():
                x_values_test.append(test_dict[key][0])
                y_values_test.append(test_dict[key][1])
                n_values_test.append(key)

            X_values_train = np.array(x_values_train)
            X_values_test = np.array(x_values_test)
            X_values_all = np.array(x_values_train + x_values_test)
            y_values_all = y_values_train + y_values_test
            n_values_all = n_values_train + n_values_test

            #Create linear regression object
            regr = linear_model.LinearRegression()

            #Train the model using the training sets
            regr.fit(X_values_train, y_values_train)

            #Make predictions using the testing set
            y_values_pred = regr.predict(X_values_test)
            y_values_pred_all = regr.predict(X_values_all)

            #The coefficients
            print('Coefficients: \n', regr.coef_)
            #The mean squared error
            print("Mean squared error: %.2f" %
                  mean_squared_error(y_values_test, y_values_pred))
            #Explained variance score: 1 is perfect prediction
            print('R2 score: %.2f' % r2_score(y_values_test, y_values_pred))

            x = y_values_pred_all
            y = y_values_all
            z = np.polyfit(x, y, 1)
            z_formatted = np.ndarray.tolist(z)
            p = np.poly1d(z)
            xp = np.linspace(min(y_values_pred_all), max(y_values_pred_all),
                             100)

            #calculate correlation coefficient
            correl_y = p(x)
            R = np.corrcoef(y_values_all, y_values_pred_all)
            cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
            print("\nCorrelation coefficient: " + str('%0.2f' % cor))

            print("\nSuggested polynomial a*x + b has [a, b]: " +
                  str('%0.2f' % z_formatted[0]) + ", " +
                  str('%0.2f' %
                      z_formatted[1]))  #+ str('%0.2f' % z_formatted[3]))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Rating Estimate (weather fundamentals)", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Rating", color='black', fontsize=12)
            plt.scatter(y_values_pred_all, y_values_all, color=color)
            plt.plot(xp, p(xp), color='red')
            plt.tick_params(axis='y', labelcolor=color)

            for i, txt in enumerate(n_values_all):
                plt.annotate(txt, (y_values_pred_all[i], y_values_all[i]))

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Rating vs Estimate", fontsize=14)

            #Show chart
            plt.show()
Example #8
0
        def proceed_with_method():

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            rating_dict_raw = Chateau_rating(self.address).get_rating_data()
            rating_dict = dict_unpacker(rating_dict_raw)

            seasonal_weather_dict_p = average_seasonal_weather_dict(
                weather_dict_p)
            seasonal_weather_dict_v = average_seasonal_weather_dict(
                weather_dict_v)

            price_dict_raw = Chateau_data(self.address).get_price_data()

            price_dict = dict_unpacker(price_dict_raw)

            x_values, y_values, n_values = [], [], []

            for key, rating in rating_dict.items():
                if key in rating_dict.keys() and key > datetime(
                        1970, 12, 31) and rating > 96:

                    p_values, v_values = [], []

                    for w_date, data in weather_dict_v.items():
                        if w_date < eomonth(
                                key.year, end_month - 1) and w_date > eomonth(
                                    key.year, start_month - 1):
                            v_values.append(float(data))

                    if v_values == []:
                        None
                    else:
                        av = statistics.mean(v_values)
                        x_values.append(av)
                        y_values.append(rating)
                        n_values.append(key.year)

            #calculate best fit line
            x = x_values
            y = y_values
            z = np.polyfit(x, y, 2)
            z_formatted = np.ndarray.tolist(z)
            p = np.poly1d(z)
            xp = np.linspace(min(x_values), max(x_values), 100)

            #calculate correlation coefficient
            correl_y = p(x)
            #A = np.vstack([x, np.ones(len(x))]).T
            #m, c = np.linalg.lstsq(A, correl_y, rcond=None)[0]
            #print(m, c)
            R = np.corrcoef(y, correl_y)
            cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
            print("\n For month:" + str(start_month))
            print("\nCorrelation coefficient: " + str('%0.2f' % cor))

            print("\nSuggested polynomial a*x^2 + bx + c has [a, b, c]: " +
                  str('%0.2f' % z_formatted[0]) + ", " +
                  str('%0.2f' % z_formatted[1]) + ", " +
                  str('%0.2f' %
                      z_formatted[2]))  #+ str('%0.2f' % z_formatted[3]))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Temp", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Rating", color='black', fontsize=12)
            plt.scatter(x_values, y_values, color=color)
            plt.plot(xp, p(xp), color='red')
            plt.tick_params(axis='y', labelcolor=color)

            for i, txt in enumerate(n_values):
                plt.annotate(txt, (x[i], y[i]))

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Rating vs Price", fontsize=14)

            #Show chart
            plt.show()
Example #9
0
        def proceed_with_method():
            dict_forecast_v = Chateau_comb(self.address).used_correl_analysis(
                'v', str(update))
            dict_forecast_p = Chateau_comb(self.address).used_correl_analysis(
                'p', str(update))
            weather_dict_raw_p = Chateau(self.address).weather_dict('p')
            weather_dict_p = dict_unpacker(weather_dict_raw_p)

            weather_dict_raw_v = Chateau(self.address).weather_dict('v')
            weather_dict_v = dict_unpacker(weather_dict_raw_v)

            m_weather_dict_p = seasonal_weather_dict(weather_dict_p)
            m_weather_dict_v = seasonal_weather_dict(weather_dict_v)

            price_predictions = []

            for i in dict_forecast_v.keys():
                y = vintage
                m = i
                required_date = eomonth(y, m)
                x = m_weather_dict_v[required_date]

                a = dict_forecast_v[i][1]
                b = dict_forecast_v[i][2]
                c = dict_forecast_v[i][3]

                p = a * x * x + b * x + c
                price_predictions.append(p)

            for i in dict_forecast_p.keys():
                y = vintage
                m = i
                required_date = eomonth(y, m)
                x = m_weather_dict_p[required_date]

                a = dict_forecast_p[i][1]
                b = dict_forecast_p[i][2]
                c = dict_forecast_p[i][3]

                p = a * x * x + b * x + c
                price_predictions.append(p)

            predictions_av = statistics.mean(price_predictions)

            #apply factor now based on strikes
            #calculate best fit line
            strike_dict = Chateau_comb(
                self.address).vintage_rule_finder_analysis(update)

            x_values, y_values = [], []
            for key in strike_dict.keys():
                y_values.append(strike_dict[key][0])
                x_values.append(strike_dict[key][1])

            x = x_values
            y = y_values
            z = np.polyfit(x, y, 2)
            p = np.poly1d(z)

            vintage_date = eomonth(vintage, 12)
            strikes = strike_dict[str(vintage_date)][1]

            #find the list of top prices

            price_dict_raw = Chateau_data(self.address).get_price_data()
            price_dict = dict_unpacker(price_dict_raw)

            prices, top_prices = [], []
            for px_date, price in price_dict.items():
                if px_date > datetime(1970, 12, 31):
                    prices.append(price)

            av = statistics.mean(prices)
            sd = statistics.stdev(prices)

            for i in range(0, len(prices) - 1):
                if prices[i] > av + 0.5 * sd:
                    top_prices.append(prices[i])

            top_price_mean = statistics.mean(top_prices)

            if strikes == 0:
                adj_prediction = predictions_av

            if strikes > 0:
                adj_prediction = predictions_av * p(strikes) / top_price_mean

            print('%.2f' % adj_prediction)
            return adj_prediction
Example #10
0
        def proceed_with_method():
            criteria_dict_p = Chateau_comb(
                self.address).chateau_profile('vintage_rule_finder_p')
            criteria_dict_v = Chateau_comb(
                self.address).chateau_profile('vintage_rule_finder_v')

            weather_dict_raw_p = Chateau(self.address).weather_dict('p')
            weather_dict_raw_v = Chateau(self.address).weather_dict('v')

            price_dict_raw = Chateau_data(self.address).get_price_data()

            weather_dict_p = dict_unpacker(weather_dict_raw_p)
            weather_dict_v = dict_unpacker(weather_dict_raw_v)

            seasonal_weather_dict_p = seasonal_weather_dict(weather_dict_p)
            seasonal_weather_dict_v = seasonal_weather_dict(weather_dict_v)

            price_dict = dict_unpacker(price_dict_raw)

            strike_dict = {}
            for px_date, price in price_dict.items():
                if px_date > datetime(1970, 12, 31):
                    y = px_date.year
                    strike = 0

                    #exceptions in p
                    for i in range(1, 10):
                        m = i
                        required_day = eomonth(y, m)
                        data = seasonal_weather_dict_p[required_day]
                        av = criteria_dict_p[str(i)][1]
                        sd = criteria_dict_p[str(i)][2]
                        for t in range(0, len(criteria_dict_p[str(i)][0]) - 1):
                            if criteria_dict_p[str(i)][0][t] == 'sig over av':
                                if data > av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_p[str(
                                    i)][0][t] == 'slight over av':
                                if data > av + 1 * sd and data < av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_p[str(i)][0][t] == 'sig under av':
                                if data < av - 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_p[str(
                                    i)][0][t] == 'slight under av':
                                if data < av - 1 * sd and data > av - 2 * sd:
                                    strike = strike + 1

                    #exceptions in v
                    for i in range(1, 13):
                        m = i
                        required_day = eomonth(y, m)
                        data = seasonal_weather_dict_v[required_day]
                        av = criteria_dict_v[str(i)][1]
                        sd = criteria_dict_v[str(i)][2]
                        for t in range(0, len(criteria_dict_v[str(i)][0]) - 1):
                            if criteria_dict_v[str(i)][0][t] == 'sig over av':
                                if data > av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_v[str(
                                    i)][0][t] == 'slight over av':
                                if data > av + 1 * sd and data < av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_v[str(i)][0][t] == 'sig under av':
                                if data < av - 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_v[str(
                                    i)][0][t] == 'slight under av':
                                if data < av - 1 * sd and data > av - 2 * sd:
                                    strike = strike + 1

                    strike_dict[eomonth(px_date.year, 12)] = [price, strike]

            #calculate best fit line
            x_values, y_values = [], []
            for key in strike_dict.keys():
                y_values.append(strike_dict[key][0])
                x_values.append(strike_dict[key][1])

            x = x_values
            y = y_values
            z = np.polyfit(x, y, 2)
            p = np.poly1d(z)
            xp = np.linspace(min(x_values), max(x_values), 100)

            #calculate correlation coefficient
            correl_y = p(x)
            #A = np.vstack([x, np.ones(len(x))]).T
            #m, c = np.linalg.lstsq(A, correl_y, rcond=None)[0]
            #print(m, c)
            R = np.corrcoef(y, correl_y)
            cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
            print("\nCorrelation coefficient: " + str(cor))

            print("\nSuggested polynomial a*x^2 + bx + c has [a, b, c]: " +
                  str(z))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Strikes", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Price", color='black', fontsize=12)
            plt.scatter(x_values, y_values, color=color)
            plt.plot(xp, p(xp), color='red')
            plt.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Strikes vs Price:  ", fontsize=14)

            #Show chart
            plt.show()
Example #11
0
        def proceed_with_method():
            criteria_dict_p = Chateau_comb(
                self.address).chateau_profile('vintage_rule_finder_p')
            criteria_dict_v = Chateau_comb(
                self.address).chateau_profile('vintage_rule_finder_v')

            weather_dict_raw_p = Chateau(self.address).weather_dict('p')
            weather_dict_raw_v = Chateau(self.address).weather_dict('v')

            price_dict_raw = Chateau_data(self.address).get_price_data()

            weather_dict_p = dict_unpacker(weather_dict_raw_p)
            weather_dict_v = dict_unpacker(weather_dict_raw_v)

            seasonal_weather_dict_p = seasonal_weather_dict(weather_dict_p)
            seasonal_weather_dict_v = seasonal_weather_dict(weather_dict_v)

            price_dict = dict_unpacker(price_dict_raw)

            strike_dict = {}
            for px_date, price in price_dict.items():
                if px_date > datetime(1970, 12, 31):
                    y = px_date.year
                    strike = 0

                    #exceptions in p
                    for i in range(1, 10):
                        m = i
                        required_day = eomonth(y, m)
                        data = seasonal_weather_dict_p[required_day]
                        av = criteria_dict_p[str(i)][1]
                        sd = criteria_dict_p[str(i)][2]
                        for t in range(0, len(criteria_dict_p[str(i)][0]) - 1):
                            if criteria_dict_p[str(i)][0][t] == 'sig over av':
                                if data > av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_p[str(
                                    i)][0][t] == 'slight over av':
                                if data > av + 1 * sd and data < av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_p[str(i)][0][t] == 'sig under av':
                                if data < av - 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_p[str(
                                    i)][0][t] == 'slight under av':
                                if data < av - 1 * sd and data > av - 2 * sd:
                                    strike = strike + 1

                    #exceptions in v
                    for i in range(1, 13):
                        m = i
                        required_day = eomonth(y, m)
                        data = seasonal_weather_dict_v[required_day]
                        av = criteria_dict_v[str(i)][1]
                        sd = criteria_dict_v[str(i)][2]
                        for t in range(0, len(criteria_dict_v[str(i)][0]) - 1):
                            if criteria_dict_v[str(i)][0][t] == 'sig over av':
                                if data > av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_v[str(
                                    i)][0][t] == 'slight over av':
                                if data > av + 1 * sd and data < av + 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_v[str(i)][0][t] == 'sig under av':
                                if data < av - 2 * sd:
                                    strike = strike + 1

                            if criteria_dict_v[str(
                                    i)][0][t] == 'slight under av':
                                if data < av - 1 * sd and data > av - 2 * sd:
                                    strike = strike + 1

                    strike_dict[str(eomonth(int(px_date.year),
                                            12))] = [price, strike]

            return strike_dict
Example #12
0
        def proceed_with_method():

            weather_dict_raw = Chateau(self.address).weather_dict(category)
            price_dict_raw = Chateau_data(self.address).get_price_data()

            weather_dict = dict_unpacker(weather_dict_raw)
            price_dict = dict_unpacker(price_dict_raw)

            # monthly_weather_dict = all_monthly_weather_dict(weather_dict, chosen_month)

            #find the list of top prices
            prices, prices_dates, top_prices, lower_prices, top_prices_dates, lower_prices_dates = [], [], [], [], [], []
            for px_date, price in price_dict.items():
                if px_date > datetime(1970, 12, 31):
                    prices.append(price)
                    prices_dates.append(px_date)

            av = statistics.mean(prices)
            sd = statistics.stdev(prices)

            print("\nAverage/Stdev price is: " + str(av) + "/ " + str(sd) +
                  "\n")

            for i in range(0, len(prices) - 1):
                if prices[i] > av + 0.5 * sd:
                    top_prices.append(prices[i])
                    top_prices_dates.append(prices_dates[i])
                elif prices[i] < av - 0 * sd:
                    lower_prices.append(prices[i])
                    lower_prices_dates.append(prices_dates[i])

            poor_criteria_dict = {}

            for i in range(1, 13):
                poor_criteria_dict[i] = []

            weather_stats = all_monthly_weather_dict_detail(weather_dict)
            print(weather_stats)
            specific_weather_dict = seasonal_weather_dict(weather_dict)
            #find poor price criteria
            for lower_price_date in lower_prices_dates:
                y = lower_price_date.year
                for i in range(1, 13):
                    m = i
                    weather_date = eomonth(y, m)
                    weather_stat = specific_weather_dict[weather_date]
                    av = weather_stats[i][0]
                    sd = weather_stats[i][1]

                    if weather_stat > av + 2 * sd:
                        title = "sig over av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

                    elif weather_stat > av + 1 * sd:
                        title = "slight over av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

                    elif weather_stat < av - 2 * sd:
                        title = "slight under av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

                    elif weather_stat < av - 1 * sd:
                        title = "sig under av"
                        if title not in poor_criteria_dict[i]:
                            poor_criteria_dict[i].append(title)

            #find top price criteria

            top_criteria_dict = {}

            for i in range(1, 13):
                top_criteria_dict[i] = []

            for top_price_date in top_prices_dates:
                y = top_price_date.year
                for i in range(1, 13):
                    m = i
                    weather_date = eomonth(y, m)
                    weather_stat = specific_weather_dict[weather_date]
                    av = weather_stats[i][0]
                    sd = weather_stats[i][1]

                    if weather_stat > av + 2 * sd:
                        title = "sig over av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

                    elif weather_stat > av + 1 * sd:
                        title = "slight over av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

                    elif weather_stat < av - 2 * sd:
                        title = "sig under av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

                    elif weather_stat < av - 1 * sd:
                        title = "slight under av"
                        if title not in top_criteria_dict[i]:
                            top_criteria_dict[i].append(title)

            bad_criteria_dict = {}

            for i in range(1, 13):
                bad_criteria_dict[i] = []

            for i in range(1, 13):
                y = [
                    x for x in poor_criteria_dict[i]
                    if x not in top_criteria_dict[i]
                ]
                av = weather_stats[i][0]
                sd = weather_stats[i][1]
                bad_criteria_dict[i] = [y, av, sd]

            good_criteria_dict = {}

            for i in range(1, 13):
                good_criteria_dict[i] = []

            for i in range(1, 13):
                y = [
                    x for x in top_criteria_dict[i]
                    if x not in poor_criteria_dict[i]
                ]
                av = weather_stats[i][0]
                sd = weather_stats[i][1]
                good_criteria_dict[i] = [y, av, sd]

            print("\n" + str(self.address) +
                  " great vintages have had these seasonal " + str(category) +
                  " weather anomalies: " + str(top_criteria_dict))
            print("\n" + str(self.address) +
                  " standard vintages have had these seasonal " +
                  str(category) + " weather anomalies: " +
                  str(poor_criteria_dict))
            print("\n" + str(self.address) +
                  " we will use the following criteria as bad " +
                  str(category) + " vintage indicators: " +
                  str(bad_criteria_dict))
            #print("\n" + str(self.address) + " we will use the following criteria as good vintage indicators: " + str(good_criteria_dict))
            return bad_criteria_dict
Example #13
0
        def proceed_with_method():

            weather_dict_raw = Chateau(self.address).weather_dict(category)
            price_dict_raw = Chateau_data(self.address).get_price_data()

            weather_dict = dict_unpacker(weather_dict_raw)
            price_dict = dict_unpacker(price_dict_raw)

            monthly_weather_dict = all_monthly_weather_dict(
                weather_dict, chosen_month)

            #find the average price
            px_list = []
            for px_date, price in price_dict.items():
                if px_date > datetime(1960, 12, 31):
                    px_list.append(price)

            av = statistics.mean(px_list)
            sd = statistics.stdev(px_list)

            print(str(av) + " is the average price for " + str(self.address))

            #add prices above average to the selected list
            selected_price_dict = {}
            y_values = []
            for px_date, price in price_dict.items():
                if price > av + 0.5 * sd and px_date > datetime(1970, 12, 31):
                    selected_price_dict[px_date] = price
                    y_values.append(price)

            #find weather data for price year
            x_values = []
            for px_date, price in selected_price_dict.items():
                y = px_date.year
                m = chosen_month
                relevant_date = eomonth(y, m)
                addition = monthly_weather_dict[relevant_date]
                x_values.append(addition)

            #calculate best fit line
            x = x_values
            y = y_values
            z = np.polyfit(x, y, 2)
            p = np.poly1d(z)
            xp = np.linspace(min(x_values), max(x_values), 100)

            #calculate correlation coefficient
            correl_y = p(x)
            #A = np.vstack([x, np.ones(len(x))]).T
            #m, c = np.linalg.lstsq(A, correl_y, rcond=None)[0]
            #print(m, c)
            R = np.corrcoef(y, correl_y)
            cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
            print("\nCorrelation coefficient: " + str(cor))

            print("\nSuggested polynomial a*x^2 + bx + c has [a, b, c]: " +
                  str(z))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel(str(category) + " avg daily", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Price", color='black', fontsize=12)
            plt.scatter(x_values, y_values, color=color)
            plt.plot(xp, p(xp), color='red')
            plt.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Weather Pattern for Month:  " +
                      str(chosen_month),
                      fontsize=14)

            #Show chart
            plt.show()
Example #14
0
        def proceed_with_method():

            price_dict = Chateau_data(self.address).get_price_data()
            weather_dict = Chateau(self.address).weather_dict(category)

            #turn weather_dict into annual rainfall
            monthly_weather_dict = {}

            def eomonth(y, m):
                year = y
                if m == 12:
                    month = 1
                else:
                    month = int(m) + 1
                given_day = datetime(year, month, 1)
                required_day = given_day - timedelta(days=1)
                return required_day

            for p_date, precip in weather_dict.items():
                formattedas_date = datetime.strptime(p_date, "%Y-%m-%d")
                y = formattedas_date.year
                m = formattedas_date.month
                needed_day = eomonth(y, m)
                if needed_day in monthly_weather_dict.keys():
                    monthly_weather_dict[needed_day] = monthly_weather_dict[
                        needed_day] + float(precip)
                else:
                    monthly_weather_dict[needed_day] = float(precip)

            monthly_weather_dict_final = {}
            for p_date, precip in monthly_weather_dict.items():
                da = p_date
                if da.month == given_month:
                    monthly_weather_dict_final[da] = precip

            # start chart
            x_values, y_values, z_values = [], [], []

            for key in price_dict.keys():
                x_date = datetime.strptime(key, '%Y-%m-%d')
                if x_date > datetime(1980, 12, 31):
                    x_values.append(key)
                    y_values.append(float(str(price_dict[key])))
                    z_date = eomonth(x_date.year, given_month)
                    z_values.append(annual_weather_dict_final[z_date])

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:red'
            plt.xlabel("Date", fontsize=12)
            plt.ylabel("Price", color='black', fontsize=12)
            plt.bar(x_values, y_values, color=color)
            plt.tick_params(axis='y', labelcolor=color)

            ax2 = plt.twinx(
            )  # instantiate a second axes that shares the same x-axis

            #axis 2
            color = 'tab:blue'
            ax2.set_ylabel(
                "Precip", color='black',
                fontsize=12)  # we already handled the x-label with ax1
            ax2.plot(x_values, z_values, color=color)
            ax2.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title("Weather/Price correlation " + str(given_month),
                      fontsize=14)

            #Show chart
            plt.show()
Example #15
0
        def proceed_with_method():

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            #get the right start date depending on hemisphere
            hemisphere = Chateau_fundamentals(
                self.address).chateau_profile('hemisphere_finder')

            if hemisphere == 'N':
                start_month = 4
            elif hemisphere == 'S':
                start_month = 10
            else:
                print("Hemisphere location error")

            vintage_start_date = eomonth(vintage, start_month - 1)

            #make a list of dates in the vintage
            vintage_dates = []

            #find the budbreak start date
            for w_date, temp in weather_dict_v.items():
                if w_date > vintage_start_date - timedelta(
                        days=5) and w_date < vintage_start_date + timedelta(
                            days=365):
                    vintage_dates.append(w_date)

            bud_break_start_date = vintage_start_date
            bud_break_strike = 0

            for i in range(4,
                           len(vintage_dates) -
                           1):  #need the start date to have a mavg
                mavg_list = []
                for t in (0, 4):
                    mavg_list.append(weather_dict_v[vintage_dates[i - t]])

                fdav = statistics.mean(mavg_list)
                #print(fdav)

                if fdav > 10 and bud_break_start_date == vintage_start_date:
                    bud_break_start_date = vintage_dates[i]

                if fdav < 1 and bud_break_start_date != vintage_start_date and vintage_dates[
                        i] > bud_break_start_date and vintage_dates[
                            i] < bud_break_start_date + timedelta(days=80):
                    bud_break_strike = bud_break_strike + 1

            #find the flowering start date
            flower_start_date = bud_break_start_date
            flower_strike = 0

            for i in range(4,
                           len(vintage_dates) -
                           1):  #need the start date to have a mavg
                mavg_list = []
                for t in (0, 4):
                    mavg_list.append(weather_dict_v[vintage_dates[i - t]])

                fdav = statistics.mean(mavg_list)
                #print(fdav)

                if fdav > 20 and bud_break_start_date == flower_start_date and vintage_dates[
                        i] > bud_break_start_date + timedelta(days=20):
                    flower_start_date = vintage_dates[i]

                if fdav < 15 and bud_break_start_date != flower_start_date and vintage_dates[
                        i] > flower_start_date and vintage_dates[
                            i] < flower_start_date + timedelta(days=42):
                    flower_strike = flower_strike + 1

            fruit_set_start_date = flower_start_date + timedelta(days=30)
            veraison_start_date = fruit_set_start_date + timedelta(days=45)
            #find the harvest start date
            harvest_start_date = veraison_start_date
            for i in range(4,
                           len(vintage_dates) -
                           1):  #need the start date to have a mavg
                mavg_list = []
                for t in (0, 4):
                    mavg_list.append(weather_dict_v[vintage_dates[i - t]])

                fdav = statistics.mean(mavg_list)
                #print(fdav)

                if fdav < 16 and veraison_start_date == harvest_start_date and vintage_dates[
                        i] > veraison_start_date + timedelta(days=10):
                    harvest_start_date = vintage_dates[i]

            names_list = [
                'bud_break', 'flower', 'fruit_set', 'veraison', 'harvest'
            ]

            start_dates_list = [
                bud_break_start_date, flower_start_date, fruit_set_start_date,
                veraison_start_date, harvest_start_date
            ]

            length_list = []

            def days_between(d1, d2):

                return abs((d2 - d1).days)

            for i in range(0, 4):
                d = days_between(start_dates_list[i], start_dates_list[i + 1])
                length_list.append(d)
            length_list.append(0)

            p_list, v_list = [], []

            for i in range(0, 4):
                p_counter = 0
                for p_date, precip in weather_dict_p.items():
                    if p_date >= start_dates_list[
                            i] and p_date < start_dates_list[i + 1]:
                        p_counter = p_counter + precip
                p_list.append(p_counter)
            p_list.append(0)

            for i in range(0, 4):
                v_counter = 0
                total = 0
                for v_date, temp in weather_dict_v.items():
                    if v_date >= start_dates_list[
                            i] and v_date < start_dates_list[i + 1]:
                        v_counter = v_counter + temp
                        total = total + 1
                if total == 0:
                    print("no data for " + str(vintage))
                    return None
                v_av = v_counter / total
                v_list.append(v_av)
            v_list.append(0)

            #print(names_list)
            #print(start_dates_list)
            #print(p_list)
            #print(v_list)

            fundamentals = {}
            for i in range(0, 5):
                fundamentals[names_list[i]] = [
                    str(start_dates_list[i]), length_list[i], p_list[i],
                    v_list[i]
                ]

            return fundamentals
Example #16
0
    def vintage_distance(self, vintage, start_month='3', end_month='8'):
        """returns the weather profile of the vintage"""

        weather_dict_p = Chateau(self.address).weather_dict('p')
        weather_dict_v = Chateau(self.address).weather_dict('v')

        #turn weather_dict into annual rainfall

        def eomonth(y, m):
            year = y
            if m == 12:
                month = 1
            else:
                month = int(m) + 1
            given_day = datetime(year, month, 1)
            required_day = given_day - timedelta(days=1)
            return required_day

        def seasonal_weather_dict(dict_sample):
            """returns dict of average daily weather data aggregated by month"""
            monthly_weather_dict = {}
            for p_date, precip in dict_sample.items():
                formattedas_date = datetime.strptime(p_date, "%Y-%m-%d")
                y = formattedas_date.year
                m = formattedas_date.month
                needed_day = eomonth(y, m)
                if needed_day in monthly_weather_dict.keys():
                    x = monthly_weather_dict[needed_day][0] + float(precip)
                    y = monthly_weather_dict[needed_day][1] + 1
                    monthly_weather_dict[needed_day] = [x, y]
                else:
                    monthly_weather_dict[needed_day] = [float(precip), 1]

            monthly_weather_dict_av = {}
            for needed_day in monthly_weather_dict.keys():
                d = monthly_weather_dict[needed_day][0] / monthly_weather_dict[
                    needed_day][1]
                monthly_weather_dict_av[needed_day] = d
            return monthly_weather_dict_av

        def average_seasonal_weather_dict(dict_sample):
            """returns average seasonal weather dictionary"""
            monthly_weather_dict = seasonal_weather_dict(dict_sample)
            average_seasonal_weather_dict = {}
            for p_date, precip in monthly_weather_dict.items():
                if p_date.month in average_seasonal_weather_dict.keys():
                    x = average_seasonal_weather_dict[p_date.month][0] + float(
                        precip)
                    y = average_seasonal_weather_dict[p_date.month][1] + 1
                    average_seasonal_weather_dict[p_date.month] = [x, y]
                else:
                    average_seasonal_weather_dict[p_date.month] = [
                        float(precip), 1
                    ]

            average_seasonal_weather_dict_final = {}
            for month, list_element in average_seasonal_weather_dict.items():
                d = (list_element[0] / list_element[1])
                average_seasonal_weather_dict_final[month] = d

            return average_seasonal_weather_dict_final

        def vintage_monthly_weather_dict(dict_sample, vintage):
            """returns dict of weather data aggregated by month for vintage year"""
            monthly_weather_dict = seasonal_weather_dict(dict_sample)
            vintage_monthly_weather_dict_final = {}
            for p_date, precip in monthly_weather_dict.items():
                if p_date.year == vintage:
                    vintage_monthly_weather_dict_final[p_date] = precip
            return vintage_monthly_weather_dict_final

        monthly_weather_dict_p = vintage_monthly_weather_dict(
            weather_dict_p, vintage)
        monthly_weather_dict_v = vintage_monthly_weather_dict(
            weather_dict_v, vintage)
        seasonal_weather_dict_p = average_seasonal_weather_dict(weather_dict_p)
        seasonal_weather_dict_v = average_seasonal_weather_dict(weather_dict_v)

        # start chart
        x_values, y_values, z_values, sy_values, sz_values = [], [], [], [], []

        for key in monthly_weather_dict_p.keys():
            x_date = key
            #datetime.strptime(key, '%Y-%m-%d')
            if x_date > datetime(1980, 12, 31):
                x_values.append(key)
                y_values.append(float(str(monthly_weather_dict_p[key])))
                z_values.append(monthly_weather_dict_v[x_date])
                sy_values.append(float(str(
                    seasonal_weather_dict_p[key.month])))
                sz_values.append(seasonal_weather_dict_v[x_date.month])

        msq2 = 0

        for i in range(int(start_month) - 1, int(end_month) - 1):
            pd = y_values[i] - sy_values[i]
            vd = z_values[i] - sz_values[i]
            add = pd * pd * 0 + vd * vd
            msq2 = msq2 + add

        msq = math.sqrt(msq2)
        #print(vintage)
        #print(msq)

        return msq
Example #17
0
        def proceed_with_method():

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            rating_dict_raw = Chateau_rating(self.address).get_rating_data()
            rating_dict = dict_unpacker(rating_dict_raw)

            seasonal_weather_dict_p = average_seasonal_weather_dict(
                weather_dict_p)
            seasonal_weather_dict_v = average_seasonal_weather_dict(
                weather_dict_v)

            x_values_train, x_values_test = [], []
            y_values_train, y_values_test = [], []
            t_values_train, t_values_test = [], []

            for r_date, rating in rating_dict.items():
                if r_date > datetime(1970, 12, 31) and r_date <= datetime(
                        2008, 12, 31):
                    vintage = r_date.year

                    monthly_weather_dict_p = vintage_monthly_weather_dict(
                        weather_dict_p, vintage)
                    monthly_weather_dict_v = vintage_monthly_weather_dict(
                        weather_dict_v, vintage)

                    # start chart
                    x_values, y_values, z_values = [], [], []
                    #sy_values, sz_values = [], []

                    for key in monthly_weather_dict_p.keys():
                        x_date = key
                        #y_values.append(float(str(monthly_weather_dict_p[key])))
                        #z_values.append(monthly_weather_dict_v[x_date])
                        y_values.append(
                            float(str(monthly_weather_dict_p[key])) -
                            float(str(seasonal_weather_dict_p[key.month])))
                        z_values.append(monthly_weather_dict_v[x_date] -
                                        seasonal_weather_dict_v[x_date.month])
                        #sy_values.append(float(str(seasonal_weather_dict_p[key.month])))
                        #sz_values.append(seasonal_weather_dict_v[x_date.month])
                    t_values_train.append(r_date)
                    y_values_train.append(rating)
                    x_values_train.append(y_values + z_values)
                    #z_values_train.append(z_values)

                if r_date > datetime(2008,
                                     12, 31) and r_date in weather_dict_p.keys(
                                     ) and r_date in weather_dict_v.keys():
                    vintage = r_date.year

                    monthly_weather_dict_p = vintage_monthly_weather_dict(
                        weather_dict_p, vintage)
                    monthly_weather_dict_v = vintage_monthly_weather_dict(
                        weather_dict_v, vintage)

                    # start chart
                    x_values, y_values, z_values = [], [], []
                    #sy_values, sz_values = [], []

                    for key in monthly_weather_dict_p.keys():
                        x_date = key
                        #y_values.append(float(str(monthly_weather_dict_p[key])))
                        #z_values.append(monthly_weather_dict_v[x_date])
                        y_values.append(
                            float(str(monthly_weather_dict_p[key])) -
                            float(str(seasonal_weather_dict_p[key.month])))
                        z_values.append(monthly_weather_dict_v[x_date] -
                                        seasonal_weather_dict_v[x_date.month])
                        #sy_values.append(float(str(seasonal_weather_dict_p[key.month])))
                        #sz_values.append(seasonal_weather_dict_v[x_date.month])

                    t_values_test.append(r_date)
                    y_values_test.append(rating)
                    x_values_test.append(y_values + z_values)

            print(t_values_train + t_values_test)

            # Create linear regression object
            regr = linear_model.LinearRegression()

            # Train the model using the training sets
            regr.fit(x_values_train, y_values_train)

            # Make predictions using the testing set
            y_values_pred = regr.predict(x_values_test)
            correl_y = regr.predict(x_values_train + x_values_test)

            # The coefficients
            print('Coefficients: \n', regr.coef_)
            # The mean squared error
            print("Mean squared error: %.2f" %
                  mean_squared_error(y_values_test, y_values_pred))
            # Explained variance score: 1 is perfect prediction
            print('R2 score: %.2f' % r2_score(y_values_test, y_values_pred))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Date", fontsize=12)
            plt.ylabel("Price", color='black', fontsize=12)
            plt.bar(
                t_values_train + t_values_test,
                y_values_train + y_values_test,
                facecolor='none',
                edgecolor='blue',
            )
            plt.scatter(t_values_train + t_values_test,
                        correl_y,
                        color='red',
                        linestyle='dashed')
            plt.tick_params(axis='y', labelcolor=color)
            plt.ylim((70, 110))

            #ax2 = plt.twinx()  # instantiate a second axes that shares the same x-axis

            #axis 2
            #color = 'tab:red'
            #ax2.set_ylabel("Price", color='black', fontsize=12)  # we already handled the x-label with ax1
            #ax2.plot(x_values, y_values, color=color)
            #ax2.scatter(x_values, z_values, color=color, linestyle='dashed')
            #ax2.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " actual px vs forecast",
                      fontsize=14)

            #Show chart
            plt.show()
Example #18
0
        def proceed_with_method():

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            rating_dict_raw = Chateau_rating(self.address).get_rating_data()
            rating_dict = dict_unpacker(rating_dict_raw)

            seasonal_weather_dict_p = seasonal_weather_dict(weather_dict_p)
            seasonal_weather_dict_v = seasonal_weather_dict(weather_dict_v)

            av_seasonal_weather_dict_p = average_seasonal_weather_dict(
                weather_dict_p)
            av_seasonal_weather_dict_v = average_seasonal_weather_dict(
                weather_dict_v)

            x_values, y_values, n_values = [], [], []

            for key, rating in rating_dict.items():
                if key > datetime(1970, 12, 31) and int(key.year) > 1970:

                    strike_v = 0
                    strike_p = 0

                    for i in range(4, 10):
                        try:
                            if seasonal_weather_dict_v[eomonth(
                                    key.year,
                                    i)] < av_seasonal_weather_dict_v[i]:

                                if i in range(7, 10):
                                    a = 0.5
                                else:
                                    a = 1

                                strike_v = strike_v + (
                                    av_seasonal_weather_dict_v[i] -
                                    seasonal_weather_dict_v[eomonth(
                                        key.year, i)])

                        except Exception:
                            None

                    for i in range(5, 10):
                        try:
                            if seasonal_weather_dict_p[eomonth(
                                    key.year,
                                    i)] > 1.5 * av_seasonal_weather_dict_p[i]:
                                strike_p = strike_p + (
                                    seasonal_weather_dict_p[eomonth(
                                        key.year, i)] -
                                    av_seasonal_weather_dict_p[i])
                        except Exception:
                            None

                x_values.append(strike_v + strike_p)
                y_values.append(rating)
                n_values.append(key.year)

            #calculate best fit line
            x = x_values
            y = y_values
            z = np.polyfit(x, y, 1)
            z_formatted = np.ndarray.tolist(z)
            p = np.poly1d(z)
            xp = np.linspace(min(x_values), max(x_values), 100)

            #calculate correlation coefficient
            correl_y = p(x)
            #A = np.vstack([x, np.ones(len(x))]).T
            #m, c = np.linalg.lstsq(A, correl_y, rcond=None)[0]
            #print(m, c)
            R = np.corrcoef(y, correl_y)
            cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
            print("\nCorrelation coefficient: " + str('%0.2f' % cor))

            print("\nSuggested polynomial a*x + b has [a, b]: " +
                  str('%0.2f' % z_formatted[0]) + ", " +
                  str('%0.2f' %
                      z_formatted[1]))  #+ str('%0.2f' % z_formatted[3]))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Temp", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Rating", color='black', fontsize=12)
            plt.scatter(x_values, y_values, color=color)
            plt.plot(xp, p(xp), color='red')
            plt.tick_params(axis='y', labelcolor=color)

            for i, txt in enumerate(n_values):
                plt.annotate(txt, (x[i], y[i]))

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Rating vs Price", fontsize=14)

            #Show chart
            plt.show()
Example #19
0
        def proceed_with_method():

            price_dict_raw = Chateau_data(self.address).get_price_data()
            price_dict = dict_unpacker(price_dict_raw)

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            # start chart
            x_values, y_values, z_values = [], [], []
            p_values_train, p_values_test = [], []
            x_values_train, x_values_test = [], []
            y_values_train, y_values_test = [], []
            z_values_train, z_values_test = [], []

            for p_date, price in price_dict.items():

                vintage = p_date.year
                monthly_weather_dict_p = vintage_monthly_weather_dict(
                    weather_dict_p, vintage)
                monthly_weather_dict_v = vintage_monthly_weather_dict(
                    weather_dict_v, vintage)

                if p_date > datetime(1970, 12, 31) and p_date <= datetime(
                        2000, 12, 31):
                    p_values_train.append(price)

                    x_vector_train = []
                    y_vector_train = []
                    z_vector_train = []

                    for key in monthly_weather_dict_p.keys():
                        x_date = key
                        x_vector_train.append(key)
                        y_vector_train.append(
                            float(str(monthly_weather_dict_p[key])))
                        y_vector_train.append(monthly_weather_dict_v[x_date])

                    x_values_train.append(x_vector_train)
                    y_values_train.append(y_vector_train)
                    z_values_train.append(z_vector_train)

                if p_date > datetime(2000, 12, 31):
                    p_values_test.append(price)

                    x_vector_test = []
                    y_vector_test = []
                    z_vector_test = []

                    for key in monthly_weather_dict_p.keys():
                        x_date = key
                        x_vector_test.append(key)
                        y_vector_test.append(
                            float(str(monthly_weather_dict_p[key])))
                        z_vector_test.append(monthly_weather_dict_v[x_date])

                    x_values_test.append(x_vector_test)
                    y_values_test.append(y_vector_test)
                    y_values_test.append(z_vector_test)

            x_train = y_values_train
            x_test = y_values_test
            print(x_train)
            print(p_values_train)

            scaler = StandardScaler()
            # Don't cheat - fit only on training data
            scaler.fit(x_train)
            X_train = scaler.transform(x_train)
            # apply same transformation to test data
            X_test = scaler.transform(x_test)

            X = X_train
            y = p_values_train
            clf = MLPRegressor(solver='lbfgs',
                               alpha=1e-5,
                               hidden_layer_sizes=(5, 2),
                               random_state=1)

            clf.fit(X, y)
            MLPRegressor(activation='relu',
                         alpha=1e-05,
                         batch_size='auto',
                         beta_1=0.9,
                         beta_2=0.999,
                         early_stopping=False,
                         epsilon=1e-08,
                         hidden_layer_sizes=(5, 2),
                         learning_rate='constant',
                         learning_rate_init=0.001,
                         max_iter=200,
                         momentum=0.9,
                         n_iter_no_change=10,
                         nesterovs_momentum=True,
                         power_t=0.5,
                         random_state=1,
                         shuffle=True,
                         solver='lbfgs',
                         tol=0.0001,
                         validation_fraction=0.1,
                         verbose=False,
                         warm_start=False)

            results = clf.predict(X_test)
            print(results)
            #print(str(results).replace(' ',', ').replace(' ,',''))

            y_values_pred_raw = str(results).replace(' ',
                                                     ', ').replace(' ,', '')

            y_values_pred = ast.literal_eval(y_values_pred_raw)
            #for i in range(0,len(y_values_pred_raw)-1):
            #y_values_pred.append(float(y_values_pred_raw[i]))

            #print(y_values_pred)

            print([coef.shape for coef in clf.coefs_])

            #print(clf.predict_proba(z_values_test))

            # Create linear regression object
            ##regr = linear_model.LinearRegression()

            # Train the model using the training sets
            #regr.fit(z_values_train, y_values_train)

            # Make predictions using the testing set
            #y_values_pred = regr.predict(z_values_test)

            # The coefficients
            #print('Coefficients: \n', regr.coef_)
            # The mean squared error
            #print("Mean squared error: %.2f"
            #     % mean_squared_error(p_values_test, y_values_pred))
            #Explained variance score: 1 is perfect prediction
            #print('R2 score: %.2f' % r2_score(p_values_test, y_values_pred))
            # print('R2 score: %.2f' % r2_score(y_values_test, y_values_pred))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Date", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Price", color='black', fontsize=12)
            plt.bar(
                x_values_train + x_values_test,
                p_values_train + p_values_test,
                facecolor='none',
                edgecolor='blue',
            )
            plt.scatter(x_values_train + x_values_test,
                        y_values_pred,
                        color='red',
                        linestyle='dashed')
            plt.tick_params(axis='y', labelcolor=color)

            #ax2 = plt.twinx()  # instantiate a second axes that shares the same x-axis

            #axis 2
            #color = 'tab:red'
            #ax2.set_ylabel("Price", color='black', fontsize=12)  # we already handled the x-label with ax1
            #ax2.plot(x_values, y_values, color=color)
            #ax2.scatter(x_values, z_values, color=color, linestyle='dashed')
            #ax2.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " actual px vs forecast",
                      fontsize=14)

            #Show chart
            plt.show()
Example #20
0
        def proceed_with_method():

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            rating_dict_raw = Chateau_rating(self.address).get_rating_data()
            rating_dict = dict_unpacker(rating_dict_raw)

            seasonal_weather_dict_p = seasonal_weather_dict(weather_dict_p)
            seasonal_weather_dict_v = seasonal_weather_dict(weather_dict_v)

            av_seasonal_weather_dict_p = average_seasonal_weather_dict(
                weather_dict_p)
            av_seasonal_weather_dict_v = average_seasonal_weather_dict(
                weather_dict_v)

            x_values_train, y_values_train, n_values_train = [], [], []
            x_values_test, y_values_test, n_values_test = [], [], []

            s_values_train, r_values_train, d_values_train = [], [], []
            s_values_test, r_values_test, d_values_test = [], [], []

            def func_p(x):
                func_list = []
                for i in range(0, 10):

                    if i in [12]:  #[2, 7, 9]
                        if i == 2:
                            f = 0.02 * x * x + -0.47 * x + 99.08
                        if i == 7:
                            f = -1.17 * x * x + 2.69 * x + 96.88
                        if i == 9:
                            f = -0.28 * x * x + 0.46 * x + 98.08

                    else:
                        f = 0

                    func_list.append(f)

                return func_list

            def func_v(x):
                func_list = []
                for i in range(0, 10):

                    if i in [4, 5]:  #[3,4,5,6,8]

                        if i == 3:
                            f = -1.17 * x * x + 27.42 * x + -38.69
                        if i == 4:
                            f = -0.29 * x * x + 8.03 * x + 42.72
                        if i == 5:
                            f = -0.24 * x * x + 8.05 * x + 31.77
                        if i == 6:
                            f = -0.21 * x * x + 8.90 * x + 3.81
                        if i == 8:
                            f = -0.22 * x * x + 9.64 * x - 7.21
                    else:
                        f = 0

                    func_list.append(f)

                return func_list

            for key, rating in rating_dict.items():
                if key > datetime(1970, 12, 31) and key < datetime(
                        2000, 12, 31) and int(key.year) > 1970 and rating > 96:
                    x_list = []
                    for i in range(2, 10):
                        try:
                            av_v = seasonal_weather_dict_v[eomonth(
                                key.year, i)]
                            av_p = seasonal_weather_dict_p[eomonth(
                                key.year, i)]

                            v_adj = func_v(av_v)
                            p_adj = func_p(av_p)

                            v_used = v_adj[i]
                            p_used = p_adj[i]

                            if v_used != 0:
                                x_list.append(v_used)
                            if p_used != 0:
                                x_list.append(p_used)

                        except Exception:
                            None

                    if x_list != []:
                        x_values_train.append(x_list)
                        y_values_train.append(rating)
                        n_values_train.append(key.year)

                if key >= datetime(2000, 12, 31) and int(
                        key.year) > 1970 and rating > 96:
                    x_list = []
                    for i in range(2, 10):
                        try:
                            av_v = seasonal_weather_dict_v[eomonth(
                                key.year, i)]
                            av_p = seasonal_weather_dict_p[eomonth(
                                key.year, i)]

                            v_adj = func_v(av_v)
                            p_adj = func_p(av_p)

                            v_used = v_adj[i]
                            p_used = p_adj[i]

                            if v_used != 0:
                                x_list.append(v_used)
                            if p_used != 0:
                                x_list.append(p_used)

                        except Exception:
                            None

                    if x_list != []:
                        x_values_test.append(x_list)
                        y_values_test.append(rating)
                        n_values_test.append(key.year)

            X_values_train = np.array(x_values_train)
            X_values_test = np.array(x_values_test)
            X_values_all = np.array(x_values_train + x_values_test)
            y_values_all = y_values_train + y_values_test
            n_values_all = n_values_train + n_values_test

            #Create linear regression object
            regr = linear_model.LinearRegression()

            #Train the model using the training sets
            regr.fit(X_values_train, y_values_train)

            #Make predictions using the testing set
            y_values_pred = regr.predict(X_values_test)
            y_values_pred_all = regr.predict(X_values_all)

            #The coefficients
            print('Coefficients: \n', regr.coef_)
            #The mean squared error
            print("Mean squared error: %.2f" %
                  mean_squared_error(y_values_test, y_values_pred))
            #Explained variance score: 1 is perfect prediction
            print('R2 score: %.2f' % r2_score(y_values_test, y_values_pred))

            x = y_values_pred_all
            y = y_values_all
            z = np.polyfit(x, y, 1)
            z_formatted = np.ndarray.tolist(z)
            p = np.poly1d(z)
            xp = np.linspace(min(y_values_pred_all), max(y_values_pred_all),
                             100)

            #calculate correlation coefficient
            correl_y = p(x)
            R = np.corrcoef(y_values_all, y_values_pred_all)
            cor = R.item(1)  #R is a 2x2 matrix so take the correct entry
            print("\nCorrelation coefficient: " + str('%0.2f' % cor))

            print("\nSuggested polynomial a*x + b has [a, b]: " +
                  str('%0.2f' % z_formatted[0]) + ", " +
                  str('%0.2f' %
                      z_formatted[1]))  #+ str('%0.2f' % z_formatted[3]))

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Rating Estimate (weather fundamentals)", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Rating", color='black', fontsize=12)
            plt.scatter(y_values_pred_all, y_values_all, color=color)
            plt.plot(xp, p(xp), color='red')
            plt.tick_params(axis='y', labelcolor=color)

            for i, txt in enumerate(n_values_all):
                plt.annotate(txt, (y_values_pred_all[i], y_values_all[i]))

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Rating vs Estimate", fontsize=14)

            #Show chart
            plt.show()
Example #21
0
        def proceed_with_method():

            weather_dict_p_raw = Chateau(self.address).weather_dict('p')
            weather_dict_v_raw = Chateau(self.address).weather_dict('v')

            weather_dict_p = dict_unpacker(weather_dict_p_raw)
            weather_dict_v = dict_unpacker(weather_dict_v_raw)

            monthly_weather_dict_p = vintage_monthly_weather_dict(
                weather_dict_p, vintage)
            monthly_weather_dict_v = vintage_monthly_weather_dict(
                weather_dict_v, vintage)
            seasonal_weather_dict_p = average_seasonal_weather_dict(
                weather_dict_p)
            seasonal_weather_dict_v = average_seasonal_weather_dict(
                weather_dict_v)

            # start chart
            x_values, y_values, z_values, sy_values, sz_values = [], [], [], [], []

            for key in monthly_weather_dict_p.keys():
                x_date = key
                #datetime.strptime(key, '%Y-%m-%d')
                if x_date > datetime(1970, 12, 31):
                    x_values.append(key)
                    y_values.append(float(str(monthly_weather_dict_p[key])))
                    z_values.append(monthly_weather_dict_v[x_date])
                    #y_values.append(float(str(monthly_weather_dict_p[key]))-float(str(seasonal_weather_dict_p[key.month])))
                    #z_values.append(monthly_weather_dict_v[x_date]-seasonal_weather_dict_v[x_date.month])
                    sy_values.append(
                        float(str(seasonal_weather_dict_p[key.month])))
                    sz_values.append(seasonal_weather_dict_v[x_date.month])

            #Size the output
            fig = plt.figure(dpi=128, figsize=(10, 6))

            #Chart gridlines
            plt.grid(None, 'major', 'both')

            #Axis tick formats
            for tick in plt.gca().get_xticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)
                tick.set_rotation('vertical')
            for tick in plt.gca().get_yticklabels():
                tick.set_fontname("Calibri")
                tick.set_fontsize(12)

            #Axis labels and formats

            # axis 1
            color = 'tab:blue'
            plt.xlabel("Date", fontsize=12)
            #plt.xticks(np.arange(x_values[11], x_values[0], 2))
            plt.ylabel("Precip", color='black', fontsize=12)
            plt.plot(x_values, y_values, color=color)
            plt.plot(x_values, sy_values, color=color, linestyle='dashed')
            plt.tick_params(axis='y', labelcolor=color)

            ax2 = plt.twinx(
            )  # instantiate a second axes that shares the same x-axis

            #axis 2
            color = 'tab:red'
            ax2.set_ylabel(
                "Temp", color='black',
                fontsize=12)  # we already handled the x-label with ax1
            ax2.plot(x_values, z_values, color=color)
            ax2.plot(x_values, sz_values, color=color, linestyle='dashed')
            ax2.tick_params(axis='y', labelcolor=color)

            #remove borders
            plt.gca().spines['top'].set_visible(False)

            #Chart title
            plt.title(str(self.address) + " Weather Pattern for Vintage:  " +
                      str(vintage),
                      fontsize=14)

            #Show chart
            plt.show()