def test_prob_wrong_input_negative_float(self):
     """
     If the input value n is negative float, probability(n)
     would raise ValueError('Input not of integer instance')
     """
     with self.assertRaisesRegexp(ValueError, "instance"):
         mpmath.nstr(probability(-4.3), 8)
Esempio n. 2
0
def writeCostOutput(output_file, data, return_periods, plot=True):
    """
    Write aggregated cost information to file and plot probability-cost
    curves for each zone.
    """
    LOG.info("Writing cost data to {0}".format(output_file))
    rps = np.concatenate([[1], return_periods])
    probs = probability(rps)

    output_path = os.path.split(output_file)[0]
    output_fileh = open(output_file, 'w')
    zones = data.keys()
    header = "REGION," + \
             ",".join(["RP" + str(int(r)) for r in return_periods]) + \
             ",ANN_COST,TOTAL_VALUE\n"
    output_fileh.write(header)

    for zone in zones:
        row = [data[zone]["cost" + str(int(r))] for r in return_periods]

        output_fileh.write("%s, %s, %12.0f, %13.0f\n" % (zone, ", ".join(
            ["%7.5f" % val
             for val in row]), data[zone]['ann_cost'], data[zone]['VALUE']))

        row = np.concatenate([[0], row])
        if plot:
            plotCostOutput(zone, row, probs, pjoin(output_path, 'plots'))

    output_fileh.close()
    return
Esempio n. 3
0
def main(player):
    game = _game(player)
    game.player.start(int(game.W / 2), int(game.H / 2))
    game.GetFlag()
    game.OpenSafe()

    while not game.player.over and not game.player.clear:
        game.GetFlag()
        game.OpenSafe()

        if game.cannot_open():
            prob = probability(game.player, game.flag)
            target, p_land = prob.searching()
            print(target, p_land)

            if 0 in target[2]:
                for i, tar in enumerate(target[2]):
                    if tar == 0:
                        position = game.num_to_position(target[0][i])
                        game.player.open(position[0], position[1])
                    elif tar == 1:
                        position = game.num_to_position(target[0][i])
                        game.flag[position[0]][position[1]] = True

            elif len(target[2]) != 0 and min(target[2]) < p_land:
                position = game.num_to_position(target[0][np.argmin(
                    target[2])])
                game.player.open(position[0], position[1])
            else:
                game.open_land()

    return game.player.clear
 def test_prob_wrong_input_negative_number(self):
     """
     If the input value n is negative number, probability(n)
     would raise ValueError('Input not a positive integer')
     """
     with self.assertRaisesRegexp(ValueError, "positive"):
         mpmath.nstr(probability(-4), 8)
 def test_prob_wrong_input_string(self):
     """
     If the input value n is a string, probability(n)
     would raise ValueError('Input not of integer instance')
     """
     with self.assertRaisesRegexp(ValueError, "instance"):
         mpmath.nstr(probability("die"), 8)
 def test_prob_wrong_input_zero(self):
     """
     If the input value n is zero, probability(n)
     would raise ValueError('Input not a positive integer')
     """
     with self.assertRaisesRegexp(ValueError, "positive"):
         mpmath.nstr(probability(0), 8)
 def test_prob_2_sided_die(self):
     """ test for n = 2 : P(n=1) = 0.5. This is like a coin toss.
         all outcomes = {HH, HT, TH, TT}
         desired outcomes = {HT, TH}
         probability = 2/4 = 0.5
     """
     self.assertEqual(mpmath.nstr(probability(2), 8), "0.5")
