Code Monkey home page Code Monkey logo

Comments (9)

nassuphis avatar nassuphis commented on July 30, 2024

removed this funciton, no more tSNE in the report unless people want to see it

plot_tsne_grid<-function(fn,tsne,pictures){
  s<-(floor(sqrt(length(pictures)))+1)^2
  k<-sqrt(s)
  m<-diag(k)
  gx<-as.vector(col(m))
  gy<-as.vector(row(m))
  tx<-rescale(tsne$Y[,1],range(gx))
  ty<-rescale(tsne$Y[,2],range(gy))
  tsnex<-matrix(tx,ncol=1)[,rep(1,length(gx))]
  tsney<-matrix(ty,ncol=1)[,rep(1,length(gy))]
  gridx<-matrix(gx,nrow=1)[rep(1,length(tx)),]
  gridy<-matrix(gy,nrow=1)[rep(1,length(ty)),]
  dx<-(tsnex-gridx)^2
  dy<-(tsney-gridy)^2
  d<-dx+dy
  t2g <- solve_LSAP(d)
  par(mai=c(0,0,0,0))
  plot(gx,gy,col="white",axes=FALSE,xlab="",ylab="",main="")
  for(i in seq_along(pictures))rasterImage(
    image=pictures[[i]][[fn]],
    xleft=gx[t2g[i]]-0.49,
    ybottom=gy[t2g[i]]-0.49,
    xright=gx[t2g[i]]+0.49,
    ytop=gy[t2g[i]]+0.49,
    interpolate=TRUE
  )
  abline(
    h=setdiff(unique(gx),range(gx)[1])-0.5,
    v=setdiff(unique(gy),range(gy)[1])-0.5,
    col=rgb(0.5,0.5,0.5,0.5)
  )
}

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

pair_risk_contribution plots:

  1. DUKE managers
  2. DUKE manager risk
  3. DUKE manager risk contributions
  4. DUKE manager correlations
  5. DUKE manager factor exposures
  6. DUKE pairs gros s vs gross rank
  7. DUKE pair position as multiple of ADV
  8. pair risk stdev vs pair rank
  9. pair risk vs pair gross
  10. marginal pair sdev vs pair
    11 histogram of daily pair correlation

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

actual size vs size required for 4% volatility is a metric we care about.
marginal risk contribution can be computed using FRAPO
volatility trajectories can be computed by manager and by pair.

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

the "Manager Factor Exposures" section needs to stay, but the contents is not good.

the "split plots" are pretty useless:

image

split_plot<-function(
  x,
  f,
  factor_state_fun=sign,
  state_col=rainbow(length(all_states),alpha=0.5),
  cex=2
){
  
  all_states<-sort(unique(factor_state_fun(f)))
  #state_col<-rainbow(length(all_states),alpha=0.5)
  states<-match(factor_state_fun(f),all_states)
  the_split<-data.table(
    state=states,
    value=x,
    factor=f,
    x=do.call(c,split(seq_along(states),states)),
    y=do.call(c,mapply(cumsum,split(x,states),SIMPLIFY=FALSE)),
    col=do.call(c,split(state_col[states],states))
  )
  par(mai=c(0.1,0.1,0.1,0.1))
  plot(
    x=the_split$x,
    y=the_split$y,
    col=the_split$col,
    pch=19,
    cex=cex,
    axes=FALSE,
    xlab="",
    ylab=""
  )
  par(mai=c(1.02,0.82,0.82,0.42))
  the_split
}


pms<-colnames(manager_local_pnl)
fs<-c(
  "SMX Index","UKX Index","MCX Index",
  "SXXP Index",
  "MSEEMOMO Index","MSEEGRW Index","MSEEVAL Index",
  "USO US Equity","EEM US Equity","TLT US Equity",
  "COINXBE SS Equity"
)
pic_w<-paste0(round(18/(length(pms)+1),digits=1),"cm")
pic_h<-paste0(round(21/length(fs),digits=1),"cm")
log_code(split_pics<-data.table(
    factors=sub("( Index$)|( Equity$)","",fs),
    t(structure(outer(pms,fs,FUN=Vectorize(function(pm,fac){
      res<-make_plot(
        x0<-split_plot(
          manager_local_pnl[,pm],
          factor_local_tret[,fac],
          state_col=c(rgb(1,0,0,0.5),rgb(0.2,0.2,0.2,0.5),rgb(0,1,0,0.5))
        ),
        width=pic_w,
        height=pic_h
      )
      res
    })),dimnames=list(pms,gsub(" Index$","",fs))))
))
split_align=paste0("m{",pic_w,"}")

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

we should have tables and basis points

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

the key theme of the report should be pair diversification, which can be shown using the vol trajectories and factor exposures

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

vol trajectory code, should be a function, its useful

# vol trajectories are computed by taking costituents largest vol first
tri<-function(n,d=1,s=1)(s*row(diag(n))<s*col(diag(n)))+d*diag(n)
log_code(vols<-weights*(cbind(apply(x,2,sd))[,rep(1,ncol(weights))]))
log_code(vol_ord<-structure(apply(-vols,2,.%>%rank(ties="f")),dimnames=dimnames(vols)))
log_code(res<-mapply(function(ptf){
  a1<-(weights[,rep(ptf,nrow(weights))]*diag(nrow(vols))[vol_ord[,ptf],]%*%tri(nrow(vols)))
  a2<-total_gross*a1%*%diag(1/colSums(a1))
  apply(x%*%a2,2,sd)
},ptf=colnames(vol_ord))%>%
{ rownames(.)<-paste0("vol_rank_",1:nrow(.)); . })

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024
volatility_trajectory<-function(
  returns,
  ptf
){
  vol=ptf*apply(returns[,names(ptf)],2,sd)
  vol_rank=rank(vol,ties.method="first")
  weight_matrix <- cbind(ptf=ptf)[,rep(1,length(ptf))]
  mask_matrix <- diag(length(vol))[vol_rank,] %*% tri(length(vol))
  trajectory_matrix <- weight_matrix * mask_matrix
  normalized_trajectory_matrix <- trajectory_matrix %*% diag(sum(ptf)/colSums(trajectory_matrix))
  apply(returns%*%normalized_trajectory_matrix,2,sd)
}

from pairs.

nassuphis avatar nassuphis commented on July 30, 2024

the current report
pair_risk_contribution.pdf

from pairs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.