0% found this document useful (0 votes)
8 views4 pages

SAS_Code_Supervisor_Visit_Probabilities-revised

The document contains SAS IML code for solving a linear programming problem and calculating transition probabilities between defined sites based on their coordinates. It includes functions for calculating Euclidean distances and normalizing a transition matrix derived from directed edges. Additionally, it computes long-run visit probabilities using eigenvalues and eigenvectors of the transition matrix.

Uploaded by

Kevin Otako
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

SAS_Code_Supervisor_Visit_Probabilities-revised

The document contains SAS IML code for solving a linear programming problem and calculating transition probabilities between defined sites based on their coordinates. It includes functions for calculating Euclidean distances and normalizing a transition matrix derived from directed edges. Additionally, it computes long-run visit probabilities using eigenvalues and eigenvectors of the transition matrix.

Uploaded by

Kevin Otako
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

proc iml;

c = {28.89, 28.64, 21.44, 17.42, 15.81, 19.81};


A = {1 1 1 1 1 1}; /* total supply constraint */
b = {18};
ops = {'E'}; /* equality constraint */
bounds = j(6, 2, .);
bounds[,1] = 0; /* lower bounds */
bounds[,2] = 3; /* upper bounds */
cntl = j(1,7,.);
cntl[1] = 1; /* minimize */
call lpsolve(rc, value, x, dual, redcost, c, A, b, cntl, ops, , bounds[,1], bounds[,2]);
print value x;
quit;

proc iml;
start euclid(x1, y1, x2, y2);
return( sqrt( (x1 - x2)##2 + (y1 - y2)##2 ) );
finish;

/* Define site names and coordinates */


names = {'A','B','C','D','E','F','G','H'};
coords = {
9.57 20.49, /* A */
7.69 7.13, /* B */
20.56 19.60, /* C */
20.44 12.47, /* D */
32.42 9.44, /* E */
25.55 2.14, /* F */
38.42 4.16, /* G */
38.24 20.31 /* H */
};

/* Total 8 sites */
n = nrow(coords);
P = j(n, n, 0); /* initialize transition matrix */

/* Define directed edges using indices: (from -> to) */


edges = {
4 3, /* D -> C */
4 5, /* D -> E */
5 6, /* E -> F */
5 7, /* E -> G */
6 7, /* F -> G */
7 8, /* G -> H */
3 1, /* C -> A */
3 8, /* C -> H */
2 3 /* B -> C */
};

/* Fill in P with inverse cost for each valid transition */


do i = 1 to nrow(edges);
from = edges[i,1];
to = edges[i,2];
dist = euclid(coords[from,1], coords[from,2], coords[to,1], coords[to,2]);
cost = 10 + 0.75 * dist;
P[from, to] = 1 / cost;
end;
/* Normalize rows so that each row sums to 1 */
rowSum = P[,+];
do i = 1 to n;
if rowSum[i] > 0 then
P[i,] = P[i,] / rowSum[i];
end;
/* Display transition matrix */
print P[rowname=names colname=names label="Transition Probability Matrix"];

proc iml;
start euclid(x1, y1, x2, y2);
return( sqrt( (x1 - x2)##2 + (y1 - y2)##2 ) );
finish;

/* Define site names and coordinates */


names = {'A','B','C','D','E','F','G','H'};
coords = {
9.57 20.49,
7.69 7.13,
20.56 19.60,
20.44 12.47,
32.42 9.44,
25.55 2.14,
38.42 4.16,
38.24 20.31
};
/* Initialize transition matrix */
n = nrow(coords);
P = j(n, n, 0);

/* Define directed edges: from -> to (1-based index) */


edges = {
4 3, /* D -> C */
4 5, /* D -> E */
5 6, /* E -> F */
5 7, /* E -> G */
6 7, /* F -> G */
7 8, /* G -> H */
3 1, /* C -> A */
3 8, /* C -> H */
2 3 /* B -> C */
};

/* Compute inverse cost as weights and normalize */


do i = 1 to nrow(edges);
from = edges[i,1];
to = edges[i,2];
dist = euclid(coords[from,1], coords[from,2], coords[to,1], coords[to,2]);
cost = 10 + 0.75 * dist;
P[from,to] = 1 / cost;
end;

rowSum = P[,+];
do i = 1 to n;
if rowSum[i] > 0 then
P[i,] = P[i,] / rowSum[i];
end;

print P[rowname=names colname=names label="2.f.1 Transition Probability Matrix"];

/* Question 2.f.2: Long-run probabilities */


evals = eigval(P`);
evecs = eigvec(P`);
idx = loc(abs(evals - 1) < 1e-6);
if ncol(idx) > 0 then do;
pi = evecs[,idx[1]]`;
pi = pi / sum(pi);
print pi[colname=names label="2.f.2 Long-run Visit Probabilities"];
end;
else
print "No steady-state distribution found.";
quit;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy