# White Christmas in Python Homework Sample

I’m dreaming of a white Christmas, doooh doooa daaadaaa. So what’s the chance of it being a white Christmas this year? We have 2 requirements for a white Christmas, this first is that the temperature must be below freezing, and there must be precipitation. We have provided 50 years of temperature and precipitation records for a location, and want to determine the probability of a white Christmas. Perform an exploratory data analysis for temperature and precipitation and analyze the mean, minimum and maximum temperatures and also the precipitation distribution. Using Bayes, how likely is it that we will have a white Christmas this year. For more Python programming assignments contact us for details.

Solution:

import csv
import numpy as np
import scipy.stats as stats
Import matplotlib. pilot as plot
import matplotlib.pylab as pylab

“””Reads data from csv file path and returns data and numpy array.”””
data = []
with open(filePath, ‘r’) as fp:
fp, fieldnames=[‘YearDay’, ‘SnowDay’, ‘Precip’, ‘TMin’])
if row[‘YearDay’] == ‘YearDay’ and \
row[‘SnowDay’] == ‘SnowDay’ and \
row[‘Precip’] == ‘Precip’ and \
row[‘TMin’] == ‘TMin’:
continue
data.append({
‘YearDay’: row[‘YearDay’],
‘SnowDay’: int(row[‘SnowDay’]),
‘Precip’: float(row[‘Precip’]),
‘TMin’: float(row[‘TMin’]),
})
array = np.zeros([len(data), 3])
# generate np array
cnt = 0
for row in data:
array[cnt][0] = row[‘SnowDay’]
array[cnt][1] = row[‘Precip’]
array[cnt][2] = row[‘TMin’]
cnt = cnt + 1
return data, array

def show_stats(npdata):
“””Show a analytical stats of the given data.

args:
npdata: numpy array object with data
“””
st = stats.describe(npdata)
print(“—————————————————“)
print(”)
print(‘Total {} samples found.’.format(npdata.shape[0]))
print(‘Min Precip: {}’.format(st.minmax[0][1]))
print(‘Max Precip: {}’.format(st.minmax[1][1]))
print(‘Mean Precip: {}’.format(st.mean[1]))
print(‘Vaiance of Precip: {}’.format(st.variance[1]))
print(‘Skewness of Precip: {}’.format(st.skewness[1]))
print(‘Kurtosis of Precip: {}’.format(st.kurtosis[1]))
print(‘Min TMin: {}’.format(st.minmax[0][2]))
print(‘Max TMin: {}’.format(st.minmax[1][2]))
print(‘Mean TMin: {}’.format(st.mean[2]))
print(‘Vaiance of TMin: {}’.format(st.variance[2]))
print(‘Skewness of TMin: {}’.format(st.skewness[2]))
print(‘Kurtosis of TMin: {}’.format(st.kurtosis[2]))
print(”)
print(“—————————————————“)

def show_histogram(npdata):
“””Shows histogram of Percip and TMin from the given np data.”””
plot.grid(True)
plot.xlabel(‘Precipitation’)
plot.ylabel(‘Samples’)
plot.title(‘Histogram of Precipitation’)
pylab.hist(npdata[:, 1])
plot.show()
plot.grid(True)
plot.xlabel(‘Minimum Temparature’)
plot.ylabel(‘Samples’)
plot.title(‘Histogram of Minimum Temparature’)
pylab.hist(npdata[:, 2])
plot.show()

def show_for_white_christmas(npdata):
“””Show a statistical analysis of Precip and TMin for only white Christmas.”””
tdata = npdata[npdata[:, 0] == 1]
print(“—————————————————“)
print(‘Number of White Christmas: {}’.format(tdata.shape[0]))
print(”)
st = stats.describe(tdata)
print(‘Min Precip: {}’.format(st.minmax[0][1]))
print(‘Max Precip: {}’.format(st.minmax[1][1]))
print(‘Mean Precip: {}’.format(st.mean[1]))
print(‘Vaiance of Precip: {}’.format(st.variance[1]))
print(‘Skewness of Precip: {}’.format(st.skewness[1]))
print(‘Kurtosis of Precip: {}’.format(st.kurtosis[1]))
print(‘Min TMin: {}’.format(st.minmax[0][2]))
print(‘Max TMin: {}’.format(st.minmax[1][2]))
print(‘Mean TMin: {}’.format(st.mean[2]))
print(‘Vaiance of TMin: {}’.format(st.variance[2]))
print(‘Skewness of TMin: {}’.format(st.skewness[2]))
print(‘Kurtosis of TMin: {}’.format(st.kurtosis[2]))
print(”)
print(“—————————————————“)

def show_probability_of_precipitation(npdata):
“””Show frequentist probability of precipitation from the given data.”””
no_of_sample = npdata.shape[0]
ta = npdata[npdata[:, 1] >= 20.0]
td = ta[ta[:, 1] <= 70.0]
print(“—————————————————“)
print(”)
print(‘Frequentist Probability of Precipitation to be between 20.0 to 70.0’)
print(‘Number of samples: {}’.format(no_of_sample))
print(‘Occurance Count: {}’.format(td.shape[0]))
print(‘Frequentist Probability of Precipitaion for being between 20.0 to 70.0: {}%’.format(
(float(td.shape[0])/float(no_of_sample)) * 100.0 )
)
print(”)
print(“—————————————————“)

def probability_of_freezing_boundary_layer_at_white_christmas(npdata):
“””Finds the conditional probability of freezing boundary layer at white christmas.”””
no_wc = npdata[npdata[:, 0] == 1].shape[0]
no_samples = float(npdata.shape[0])
p_wc = float(no_wc)/no_samples
no_fb = npdata[npdata[:, 2] < 0.0].shape[0]
p_fb = float(no_fb)/no_samples
tarr = npdata[npdata[:, 2] < 0.0]
no_wc_bar_fb = tarr[tarr[:, 0] == 1].shape[0]
p_wc_bar_fb = float(no_wc_bar_fb)/float(tarr.shape[0])
p_fb_bar_wc = (p_wc_bar_fb * p_fb)/p_wc
print(“—————————————————“)
print(”)
print(‘Probability of Freezing Boundary (TMin < 0) at White Christmas’)
print(‘Probability of Freezing Boundary (TMin < 0): {}’.format(p_fb))
print(‘Probability of White Christmas: {}’.format(p_wc))
print(‘Probability of White Chirstmas on a Day having TMin < 0: {}’.format(p_wc_bar_fb))
print(”)
print(‘Probability of Freezing Boundary (TMin < 0) at White Christmas: {}’.format(p_fb_bar_wc))
print(”)
print(“—————————————————“)

def probability_of_freezing_boundary_layer_at_precipitation(npdata):
“””Shows the probability of freezing boundary layer when precipitaioin(>0) is known to have occured.”””
no_samples = float(npdata.shape[0])
no_fb = npdata[npdata[:, 2] < 0.0].shape[0]
p_fb = float(no_fb)/no_samples
no_pre = npdata[npdata[:, 1] > 0].shape[0]
p_pre = float(no_pre)/no_samples
tarr = npdata[npdata[:, 2] < 0.0]
no_pre_bar_fb = tarr[tarr[:, 1] > 0].shape[0]
p_pre_bar_fb = float(no_pre_bar_fb)/float(tarr.shape[0])
p_fb_bar_pre = (p_pre_bar_fb * p_fb)/p_pre
tarr2 = npdata[npdata[:, 1] > 0]
no_fb_at_pre = tarr2[tarr2[:, 2] < 0.0].shape[0]
# print(“—————————————————“)
# print(”)
# print(‘Conditional Probability of Freezing Boundary (TMin < 0) at Precipitation (>0): {}’.format(
# float(no_fb_at_pre)/float(no_pre)
# ))
# print(”)
# print(“—————————————————“)
print(“—————————————————“)
print(”)
print(‘Conditional(Using Bayes Theorem) Probability of Freezing Boundary (TMin < 0) at Precipitation’)
print(‘Probability of Freezing Boundary (TMin < 0): {}’.format(p_fb))
print(‘Probability of Precipitation (>0): {}’.format(p_pre))
print(‘Probability of Precipitation (>0) on a Day having TMin < 0: {}’.format(p_pre_bar_fb))
print(”)
print(‘Probability of Freezing Boundary (TMin < 0) at Precipitation (>0): {}’.format(p_fb_bar_pre))
print(”)
print(“—————————————————“)

def probability_of_precipitation__at_freezing_boundary_layer(npdata):
“””Shows the probability of precipitaioin(>0) when freezing boundary layer is known to have occured.”””
no_samples = float(npdata.shape[0])
no_fb = npdata[npdata[:, 2] < 0.0].shape[0]
p_fb = float(no_fb)/no_samples
no_pre = npdata[npdata[:, 1] > 0].shape[0]
p_pre = float(no_pre)/no_samples
tarr = npdata[npdata[:, 1] > 0.0]
no_fb_bar_pre = tarr[tarr[:, 2] < 0.0].shape[0]
p_fb_bar_pre = float(no_fb_bar_pre)/float(tarr.shape[0])
p_pre_bar_fb = (p_fb_bar_pre * p_pre)/p_fb
print(“—————————————————“)
print(”)
print(‘Conditional(Using Bayes Theorem) Probability of Precipitation at Freezing Boundary (TMin < 0)’)
print(‘Probability of Freezing Boundary (TMin < 0): {}’.format(p_fb))
print(‘Probability of Precipitation (>0): {}’.format(p_pre))
print(‘Probability of Freezing Boundary (TMin < 0) at Precipitation (>0): {}’.format(p_fb_bar_pre))
print(”)
print(‘Probability of Precipitation (>0) on a Day having TMin < 0: {}’.format(p_pre_bar_fb))
print(”)
print(“—————————————————“)

if __name__ == “__main__”:

# part 2.1
show_stats(np_array)
# show_histogram(np_array)
show_for_white_christmas(np_array)

# part 2.2
show_probability_of_precipitation(np_array)

# part 2.3
probability_of_freezing_boundary_layer_at_white_christmas(np_array)

# part 2.4 a
probability_of_freezing_boundary_layer_at_precipitation(np_array)

# part 2.4 b
probability_of_precipitation__at_freezing_boundary_layer(np_array)