ファイル:Probabilities7.svg

ページのコンテンツが他言語でサポートされていません。

元のファイル(SVG ファイル、360 × 270 ピクセル、ファイルサイズ: 215キロバイト)

概要

解説
English: A probability matrix representing the bias in positions for the "Permute-With-All" algorithm from Introduction to Algorithms (Cormen et al.), also described as an implementation error in the Fisher-Yates shuffle
日付
原典 投稿者自身による著作物
作者 Kostmo
SVG 開発
InfoField
 
このSVGのソースコードは正しい
 
この ベクター画像Matplotlibで作成されました。
 
The file size of this SVG plot may be irrationally large because its text has been converted to paths inhibiting translations.
ソースコード
InfoField

Python code

#!/usr/bin/env python

def swap(a, src, dst):
	a[src], a[dst] = a[dst], a[src]


# =============================================================================
def PermuteWithAll(array, randoms_sequence=None):
	# take swap target index with equal probability in the range 0..N
	a = array[:]
	for i in range(len(a)):
		dest = randoms_sequence[i] if randoms_sequence else randrange(len(a))
		swap(a, i, dest)
	return a

# =============================================================================
def flatten(nested):
	flat = []
	for el in nested:
		if type(el) is list:
			flat.extend( flatten(el) )
		else:
			flat.append( el )
	return flat

# =============================================================================
# Plots a two-dimensional matrix
def plot_nice_matrix(xy, a, denominator=1):

	from matplotlib.backends.backend_gtkcairo import FigureCanvasGTKCairo as FigureCanvas
	from matplotlib import mpl
	from matplotlib.figure import Figure, SubplotParams
	from matplotlib.ticker import FormatStrFormatter, FixedLocator

	normalized = [[x/float(denominator) for x in row] for row in a]
	z = flatten(normalized)
	x = xy*len(xy)
	y = flatten([[i]*len(xy) for i in xy])

	sizes = [400*q for q in z]	# The matplotlib documentation says that the "size" values are in units of points^2
	f = Figure(figsize=(4, 3), dpi=100, subplotpars=SubplotParams(bottom=0.2, left=0.15, top=0.85, right=0.8))
	main_axis = f.add_subplot(111)
	scatter2 = main_axis.scatter(x, y, s=sizes, c=z, cmap=mpl.cm.RdBu)
 
	main_axis.set_xlim(-1, len(xy))
	main_axis.set_ylim(-1, len(xy))
	main_axis.invert_yaxis()

	for i, row in enumerate(a):
		for j, col in enumerate(row):
			main_axis.text(j, i + 0.5,
				"%.1f%%" % (100*row[j]/float(denominator)),
				verticalalignment='center',
				horizontalalignment='center',
				fontsize=4.5)

	cb = f.colorbar(scatter2, orientation="vertical")
	cb.set_label("Probability")
	main_axis.set_xlabel("Randomized Position")
	main_axis.set_ylabel("Original Position")
	main_axis.grid(True, color='#AAAAAA')
	main_axis.xaxis.set_major_formatter( FormatStrFormatter("%d") )
	main_axis.xaxis.set_major_locator( FixedLocator(xy) )
	main_axis.yaxis.set_major_formatter( FormatStrFormatter("%d") )
	main_axis.yaxis.set_major_locator( FixedLocator(xy) )
	f.suptitle( '"Permute-With-All" Order Bias' )

	chart_title = "probabilities" + str( len(xy) )
	FigureCanvas( f ).print_figure(chart_title + ".svg", format="svg", transparent=True)

# =============================================================================
def position_frequency_matrix(a, permutation_bins):

	# Initialize a zeroed 2D array
	probabilities = [[0 for i in range(len(a))] for j in range(len(a))]

	for sequence, count in permutation_bins.items():
		for new_position, original_position in enumerate(sequence):
			probabilities[original_position][new_position] += count
	return probabilities

# =============================================================================
if __name__ == "__main__":

	from collections import defaultdict
	import itertools

	for length in range(2, 8):

		A = range(length)
		print A

		permutation_bins = defaultdict(int)
		for random_number_sequence in itertools.product(A, repeat=len(A)):
			permutation_bins[tuple(PermuteWithAll(A, random_number_sequence))] += 1

		probabilities = position_frequency_matrix(A, permutation_bins)
		denominator = len(A)**len(A)
		plot_nice_matrix(A, probabilities, denominator)

ライセンス

この作品の著作権者である私は、この作品を以下のライセンスで提供します。
w:ja:クリエイティブ・コモンズ
表示 継承
このファイルはクリエイティブ・コモンズ 表示-継承 3.0 非移植ライセンスのもとに利用を許諾されています。
あなたは以下の条件に従う場合に限り、自由に
  • 共有 – 本作品を複製、頒布、展示、実演できます。
  • 再構成 – 二次的著作物を作成できます。
あなたの従うべき条件は以下の通りです。
  • 表示 – あなたは適切なクレジットを表示し、ライセンスへのリンクを提供し、変更があったらその旨を示さなければなりません。これらは合理的であればどのような方法で行っても構いませんが、許諾者があなたやあなたの利用行為を支持していると示唆するような方法は除きます。
  • 継承 – もしあなたがこの作品をリミックスしたり、改変したり、加工した場合には、あなたはあなたの貢献部分を元の作品とこれと同一または互換性があるライセンスの下に頒布しなければなりません。
GNU head この文書は、フリーソフトウェア財団発行のGNUフリー文書利用許諾書 (GNU Free Documentation License) 1.2またはそれ以降のバージョンの規約に基づき、複製や再配布、改変が許可されます。不可変更部分、表紙、背表紙はありません。このライセンスの複製は、GNUフリー文書利用許諾書という章に含まれています。
あなたは上記のライセンスから、どれか一つ以上を選択できます。

キャプション

このファイルの内容を1行で記述してください

このファイルに描写されている項目

題材

15 6 2010

ファイルの履歴

過去の版のファイルを表示するには、その版の日時をクリックしてください。

日付と時刻サムネイル寸法利用者コメント
現在の版2010年6月15日 (火) 15:182010年6月15日 (火) 15:18時点における版のサムネイル360 × 270 (215キロバイト)Kostmo{{Information |Description={{en|1=A probability matrix representing the bias in positions for the Permute-With-All algorithm from w:Introduction to Algorithms, also described as an implementation error in the [[w:Fisher-Yates s

以下のページがこのファイルを使用しています:

グローバルなファイル使用状況

以下に挙げる他のウィキがこの画像を使っています: