-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonteCarloGrowing.py
80 lines (65 loc) · 2.47 KB
/
MonteCarloGrowing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#Elizabeth Mahon
#Monte Carlo simulation (?) that reassigns the year/%non growing season
#then calculates the slope for each configuration
#Then chucks it in a bin and makes a histogram
#This should end up showing that the actual value is fairly unlikely
import matplotlib.pyplot as pyplot
import numpy as np
from RecentLeastSquares import LeastSquares
from RecordReader import RecordReader
from Covariance import Covariance
import random
def MonteCarloGrowing(folder, master):
data = RecordReader(folder, master)
index = data.keys()
diversions = list()
growdiv = list()
percents = list()
years = list()
all_delta = list()
trials = 10000000 #chosen arbitrarily
bins = dict()
p = 0.0
for k in range(62): #make room for each year in structures
diversions.append(0)
growdiv.append(0)
#get data for growing season
for i in range(62):
for j in range(12):
#next part necessary because there's a bit of imprecision introduced in the indexes somewhere, so I can't just recalculate them
for thing in index:
if(thing < (i+1950+.01*(j+1)+.005) and thing > (i+1950+.01*(j+1)-.005)):
diversions[i] += data[thing]
if (j < 10 and j > 3): #growing season
growdiv[i] += data[thing]
#calculate original percents
for n in range(62):
percents.append((1 - (growdiv[n]/diversions[n])) *100)
years.append(1950+n)
#get original least squares fit
deltastar = LeastSquares(zip(years,percents))
#randomize percents....
for i in range(trials):
rand = random.sample(percents, len(percents))
slope, intercept = LeastSquares(zip(years, rand))
all_delta.append(slope)
for m in all_delta:
if m > deltastar[0]:
p += 1
likely = p/trials #likelihood we would get the calculated slope randomly....
print deltastar
print likely
#make bins
maximum = max(all_delta)
binvals = np.linspace(0,maximum, endpoint = True)
print maximum
for val in binvals:
bins[val] = 0
for delta in all_delta:
for q in range(len(binvals)-1):
if ((delta < binvals[q+1]) and (delta > binvals[q])):
bins[binvals[q]] += 1
wid = ((binvals[1] - binvals[2])/2)
pyplot.bar(bins.keys()-wid, bins.values(),width = wid)
pyplot.show()
MonteCarloGrowing('/home/elizabeth/Dropbox/Windows-LinuxShare/','PercentStructures.csv')