Error NETLOGO trying to compare 30 lists and make a edge if correlation is greater than 0.5
showing error at foreach stock-prices [prices1 i] saying expecting anonymus command rather than list,block
stock-prices ; A list of lists, where each sublist contains the price data for one stock
correlations ; A matrix to store correlation coefficients
<code>
to compute-correlations
; Initialize the correlations matrix with zeros
set correlations (list (map [ -> n-values 30 [ 0 ] ] range 30))
; Compute the correlation for each pair of stocks
foreach stock-prices [prices1 i] [
foreach stock-prices [prices2 j] [
if i != j [
let corr correlation-coefficient prices1 prices2
set correlations replace-item i correlations replace-item j (item j (item i correlations)) corr
]
]
]
end
to draw-edges
ask turtles [
let my-id stock-id
ask turtles with [stock-id > my-id] [
if item stock-id (item my-id correlations) > 0.5 [
create-link-with myself
]
]
]
end
to-report correlation-coefficient [prices1 prices2]
let n length prices1
let mean1 mean prices1
let mean2 mean prices2
let stddev1 standard-deviation prices1
let stddev2 standard-deviation prices2
let covariance mean (map [ (item ? prices1 - mean1) * (item ? prices2 - mean2) ] n-values n [ ? ])
report covariance / (stddev1 * stddev2)
end
</code>
<code>
to compute-correlations
; Initialize the correlations matrix with zeros
set correlations (list (map [ -> n-values 30 [ 0 ] ] range 30))
; Compute the correlation for each pair of stocks
foreach stock-prices [prices1 i] [
foreach stock-prices [prices2 j] [
if i != j [
let corr correlation-coefficient prices1 prices2
set correlations replace-item i correlations replace-item j (item j (item i correlations)) corr
]
]
]
end
to draw-edges
ask turtles [
let my-id stock-id
ask turtles with [stock-id > my-id] [
if item stock-id (item my-id correlations) > 0.5 [
create-link-with myself
]
]
]
end
to-report correlation-coefficient [prices1 prices2]
let n length prices1
let mean1 mean prices1
let mean2 mean prices2
let stddev1 standard-deviation prices1
let stddev2 standard-deviation prices2
let covariance mean (map [ (item ? prices1 - mean1) * (item ? prices2 - mean2) ] n-values n [ ? ])
report covariance / (stddev1 * stddev2)
end
</code>
to compute-correlations
; Initialize the correlations matrix with zeros
set correlations (list (map [ -> n-values 30 [ 0 ] ] range 30))
; Compute the correlation for each pair of stocks
foreach stock-prices [prices1 i] [
foreach stock-prices [prices2 j] [
if i != j [
let corr correlation-coefficient prices1 prices2
set correlations replace-item i correlations replace-item j (item j (item i correlations)) corr
]
]
]
end
to draw-edges
ask turtles [
let my-id stock-id
ask turtles with [stock-id > my-id] [
if item stock-id (item my-id correlations) > 0.5 [
create-link-with myself
]
]
]
end
to-report correlation-coefficient [prices1 prices2]
let n length prices1
let mean1 mean prices1
let mean2 mean prices2
let stddev1 standard-deviation prices1
let stddev2 standard-deviation prices2
let covariance mean (map [ (item ? prices1 - mean1) * (item ? prices2 - mean2) ] n-values n [ ? ])
report covariance / (stddev1 * stddev2)
end
New contributor
Rakesh Kumar Behera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.