Esempio n. 8
0
def writeDmgOutput(output_file, data, return_periods, plot=True):
    """
    Write aggregated loss information to file and plot probability-loss
    curves for each zone
    """
    LOG.info("Writing damage data to {0}".format(output_file))
    rps = np.concatenate([[1], return_periods])
    probs = probability(rps)

    output_path = os.path.split(output_file)[0]
    output_fileh = open(output_file, 'w')
    zones = data.keys()
    header = "REGION," + \
             ",".join(["RP" + str(int(r)) for r in return_periods]) + \
             ",ANN_DMG, TOTAL_FLAREA\n"
    output_fileh.write(header)
    for zone in zones:
        row = [data[zone]["flarea" + str(int(r))] for r in return_periods]
        output_fileh.write("%s, %s, %7.5f, %9.5f\n" %
                           (zone, ", ".join(["%7.5f" % val for val in row]),
                            data[zone]['ann_dmg'], data[zone]['flarea_sum']))

        row = np.concatenate([[0], row])

        if plot:
            plotDmgOutput(zone, row, probs, pjoin(output_path, 'plots'))

    output_fileh.close()
Esempio n. 9
0
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.orientation = 'horizontal'
        self.destination = 'London'
        paths = graph.getPaths('London')
        self.path = "Kuala+Lumpur,Kabul,London"
        
        left_layout = BoxLayout(orientation = 'vertical')
        self.left_label = Label(text='From Kuala Lumpur\nTo {}'.format(self.destination))
        left_layout.add_widget(self.left_label)
        for d in destinations:
            btn = Button(text=d)
            #btn.bind(state=self.choose_destination)
            btn.bind(on_press=self.choose_destination)
            left_layout.add_widget(btn)
        self.add_widget(left_layout)
        
        # "./map.html?path=Kuala+Lumpur,Tokyo,New+York&id=1234"
        self.webview = CEFBrowser(url="https://waixiong.github.io/AlgoAssisgnmentMap/map.html?paths="+self.path+"&id=1234", size_hint=(3, 1))
        self.add_widget(self.webview)
        
        self.right_layout = BoxLayout(orientation = 'vertical', size_hint=(2, 1))
        self.probabilityGraph = probability(paths).canvas
        self.right_layout.add_widget(self.probabilityGraph)
        self.wordGraph = BoxLayout()
        self.right_layout.add_widget(self.wordGraph)

        f1, f2 = city['London'].graph()
        b = BoxLayout(orientation = 'vertical')
        b.add_widget(f1.canvas)
        b.add_widget(f2.canvas)
        self.wordGraph.add_widget(b)

        self.add_widget(self.right_layout)
Esempio n. 10
0
    def choose_destination(self, instance):
        self.destination = instance.text
        
        # graph.remove_edge('Kuala Lumpur', instance.text)
        # p = graph.dijkstra('Kuala Lumpur', instance.text)
        # graph.add_edge('Kuala Lumpur', instance.text, cost=calculate(locations['Kuala Lumpur']['lat'], locations['Kuala Lumpur']['lon'], locations[instance.text]['lat'], locations[instance.text]['lon']))
        paths = graph.getPaths(instance.text) ## distance.py Joyen
        cities = {}
        for path in paths:
            l = list(path.path)
            for i in range(1, len(l)-1):
                cities[l[i]] = True
        print(str(len(paths)) + ' paths prepared')
        #p = paths[0].path

        self.right_layout.remove_widget(self.probabilityGraph)
        self.right_layout.remove_widget(self.wordGraph)

        self.probabilityGraph = probability(paths).canvas
        self.wordGraph = BoxLayout()
        for c in cities:
            f1, f2 = city[c].graph()
            b = BoxLayout(orientation = 'vertical')
            b.add_widget(f1.canvas)
            b.add_widget(f2.canvas)
            self.wordGraph.add_widget(b)
        
        # p = mp.Pool(3)
        # theIterable = cities.keys()
        # print('KEY ' + str(theIterable) + '$$$$$$$$$$$$$$')
        # p.map(self.add_bar_chart,list(cities)) # range(0,1000) if you want to replicate your example
        # p.close()
        # p.join()

        self.right_layout.add_widget(self.probabilityGraph)
        self.right_layout.add_widget(self.wordGraph)

        paths.sort(key=getP, reverse=True)
        self.path = ""
        for path in paths:
            for cityP in list(path.path):
                self.path += cityP+','
            self.path = self.path[0:-1]
            self.path += '|'
        self.path = self.path[0:-1]
        #self.path = self.path.replace(' ', '+')
        print(self.path)
        #self.line.coordinates=[[2.7456, 101.7072], [locations[instance.text]['lat'], locations[instance.text]['lon']]]
        self.left_label.text = 'From Kuala Lumpur\nTo {}'.format(self.destination)
        self.webview.url = "https://waixiong.github.io/AlgoAssisgnmentMap/map.html?paths="+self.path
