#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue May 21 17:36:58 2024 @author: jdaniel """ import numpy as np from matplotlib import pyplot as plt def AltMarket(num_samples,nummarkets,varmarket,variances): num_vals = num_samples var = varmarket nreg = len(variances[:,0]) ndim = nummarkets a = np.zeros([ndim,ndim]) rans = np.random.randn(num_vals,ndim) c = np.copy(rans) c = np.zeros((num_vals,nreg,ndim)) for i in range(0,ndim): for j in range(0,ndim): a[i,j] = -var/(float(ndim)-1) a[i,i] = var*1.00001 b = np.linalg.cholesky(a) for i in range(0,num_vals): for j in range(0,nreg): c[i,j,:] = np.matmul(b,rans[i,:]) return(c) def AltMarket_FractionMethod(num_samples,nummarkets,varmarket,variances): num_vals = num_samples var = varmarket ndim = nummarkets nreg = len(variances[:,0]) ndimactual = len(variances[0,:]) rans = np.random.randn(num_vals,nreg,ndim) rans = np.random.randn(num_vals,ndim) c = np.zeros((num_vals,nreg,ndim)) for i in range(0,num_vals): for j in range(0,nreg): if (ndim == ndimactual): #c[i,j,:] = rans[i,:] * .4*variances[j,k] for k in range(0,ndim): # c[i,j,k] = rans[i,k] * .5*variances[j,k] c[i,j,k] = rans[i,k] * np.max([.2*variances[j,k],.05]) else: for k in range(3,ndim-1): c[i,j,k] = rans[i,k] * np.max([.2*variances[j,k],.05]) return(c) def marketmc(ranArr,marketArr): nsample = len(ranArr[:,0,0]) nran = len(ranArr[0,0,:]) nregion = len(marketArr[:,0]) nmarket = len(marketArr[0,:]) marketOut = np.zeros([nsample,nregion,nmarket]) marketOut[0,:,:] = marketArr[:,:] for i in range(1,nsample): for j in range(0,nregion): marketOut[i,j,:] = marketArr[j,:] + ranArr[i,j,:] kk = np.where(marketOut[i,j,:] > 1) ii = np.where(marketOut[i,j,:] < 0) jj = np.where(marketOut[i,j,:] >= 0) ishape = np.shape(ii) if (ishape[1] != 0): marketOut[i,j,ii] = 0. sumval = np.sum(marketOut[i,j,:]) marketOut[i,j,jj] = marketOut[i,j,jj] / sumval return(marketOut)