def test_significance(length): delta_array = [] for i in range(0, 1000): cause, effect = generate_continue_data(200, random.randint( 1, 3)) # random.randint(1,5) cause = normalize(cause) cause = zero_change(cause) effect = normalize(effect) effect = zero_change(effect) cause2effect = calculate_difference3(cause, effect, length) effect2cause = calculate_difference3(effect, cause, length) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause) p = math.pow(2, -(cause2effect - effect2cause)) #p_array.append(p) delta_array.append(cause2effect - effect2cause) fig = plt.figure() axes = fig.add_subplot(111) delta_array.sort(reverse=True) for i in range(len(delta_array)): if delta_array[i] < -math.log(0.05, 2): # 第i行数据,及returnMat[i:,0]及矩阵的切片意思是:i:i+1代表第i行数据,0代表第1列数据 axes.scatter(i, delta_array[i], color='grey') else: axes.scatter(i, delta_array[i], color='green') plt.xlabel('X') plt.ylabel('Y') plt.show()
def temperature_test(): indoor_temp, outdoor_temp = read_temperature() cause = outdoor_temp effect = indoor_temp cause = zero_change(cause) effect = zero_change(effect) cause2effect = calculate_difference3(cause, effect, 10) effect2cause = calculate_difference3(effect, cause, 10) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause)
def test_solar(): temp, solar = read_solar() cause = map(float, solar) effect = map(float, temp) cause = zero_change(cause) effect = zero_change(effect) cause2effect = calculate_difference3(cause, effect, 5) effect2cause = calculate_difference3(effect, cause, 5) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause)
def test_snow(): temp, snow = read_snow() cause = map(float, temp) effect = map(float, snow) cause = normalize(cause) cause = zero_change(cause) effect = normalize(effect) effect = zero_change(effect) cause2effect = calculate_difference3(cause, effect, 10) effect2cause = calculate_difference3(effect, cause, 10) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause)
def ozone_test(): ozone, temp = read_ozone() cause = temp effect = ozone cause = zero_change(cause) effect = zero_change(effect) for i in range(4, 10): cause2effect = calculate_difference3(cause, effect, i) effect2cause = calculate_difference3(effect, cause, i) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause) p = math.pow(2, -(cause2effect - effect2cause)) print p
def test_sin2(c, e): cause = c effect = e #cause = normalize(cause) cause = zero_change(cause) #effect = normalize(effect) effect = zero_change(effect) cause2effect = calculate_difference3(cause, effect, 100) effect2cause = calculate_difference3(effect, cause, 100) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause) p = math.pow(2, -(cause2effect - effect2cause)) print p
def causality_test(): x, y = read_stock_price() a, b = read_ozone() cause = y[0:300] effect = b[0:300] cause = normalize(cause) cause = zero_change(cause) effect = normalize(effect) effect = zero_change(effect) cause2effect = calculate_difference3(cause, effect, 10) effect2cause = calculate_difference3(effect, cause, 10) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause) p = math.pow(2, -(cause2effect - effect2cause)) print p
def test_daily_temperature(): index, temp = read_daily_temperature() counter = 0 index_low = [] temp_low = [] while counter < len(index): index_low.append(index[counter]) temp_low.append(temp[counter]) counter += 30 cause = index effect = temp cause = zero_change(cause) effect = zero_change(effect) cause2effect = calculate_difference4(cause, effect, 5) effect2cause = calculate_difference4(effect, cause, 5) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause)
def river_test2(): length = 6 date_time, fremersdorf, hanweiler, sanktarnual = collect_data2() fremersdorf = map(float, fremersdorf) hanweiler = map(float, hanweiler) sanktarnual = map(float, sanktarnual) fremersdorf = zero_change(fremersdorf) hanweiler = zero_change(hanweiler) sanktarnual = zero_change(sanktarnual) cause = change_to_zero_one(fremersdorf) effect = change_to_zero_one(hanweiler) delta_fr_to_hw = bernoulli2(effect, length) - cbernoulli2( effect, cause, length) delta_hw_to_fr = bernoulli2(cause, length) - cbernoulli2( cause, effect, length) #delta_fr_to_hw = calculate_difference3(fremersdorf,hanweiler,10) #delta_hw_to_fr = calculate_difference3(hanweiler,fremersdorf,10) print "fr->hw =", delta_fr_to_hw, " hw->fr =", delta_hw_to_fr cause = change_to_zero_one(sanktarnual) effect = change_to_zero_one(hanweiler) delta_sa_to_hw = bernoulli2(effect, length) - cbernoulli2( effect, cause, length) delta_hw_to_sa = bernoulli2(cause, length) - cbernoulli2( cause, effect, length) #delta_sa_to_hw =calculate_difference3(sanktarnual,hanweiler,10) #delta_hw_to_sa = calculate_difference3(hanweiler,sanktarnual,10) print "sa->hw =", delta_sa_to_hw, " hw->sa =", delta_hw_to_sa cause = change_to_zero_one(fremersdorf) effect = change_to_zero_one(sanktarnual) delta_fr_to_sa = bernoulli2(effect, length) - cbernoulli2( effect, cause, length) delta_sa_to_fr = bernoulli2(cause, length) - cbernoulli2( cause, effect, length) #delta_fr_to_sa = calculate_difference3(fremersdorf,sanktarnual,10) #delta_sa_to_fr = calculate_difference3(sanktarnual,fremersdorf,10) print "fr->sa =", delta_fr_to_sa, " sa->fr =", delta_sa_to_fr
def test_simulation(length): counter = 0 p_array = [] for i in range(0, 1000): cause, effect = generate_continue_data(200, 3) # random.randint(1,5) #effect, test2 = generate_continue_data(200, 3) # random.randint(1,5) cause = normalize(cause) cause = zero_change(cause) effect = normalize(effect) effect = zero_change(effect) cause2effect = calculate_difference4(cause, effect, length) effect2cause = calculate_difference4(effect, cause, length) print 'cause' + ' -> ' + 'effect' + ':' + str(cause2effect) print 'effect' + ' -> ' + 'cause' + ':' + str(effect2cause) p = math.pow(2, -(cause2effect - effect2cause)) p_array.append(p) if cause2effect > effect2cause and (cause2effect - effect2cause) >= -log(0.05): counter += 1 print print counter print bh_procedure(p_array, 0.05) return counter / 100.0
def river_test(): length = 6 date_time, speyer, mannheim, worms, mainz = collect_data() counter = 0 speyer_low = [] mannheim_low = [] worms_low = [] mainz_low = [] while counter < 2880: speyer_low.append(speyer[counter]) mannheim_low.append(mannheim[counter]) worms_low.append(worms[counter]) mainz_low.append(mainz[counter]) counter += 1 speyer = map(float, speyer_low) mannheim = map(float, mannheim_low) worms = map(float, worms_low) mainz = map(float, mainz_low) cause = speyer effect = mannheim cause = zero_change(cause) effect = zero_change(effect) cause = change_to_zero_one(cause) effect = change_to_zero_one(effect) sp2ma = bernoulli2(effect, length) - cbernoulli2(effect, cause, length) ma2sp = bernoulli2(cause, length) - cbernoulli2(cause, effect, length) #sp2ma = calculate_difference3(cause, effect, 10) #ma2sp = calculate_difference3(effect, cause, 10) print 'sp' + ' -> ' + 'ma' + ':' + str(sp2ma) print 'ma' + ' -> ' + 'sp' + ':' + str(ma2sp) print cause = speyer effect = worms cause = zero_change(cause) effect = zero_change(effect) cause = change_to_zero_one(cause) effect = change_to_zero_one(effect) sp2wo = bernoulli2(effect, length) - cbernoulli2(effect, cause, length) wo2sp = bernoulli2(cause, length) - cbernoulli2(cause, effect, length) #sp2wo = calculate_difference3(cause, effect, 10) #wo2sp = calculate_difference3(effect, cause, 10) print 'sp' + ' -> ' + 'wo' + ':' + str(sp2wo) print 'wo' + ' -> ' + 'sp' + ':' + str(wo2sp) print cause = speyer effect = mainz cause = zero_change(cause) effect = zero_change(effect) cause = change_to_zero_one(cause) effect = change_to_zero_one(effect) sp2mz = bernoulli2(effect, length) - cbernoulli2(effect, cause, length) mz2sp = bernoulli2(cause, length) - cbernoulli2(cause, effect, length) #sp2mz = calculate_difference3(cause, effect, 10) #mz2sp = calculate_difference3(effect, cause, 10) print 'sp' + ' -> ' + 'mz' + ':' + str(sp2mz) print 'mz' + ' -> ' + 'sp' + ':' + str(mz2sp) print cause = mannheim effect = worms cause = zero_change(cause) effect = zero_change(effect) cause = change_to_zero_one(cause) effect = change_to_zero_one(effect) ma2wo = bernoulli2(effect, length) - cbernoulli2(effect, cause, length) wo2ma = bernoulli2(cause, length) - cbernoulli2(cause, effect, length) #ma2wo = calculate_difference3(cause, effect, 10) #wo2ma = calculate_difference3(effect, cause, 10) print 'ma' + ' -> ' + 'wo' + ':' + str(ma2wo) print 'wo' + ' -> ' + 'ma' + ':' + str(wo2ma) print cause = mannheim effect = mainz cause = zero_change(cause) effect = zero_change(effect) cause = change_to_zero_one(cause) effect = change_to_zero_one(effect) ma2mz = bernoulli2(effect, length) - cbernoulli2(effect, cause, length) mz2ma = bernoulli2(cause, length) - cbernoulli2(cause, effect, length) #ma2mz= calculate_difference3(cause, effect, 10) #mz2ma = calculate_difference3(effect, cause, 10) print 'ma' + ' -> ' + 'mz' + ':' + str(ma2mz) print 'mz' + ' -> ' + 'ma' + ':' + str(mz2ma) print cause = worms effect = mainz cause = zero_change(cause) effect = zero_change(effect) cause = change_to_zero_one(cause) effect = change_to_zero_one(effect) wo2mz = bernoulli2(effect, length) - cbernoulli2(effect, cause, length) mz2wo = bernoulli2(cause, length) - cbernoulli2(cause, effect, length) #wo2mz = calculate_difference3(cause, effect, 10) #mz2wo = calculate_difference3(effect, cause, 10) print 'wo' + ' -> ' + 'mz' + ':' + str(wo2mz) print 'mz' + ' -> ' + 'wo' + ':' + str(mz2wo) print