Esempio n. 11
0
def integrateLoss(prob, dmg):
    """Integrate the return period losses to evaluate annualised loss"""

    # Make an assumption that we have zero loss for a 1-year event:
    # This means we need to define the level of damage as zero, and
    # the probability as that of a 1-year return period event.

    LOG.debug("Integrating losses to evaluate annualised loss")
    if all(dmg < EPSILON):
        ann_loss = 0.0
    else:
        prob1yr = probability(1.0)
        dmg = np.concatenate([[0.0], dmg])
        prob = np.concatenate([[prob1yr], prob])
        ann_loss = simps(prob, dmg)

    return ann_loss
Esempio n. 12
0
def plotResults(avg_loss, tot_cost, return_periods, output_folder):
    """
    Plot the results as PML curves or similar
    """

    output_path = pjoin(output_folder, 'plots')

    return_periods = np.concatenate([[1], return_periods])
    probs = probability(return_periods)

    avg_loss = np.concatenate([[0], avg_loss])
    tot_cost = np.concatenate([[0], tot_cost])

    LOG.info("Plotting probability-loss curve")
    fig1 = pyplot.figure()
    ax1 = fig1.add_subplot(111)
    ax1.fill_between(avg_loss * 100., probs, 0.0, lw=2, alpha=0.5)
    ax1.set_yscale('log', nonposy='clip')
    ax1.set_ylim(0.0001, 1.0)
    ax1.grid(True, which='both', linestyle=':', color='k')
    pyplot.xlabel("Damage ratio (% of reconstruction cost)")
    pyplot.ylabel("Annual probability")
    pyplot.title("Probability-loss curve")
    pyplot.savefig(pjoin(output_path, "probability-loss.png"))
    LOG.info("Plotting return period-loss curve")
    pyplot.figure(2)
    pyplot.semilogx(return_periods, avg_loss * 100., lw=2, marker='+', ms=10)
    pyplot.xlabel("Return period (years)")
    pyplot.ylabel("Damage ratio (% of reconstruction cost)")
    pyplot.grid(True, which='both', linestyle=':', color='k')
    pyplot.title("Return period loss curve")
    pyplot.savefig(pjoin(output_path, "rp-loss-curve.png"))
    LOG.info("Plotting probability-cost curve")
    fig3 = pyplot.figure(3)
    ax3 = fig3.add_subplot(111)
    ax3.fill_between(tot_cost / np.power(10., 9), probs, 0.0, lw=2, alpha=0.5)
    ax3.set_yscale('log', nonposy='clip')
    ax3.set_ylim(0.0001, 1.0)
    ax3.grid(True, which='both', linestyle=':', color='k')
    pyplot.xlabel("Loss (billion Peso)")
    pyplot.ylabel("Annual probability")
    pyplot.title("Probability-loss curve")
    pyplot.savefig(pjoin(output_path, "probability-cost.png"))

    return
Esempio n. 13
0
def foo(filename):
	with open(filename, "r") as fin:
		fout = open("{0}_out".format(filename), "w")
		m = int(fin.readline())
		
		fout.write("{0}\n".format(m))
		for i in xrange(m + 1):
			r = int(fin.readline().split()[1].strip())
			n = int(fin.readline().strip())
			# print r, m, n
			fout.write("{0} {1}\n{2}\n".format(m, r, n))
			for j in xrange(n):
				arg, val = (float(word) for word in fin.readline().split())
				# print r, m, arg
				p = probability.probability(r, m, arg)
				fout.write("{0:0.9f} {1:0.9f} {2:0.9f}\n".format(arg, val, p))
			fin.readline()
			fout.write("\n")

		fout.close()
Esempio n. 14
0
        'THTT', 'HTHT', 'HTHH', 'HHHT', 'TTHH', 'THHH', 'TTTT', 'HHTT', 'TTTH',
        'TTHH', 'HTHT', 'HTHT', 'TTTT', 'HTTT', 'TTTH', 'HTTT', 'TTHH', 'THTH',
        'THHH', 'HTHH'
    ],
    'Colby': [
        'HTTH', 'HTHH', 'THTT', 'HHTH', 'TTHT', 'HTTT', 'THHH', 'TTHH', 'HHTT',
        'THTH', 'HHTT', 'THTH', 'THHH', 'TTHH', 'THTT', 'HHTH', 'HTTH', 'HTHH',
        'TTHT', 'HTTT'
    ],
    'Justin': [
        'THTT', 'HTHT', 'TTTH', 'THHT', 'HTHH', 'TTTH', 'THTH', 'HHTH', 'TTTT',
        'HTTH', 'HHTT', 'THHH', 'HHHH', 'THTH', 'HTTH', 'TTHH', 'HTHT', 'HHHT',
        'THHT', 'TTTH'
    ]
}
probability_results = [probability(x, 4) for x in range(5)]
person_results = {}


def analyze_coinset(num_heads, coinset):
    num_succesful_results = 0
    for coindata in coinset:
        heads = 0
        for outcome in coindata:
            if outcome == 'H':
                heads += 1
        if heads == num_heads:
            num_succesful_results += 1
    return num_succesful_results / len(coinset)

