Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Penerapan SAS/IML

Operator Sweep & Penerapan pada Perhitungan Numerik

𝜏𝜌
Penggunaan Gugus Data
Penggunaan Gugus Data SAS
• Pernyataan USE untuk membuka gugus data SAS
USE SAS-data-set <VAR operand > <WHERE(expression) > ;
• Pernyataan CREATE membuka gugus data baru baik sebagai
gugus data input maupun output
• Using the READ Statement with the VAR and INTO Clauses
Latihan 1
– Bangkitkanlah variabel berikut ini pada tahapan
data dengan nama data3
• x = 1,2,3…,50
• y = x(1/x)
• z = x/y
– Gunakan pada proc IML dengan semua variabel
tersebut menjadi satu variabel A, kemudian
carilah rataan untuk setiap variabel (misalkan M).
– Buatlah data “hasil” dari proc IML tersebut yang
berisi M
Jawaban 1
data data1;
do x = 1 to 50;
y =x**(1/x);
z =x/y;
output;
end;
run;

proc iml;
use data1;
read all var {x y z} into A;
M = A[:,];
create hasil var{M};
append;
quit;
proc print data=hasil;
run;
SWEEP Operator
SWEEP Algorithm:

• D = akk
• Divide kth row using D
• For every i  k rows, do:
1.B = aik
2.ith row minus using B* kth row
3.aik = -B/D
• akk = 1/D
Latihan 2
proc iml;
reset print;
A = {4 2 2,2 7 0,2 0 0};
B = sweep(A,1);
C = sweep(B,2);
sama
C1 = sweep(A,{1 2});
D = sweep(C,3);
D1 = sweep(A,{1 2 3});
D2 = sweep(A,{3 1 2});
sama
kali = A*D;
sama
invA = inv(A); Sama untuk anak
matriks 2 x 2
A22 = A[{1 2},{1 2}];
invA22 = inv(A22);

A1 = sweep(B,1);
More about SWEEP
• To use the SWEEP function for regression, suppose the matrix A contains
𝑋′𝑋 𝑋′𝑌
𝑌′𝑋 𝑌′𝑌
• where X'X is k ×k.
Then B = SWEEP(A,1 ... k) contains
𝑋 ′ 𝑋 −1 𝑋 ′ 𝑋 −1 𝑋 ′ 𝑌
−𝑌 ′ 𝑋 𝑋 ′ 𝑋 −1 𝑌′(𝐼 − 𝑋 𝑋 ′ 𝑋 −1 𝑋′)𝑌
• The partitions of B form the beta values, SSE, and a matrix proportional to
the covariance of the beta values for the least-squares estimates of B in
the linear model
Latihan 3
• Find the parameter estimate from this regression
case using SWEEP operator
y X
10 3
15 5
16 6
7 2
Jawaban 3
proc iml;
reset print;
X = {3,5,6,2};
Y = {10,15,16,7};
X1 = J(4,1,1)||X;

XX = X1`*X1;
XY = X1`*Y;
YX = XY`; *ATAU Y`*X1;
YY = Y`*Y;

A = (XX||XY)//(YX||YY);

s = sweep(A,{1,2});

beta = s[{1 2},3];


betaa = inv(XX)*XY;

jkg = s[3,3];
jkga = YY-YX*inv(XX)*XY;
Iterative Modules
• Using DO statement clauses :
DO scalar=start TO stop <BY increment>;
DO <scalar=start TO stop <BY increment>>
WHILE(expression);
DO <scalar=start TO stop <BY increment>>
UNTIL(expression);
Application 1
• Forecasting the sum of infinity sequent
Assume we have infinity sequent 𝑧
so
𝑧 = 𝑧1 , 𝑧2 , 𝑧3 …
We need to forecast the number of sum from 𝑧

𝑠𝑢𝑚 𝑧 = 𝑧𝑖
𝑖=1
Latihan 4
• Suppose an infinity sequent 𝑦 where
2𝑖 + 1
𝑦𝑖 = 2
𝑖 +2
We need to forecast sum of 𝑦𝑖 , let
∞ ∞
2𝑖 + 1
𝑧= 𝑦𝑖 =
𝑖2 + 2
𝑖=1 𝑖=1
Jawaban 4
proc iml;
start ss;
y0 = 0;
i = 1;
e = 10;
do while(e>0.00001);
y1 = y0 + (2*i +1)/(i**2 + 2);
e = abs(y1-y0);
y0 = y1;
i = i+1;
end;
finish;

run ss;
print y0 i;
Latihan 5
• Make an IML program for calculating the
forecasting of sum from
6 8 10 12
𝑧 =2− + − + −⋯
5 10 17 26
Jawaban 5

𝑛+1
2𝑛 + 2
𝑧 = −1
𝑛2 + 1
𝑛=1
proc iml;
start ss;
y0 = 0;i = 1;e = 10;
do while(e>0.00001);
y1 = y0 + (((-1)**(i+1))*((2*i)+2)/((i**2)+1));
e = abs(y1-y0);
y0 = y1;
i = i+1;
end;
finish;

run ss;
print y0 i;
Latihan 6
• Find the forcasting of sum from this sequent
below:
2 3 6 11 18 27
+ + + + + +⋯
5 40 135 320 625 1080
Jawaban 6

𝑛2 − 2𝑛 + 3
5𝑛3
𝑛=1
proc iml;
start ss;
y0 = 0; i = 1; e = 10;
do while(e>0.00001);
y1 = y0 + ((i-1)**2+2)/(5*i**3);
e = abs(y1-y0);
y0 = y1;
i = i+1;
end;
finish;

run ss;
print y0 i;
Application 2
• Numerical Methods for finding solution from
equation 𝑓 𝑥 = 0
→ Newton Raphson
𝑓(𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
𝑓′(𝑥𝑛 )
Latihan 7
• Let the equation 𝑥 3 − 𝑥 − 1 = 0
Using Newton Raphson method, find the real
root from the equation above !
Jawaban 7
• 𝑓 𝑥 = 𝑥3 − 𝑥 − 1
• 𝑓 ′ 𝑥 = 3𝑥 2 − 1
• Since 𝑓 1 = −1 and 𝑓 2 = 5, the function
has a root in the interval [1,2]
• Let’s make an initial guess 𝑥0 = 1.5
𝑥𝑛 3 − 𝑥𝑛 − 1
𝑥𝑛+1 = 𝑥𝑛 − 2
3𝑥𝑛 − 1
Jawaban 7
proc iml;
*evaluasi selisih x;
start nr(x0);
e = 10;
do while(e > 0.00001);
x1 = x0 - (x0**3-x0-1)/(3*x0**2-1);
e = abs(x1-x0);
x0 = x1;
end;
return(x1);
finish;

x0 = 1.5;
hasil=nr(x0);
print hasil;
Jawaban 7
*evaluasi nilai f(x) menuju 0;
start nr1(x0);
e = 10;
do while(e > 0.00001);
x1 = x0 - (x0**3-x0-1)/(3*x0**2-1);
f1 = x1**3-x1-1;
e = abs(f1-0);
x0 = x1;
end;
return(x1);
finish;

x0 = 1.5;
hasil=nr1(x0);
print hasil;
Plot latihan 7

data data2;
do x=-10 to 10 by 0.1;
y=x**3-x-1;
output;
end;
run;
proc plot;
plot y*x;
run;
Thanks 

You might also like