You are on page 1of 6

Financial Analytics CA -2

Name :YUKTHASHREE
G
USN- 19BSR18029
BSc DSA , 6th sem

1.OPTIMIZATIO
N USING R

# using lpSolve
require(lpSolve)

# Set the coefficients of the decision variables


a <- c(38, 48, 78)
# Create constraint matrix c
b <- matrix(c(1, 1, -10,
4, 3, -20,
1, 0, -2,
1, 1, 0), nrow=4, byrow=TRUE)
# Right hand side for the constraints
c <- c(500, 200, 100, 2500)
# Direction of the constraints
direction<-c("<=", "<=", "<=", ">=")

# Find the optimal solution


optimum <- lp(direction="min",
objective.in = a,
const.mat = b,
const.dir = direction,
const.rhs = c,
all.int = T)
# Print status: 0 = success, 2 = no feasible solution
print(optimum$status)
# Display the optimum values for x_5p, x_6p and x_w
best_sol <- optimum$solution
names(best_sol) <- c("x_5p", "x_6p", "x_w")
print(best_sol)
# Check the value of objective function at optimal point
print(paste("Total cost: ", optimum$objval, sep=""))
rm(optimum, direction, best_sol)

# Let's try to solve the problem again using lpSolveAPI


# Use lpSolveAPI
require(lpSolveAPI
# Set 4 constraints and 3 decision variables
lprec <- make.lp(nrow = 4, ncol = 3)
# Set the type of problem we are trying to solve
lp.control(lprec, sense="min")
# Set type of decision variables
set.type(lprec, 1:3, type=c("integer"))
# Set objective function coefficients vector C
set.objfn(lprec, a)

# Add constraints
add.constraint(lprec, b[1, ], "<=", c[1])
add.constraint(lprec, b[2, ], "<=", c[2])
add.constraint(lprec, b[3, ], "<=", c[3])
add.constraint(lprec, b[4, ], ">=", c[4])

# Display the LPsolve matrix


lprec
# Solve problem
solve(lprec)
# Get the decision variables values
get.variables(lprec)
# Get the value of the objective function
get.objective(lprec)

2. QUANTITATIVE RISK MANAGEMENT

sp500

B)The sp500 is a stock market index that tracks the stocks of 500 large-cap companies. It represents the stock
market's performance by reporting the risks and returns of the biggest companies. Investors use it as the benchmark
of the overall market, to which all other investments are compared.
S&P stands for Standard and Poor, the names of the two founding financial companies.

A) #exploring sp500 stock indices


#install.packages("qrmdata")
library(qrmdata)
data("SP500")
head(SP500)
tail(SP500)
C) plot(SP500)
D) # 4 induvidual
constituents
data("SP500_const")
# Apply names() and head() to DJ_const
names(SP500_const)
head(SP500_const)
# Extract mmm abt abbv acn in 2008-09 and assign to stocks
stocks <- SP500_const["2008/2009", c("MMM", "ABT","ATVI","ACN")]
# Plot stocks with plot
plot(stocks$MMM)
plot(stocks$ABT)
plot(stocks$ATVI)
plot(stocks$ACN)

E) # log return of stock index


logreturn<-diff(log(SP500))[-1]
head(logreturn)
logreturn1<-diff(log(SP500_const$MMM))
head(logreturn1)
logreturn2<-diff(log(SP500_const$ABT))
head(logreturn2)
As the code is successfully run by installing all the
required packages and giving a perfect input
We acquired the output successfully.

You might also like