Esempio n. 15
0
def totalDamage(building_types, fields, records, return_periods,
                building_costs):
    """
    Calculate the total damage for each feature, for each return
    period.  Total damage is the weighted sum of damage to each
    building type, weighted by the fraction of floor area for that
    building type.
    """

    LOG.info("Calculating total damage to building stock")
    LOG.debug("Calculating total value of building stock in each feature")

    nrecords = len(records)
    n_bldg_type = len(building_types.keys())

    lu4 = getField('L4_USE', fields, records, str)
    lu5 = getField('L5_USE', fields, records, str)
    vintage = getField('ERA_CONST', fields, records, str)
    area_sqm = getField('AREA_SQM', fields, records)

    values = np.empty((n_bldg_type, nrecords))

    for i, bld_type in enumerate(building_types.keys()):
        flarea = getField(bld_type, fields, records)
        values[i, :] = calculateValue(flarea, lu4, lu5, bld_type,
                                      building_costs)

    totvalue = np.sum(values, axis=0)
    fields.append(['bldg_value', "N", 19, 0])

    for i, record in enumerate(records):
        record.append(totvalue[i])

    LOG.debug("Appending required fields for return period losses")
    for ret_per in return_periods:
        fields.append(['dmg' + str(int(ret_per)), "N", 9, 6])
        fields.append(['cost' + str(int(ret_per)), "N", 10, 0])
        fields.append(['dmgf' + str(int(ret_per)), "N", 10, 6])

    fields.append(['ann_loss', "N", 9, 6])
    fields.append(['ann_cost', "N", 10, 0])
    fields.append(['ann_dmgf', "N", 10, 6])

    # Create a mask to allow us to modify the vulnerability for
    # buildings of different ages. The key time is 1992. This applies
    # to the MWS, CWS, CHB and C1-L building types, where there were
    # changes to the construction materials used.
    vmask = np.array(vintage) == 'Post-1992'

    rp_damage = np.empty((len(return_periods), nrecords))
    rp_costs = np.empty((len(return_periods), nrecords))
    rp_dmgfl = np.empty((len(return_periods), nrecords))

    for n, ret_per in enumerate(return_periods):
        LOG.info("Processing return period {0}".format(ret_per.astype(int)))
        wind_speed = getField('V' + str(ret_per.astype(int)), fields, records)

        costs = np.empty((len(building_types.keys()), nrecords))
        dmg_flarea = np.empty((len(building_types.keys()), nrecords))

        for i, bld_type in enumerate(building_types.keys()):
            # Calculate corresponding value of built assets:
            flarea = getField(bld_type, fields, records)
            value = calculateValue(flarea, lu4, lu5, bld_type, building_costs)

            # Calculate the damage (as a fraction of replacement cost):
            mu = building_types[bld_type]['mu'] * np.ones(len(wind_speed))
            sigma = building_types[bld_type]['sigma'] * np.ones(
                len(wind_speed))
            scale = building_types[bld_type]['scale'] * np.ones(
                len(wind_speed))

            mu, sigma, scale = adjustDamageCurves(bld_type, vmask, mu, sigma,
                                                  scale)

            d = damage(wind_speed, mu, sigma, scale)

            costs[i, :] = d * value
            dmg_flarea[i, :] = 100. * d * flarea / area_sqm

        totcost = np.sum(costs, axis=0)
        totdmg = totcost / totvalue
        np.putmask(totdmg, totvalue == 0, 0)

        # Store the damaged floor area equivalent in units of hectare/sq km.
        totdmgfl = np.sum(dmg_flarea, axis=0)

        del costs
        del dmg_flarea

        for i, record in enumerate(records):
            record.extend([totdmg[i], totcost[i], totdmgfl[i]])

        rp_damage[n, :] = totdmg
        rp_costs[n, :] = totcost
        rp_dmgfl[n, :] = totdmgfl

        #LOG.info("Size of records = {0} MB".format(
        #            total_size(records)/1024**2))

    LOG.info("Calculating annualised losses")
    # Calculate annual probability:
    annual_prob = probability(return_periods)
    for i, record in enumerate(records):
        annualised_loss = integrateLoss(annual_prob, rp_damage[:, i])
        annualised_costs = integrateLoss(annual_prob, rp_costs[:, i])
        annualised_dmgfl = integrateLoss(annual_prob, rp_dmgfl[:, i])
        record.extend([annualised_loss, annualised_costs, annualised_dmgfl])

    return fields, records
Esempio n. 16
0
parser.add_option("-t", "--train",
                  dest="train",
                  help="Search for train or group of trains by name")
parser.add_option("-p", "--probability",
                  dest="probability", action="store_true", default=False,
                  help="Calculate probability to reach destination")
parser.add_option("-d", "--delays-file",
                  dest="path", default=DATA_PATH, metavar="FILE",
                  help="Change path to delays file")
parser.add_option("-s", "--show",
                  dest="show", action="store_true", default=False,
                  help="Only show graph (else they are stored in file)")

options, optionsValues = parser.parse_args()

# fix console encoding
for option, value in vars(options).iteritems():
    if isinstance(value, basestring):
        setattr(options, option, value.decode(sys.getfilesystemencoding()))
for i in range(0, len(optionsValues)):
    optionsValues[i] = optionsValues[i].decode(sys.getfilesystemencoding())

DATA_PATH = options.path

if not options.probability:
    from stats import stats
    stats(DATA_PATH, options)
else:
    from probability import probability
    probability(DATA_PATH, optionsValues)
Esempio n. 17
0

def analyze_coinset(num_heads, coinset):
    num_succesful_results = 0
    for coindata in coinset:
        heads = 0
        for outcome in coindata:
            if outcome == 'H':
                heads += 1
        if heads == num_heads:
            num_succesful_results += 1
    return num_succesful_results / len(coinset)


