def test_SA(): m = Markovian(f, n_Markov_states, 5) n = Markovian(g, n_Markov_states, 5) m.SA() n.SA()
def generator_augmented(random_state, size): # (r_it, r_Mt, epsilon_Mt, sigma^2_Mt) process = generator(random_state, size) market_return = process[:,:,0] process_aug = numpy.concatenate( (beta*(market_return[:,:,numpy.newaxis]-rf) + rf,process), axis=-1, ) return process_aug # Markov chain discretization sample_paths = generator(numpy.random.RandomState(0),size=1000) return_sample_paths = sample_paths[:,:,0] var_sample_paths = sample_paths[:,:,2] price_sample_paths = numpy.cumprod(numpy.exp(return_sample_paths),axis=1) markovian = Markovian(generator,n_Markov_states=[1]+[100]*(T-1),n_sample_paths=1000000) markovian.SA() # augment to 103 dimension Markov_states = [None for _ in range(T)] transition_matrix = markovian.transition_matrix for t in range(T): market_return = markovian.Markov_states[t][:,0].reshape(-1,1) asset_return_market_exposure = beta*(market_return-rf) + rf Markov_states[t] = numpy.concatenate( (asset_return_market_exposure,markovian.Markov_states[t]), axis=1) # comparison of the true process vs the Markov chain approximation fig, ax = plt.subplots(1,2,figsize=(10,5),sharey=True) fig = fan_plot(return_sample_paths, ax=ax[0]) s = markovian.simulate(1000) fig = fan_plot(s[:,:,-3], ax=ax[1]) ax[0].set_xlabel("stages") ax[0].set_ylabel("returns")