-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjective_functions.R
More file actions
45 lines (37 loc) · 1.13 KB
/
objective_functions.R
File metadata and controls
45 lines (37 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Read in data for eval function
df = read.csv("processed_financials.csv")
#minimise price per earnings
price_earnings_ratio = function(data, weightings){
total_sum = 0
for (w in weightings) {
if (w > 0) {
sector = as.double(which(weightings == w))
sector_data = data[data$sector_index == sector,]
price_earnings = sector_data$Price.Earnings
total_sum = total_sum + (sum(price_earnings) * w)
}
}
return(total_sum)
}
value_at_risk = function(data, weightings) {
VaR = 0
for (w in weightings) {
if (w > 0) {
sector = as.double(which(weightings == w))
sector_data = data[data$sector_index == sector,]
week_high = sector_data$X52.Week.High
week_low = sector_data$X52.Week.Low
price_variance = sum(week_low) - sum(week_high)
VaR = VaR + (price_variance * w)
}
}
return(VaR)
}
# Kinda works, might need more tweaking
budget_constraint = function(x) {
return(c(sum(x) - 100))
}
budget_new = function(x) { 100 - sum(x) }
eval = function(x) {
return (c(-price_earnings_ratio(df, x), value_at_risk(df, x))) # minimising cost per earnings and value at risk
}