x_coords = range(4)
probablility_results = [probability(x, 3) for x in x_coords]
plt.plot(x_coords, probablility_results, linewidth=2.5)
# plt.plot([0,1,2,3,4],[0.1, 0.3, 0.5, 0.1, 0.1],linewidth=2.5)
for coinset in coinsets:
    plt.plot(x_coords, [analyze_coinset(x, coinset) for x in x_coords],
             linewidth=0.75)
# plt.plot([0,1,2,3,4],[0.3, 0.1, 0.4, 0.2, 0.1],linewidth=0.75)
# plt.plot([0,1,2,3,4],[0.2, 0.2, 0.3, 0.3, 0.2],linewidth=0.75)
plt.legend(['True', 'Coinset 1', 'Coinset 2', 'Coinset 3'])
plt.xlabel('Number of Heads')
plt.ylabel('Probability')
plt.title('True Distribution vs Coinsets')
plt.savefig('plot.png')
plt.show()

#Coin 2 is a fair coinset as it has an even likely hood of 1 and 2 heads which is the same as having th same likeliehood of having 2 heads and 2 tails.
 def test_prob_1_sided_die(self):
     """ test for n = 1 : P(n=1) = 1
     """
     self.assertEqual(mpmath.nstr(probability(1), 8), "1.0")
Esempio n. 19
0
import math
from monte_carlo import monte_carlo
from probability import probability


def kl_divergence(p, q):
    divergence = 0
    for i in range(len(p)):
        divergence += p[i] * (math.log(p[i] / q[i]))
    return divergence


def fix_zeroes(arr):
    for i in range(len(arr)):
        if arr[i] == 0:
            arr[i] = 0.0001
    return arr


probability_results = [probability(x, 10) for x in range(10)]
monte_carlo_100 = fix_zeroes([monte_carlo(x, 10, 100) for x in range(10)])
monte_carlo_1000 = fix_zeroes([monte_carlo(x, 10, 1000) for x in range(10)])
monte_carlo_10000 = [monte_carlo(x, 10, 10000) for x in range(10)]
print("\nDivergence with 100 trials: ")
print("\n   " + str(kl_divergence(probability_results, monte_carlo_100)))
print("\nDivergence with 1000 trials: ")
print("\n   " + str(kl_divergence(probability_results, monte_carlo_1000)))
print("\nDivergence with 1000 trials: ")
print("\n   " + str(kl_divergence(probability_results, monte_carlo_10000)))

# As the number of samples increases, the KL divergence gets closer to 0 because as the number of trials increases the monte_carlo estimation gets closer and closer the the actual probability.
p_u = .25
p_d = .25
p_mn = 0
probabilities = [p_r, p_l, p_u, p_d, p_mn]
iterations = 0

memory = {(m, n)}
locations = [[-1.5, 0], [-1, -0.5], [-1, 0.5], [-0.5, -1], [-0.5, 0],
             [-0.5, 1], [0, -1.5], [0, -0.5], [0, 0.5], [0, 1.5], [0.5, -1],
             [0.5, 0], [0.5, 1], [1, -0.5], [1, 0.5], [1.5, 0]]

while iterations < 200000:
    chemo = []
    for i in locations:
        if (m + i[0], n + i[1]) in memory:
            conc = 0
        else:
            conc = concentration(m + i[0], n + i[1], source_m, source_n)
        chemo.append(chemoattractant(conc, tip))

    transitions = transition(chemo, k)
    normal_transitions = normal_transition(transitions)
    probabilities = probability(normal_transitions, probabilities)
    m, n = movement(probabilities, memory, workspace, m, n, size, iterations)

    iterations += 1

plt.imshow(workspace)
cm.get_cmap("jet")
plt.show()
from probability import probability
from monte_carlo import monte_carlo

print("Monte Carlo Results:")
for _ in range(3):
    print("\n   " + str(monte_carlo(5, 8, 1000)))

print("\nProbability:")
print("\n   " + str(probability(5, 8)))