1 / 20

情報基礎実習 I ( 第 8 回)

情報基礎実習 I ( 第 8 回). 木 曜4・5限 担当 :北川 晃. プログラミング演習. ‘least_square_data.csv’ より,データの点数 n と, ( x i ,y i ) ( i =1,…, n ) を読み込んで,最小二乗法よりその回帰直線 y = ax + b を求めよ.. least_square_data.csv. 5 1.2, 2.2 2.1, 3.8 3.3, 5.6 4.1, 7.1 5.0, 8.8. 配列 a ( i ) と,要素数 n を引数として, 平均値 ’ ave ’ を返す関数を 用いる.

alair
Download Presentation

情報基礎実習 I ( 第 8 回)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 情報基礎実習I(第8回) 木曜4・5限 担当:北川 晃

  2. プログラミング演習 ‘least_square_data.csv’より,データの点数nと,(xi,yi) (i=1,…,n)を読み込んで,最小二乗法よりその回帰直線y=ax+bを求めよ. least_square_data.csv 5 1.2, 2.2 2.1, 3.8 3.3, 5.6 4.1, 7.1 5.0, 8.8 配列a(i)と,要素数nを引数として, 平均値’ave’を返す関数を用いる

  3. 最小二乗法による回帰直線:出力例 データには有効数字 二桁分しか信頼性がない

  4. 最小二乗法による回帰直線:プログラム例 Sub Main() Dim n As Integer, s, items(10) As String, a, b As Single Dim x(10), x2(10), y(10), xy(10), x_a, y_a, x2_a, xy_a As Single Dim ReadText As New IO.StreamReader( _ "D:\…\least_square_data.csv", System.Text.Encoding.Default) n = ReadText.ReadLine() For i As Integer = 1 To n s = ReadText.ReadLine() items = Split(s, ",") x(i) = CSng(items(0)) y(i) = CSng(items(1)) x2(i) = x(i) ^ 2 xy(i) = x(i) * y(i) Next ReadText.Close() • xi2 • xiyi の計算

  5. 最小二乗法による回帰直線:プログラム例 x_a= average(n, x) y_a= average(n, y) xy_a= average(n, xy) x2_a = average(n, x2) a = (xy_a - x_a * y_a) / (x2_a - x_a ^ 2) b = y_a - a * x_a Console.WriteLine("回帰直線は,y={0}x+{1}", a, b) End Sub Function average(n, a) Dim s As Single = 0, ave As Single For i As Integer = 1 To n s = s + a(i) Next ave = s / n Return ave End Function それぞれの 平均値の計算 傾き,切片の計算

  6. 乱数とは? サイコロを振って出る目の数のように,予測はできないが, ある確率法則に従うでたらめな数. • 一様乱数 • 正規乱数 • ポアッソン乱数 乱数の利用: 自然科学および社会科学の問題のシミュレーション 暗号の秘密鍵生成

  7. 乱数の発生方法 サイコロを振る. 自然現象を利用する(電子管のノイズ,放射性崩壊など) 一つ前の値から次の値を計算する乱数列を生成する (擬似乱数). 例: 6 4 6 4 3 4 3 2 3 5 4 3 1 2 2 2 4 6 2 1 5 6 3 5 3 4 4 1 2 6 1 6 混合型合同法(擬似一様乱数列の生成法) (μを法として合同)

  8. 疑似乱数列の特徴 • 生来の乱数ではないので(初期値を与えている), • 同じ条件で計算すれば,再現性がある. • 周期がある.少なくとも 回後には,同じ数列が現れる. を大きくとる. 16ビットの乱数列だと・・・ (計算機で使える最大の数を使うとよい)

  9. 例題:疑似乱数列の発生 混合型合同法を用いて,乱数の初期値より, 擬似乱数列を生成するプログラムを作れ. 考え方: 初期値を読み込む. を計算し,で割ったあまりを計算する. あまりを次の乱数として,とおく. さらに次の乱数を計算する.

  10. 疑似乱数列の発生:出力例

  11. 疑似乱数列の発生:プログラム例 Dim mu As Integer = 2 ^ 15 Dim x, lambda, c As Integer Console.WriteLine("xの初期値(0-32768)を入力して下さい") Console.Write("x0=") x = Console.ReadLine() lambda = 12869 c = 6925 For i As Integer = 1 To 20 x = (lambda * x + c) Mod mu Console.WriteLine("{0,8}", x) Next

  12. プログラミング演習 疑似乱数列を発生させるプログラムを用いて, 六面サイコロの出目を生成するプログラムを作れ. • 考え方 • 混合型合同法では,0≦x<μの乱数が得られる. • 得られた乱数をμで割れば,[0,1)の乱数が得られる. • [0,1)の規格化乱数が得られれば,これに(b-a) • をかけてaを加えることで,[a,b)の乱数が得られる. • 実数で得られる乱数列を整数化する.

  13. サイコロの出目:出力例

  14. サイコロの出目の発生:プログラム例 Dim mu As Integer = 2 ^ 15 Sub Main() Console.Title = "乱数の発生" Dim x, x_dice As Integer, n As Integer = 100 Console.WriteLine("xの初期値(0-32768)を入力して下さい") Console.Write("x0=") x = Console.ReadLine() For i As Integer = 1 To n x_dice = Fix(6 * uni_rnd(x) / mu + 1) If i Mod 10 <> 0 Then Console.Write("{0,5}", x_dice) Else Console.WriteLine("{0,5}", x_dice) End If x = uni_rnd(x) Next End Sub 一様乱数を生成する関数 1≦x<7の 実数列を生成 xを次の値に置き直す

  15. サイコロの出目の発生:プログラム例(つづき)サイコロの出目の発生:プログラム例(つづき) Function uni_rnd(x) As Single Dim lambda, c As Integer lambda = 12869 c = 6925 x = (lambda * x + c) Mod mu Return x End Function 一様乱数を計算する 関数副プログラム

  16. プログラミング演習 六面サイコロの出目に関するプログラムを用いて, 各出目の頻度を計算せよ. ほぼ均等に1/6=1.666…

  17. サイコロの出目の頻度:プログラム例 各目の カウンター Dim mu As Integer = 2 ^ 15 Sub Main() Console.Title = "乱数の発生" Dim x, x_dice, n, c1, c2, c3, c4, c5, c6 As Integer Dim p1, p2, p3, p4, p5, p6 As Single c1 = 0 : c2 = 0 : c3 = 0 : c4 = 0 : c5 = 0 : c6 = 0 Console.WriteLine("xの初期値(0-32768)を入力して下さい") Console.Write("x0=") x = Console.ReadLine() n = 1000000 各目の 頻度 試行回数

  18. サイコロの出目の頻度:プログラム例(つづき)サイコロの出目の頻度:プログラム例(つづき) For i As Integer = 1 To n x_dice = Fix(6 * uni_rnd(x) / mu + 1) Select Case x_dice Case 1 c1 = c1 + 1 Case 2 c2 = c2 + 1 Case 3 c3 = c3 + 1 Case 4 c4 = c4 + 1 Case 5 c5 = c5 + 1 Case 6 c6 = c6 + 1 End Select x = uni_rnd(x) Next 出目の判定とカウント

  19. サイコロの出目の頻度:プログラム例(つづき)サイコロの出目の頻度:プログラム例(つづき) p1 = c1 / n Console.WriteLine("1の確率は{0}", p1) p2 = c2 / n Console.WriteLine("2の確率は{0}", p2) p3 = c3 / n Console.WriteLine("3の確率は{0}", p3) p4 = c4 / n Console.WriteLine("4の確率は{0}", p4) p5 = c5 / n Console.WriteLine("5の確率は{0}", p5) p6 = c6 / n Console.WriteLine("6の確率は{0}", p6) End Sub 出目の頻度と表示

  20. サイコロの出目の頻度:プログラム例(つづき)サイコロの出目の頻度:プログラム例(つづき) Function uni_rnd(x) As Single Dim lambda, c As Integer lambda = 12869 c = 6925 x = (lambda * x + c) Mod mu Return x End Function 一様乱数を計算する 関数副プログラム

More Related