text: # -*- coding: utf-8 -*-
  """
  Created on Tue Dec 10 15:09:28 2019
  
  @author: mateo
  """
  
  import HARK.ConsumptionSaving.ConsPortfolioModel as cpm
  import matplotlib.pyplot as plt
  
  # %% Set up figure path
  import sys,os
  
  # Determine if this is being run as a standalone script
  if __name__ == '__main__':
      # Running as a script
      my_file_path = os.path.abspath("../")
  else:
      # Running from do_ALL
      my_file_path = os.path.dirname(os.path.abspath("do_ALL.py"))
      my_file_path = os.path.join(my_file_path,"Code/Python/")
      
  FigPath = os.path.join(my_file_path,"Figures/")
  
  # %% Calibration and solution
  sys.path.append(my_file_path) 
  # Loading the parameters from the ../Code/Calibration/params.py script
  from Calibration.params import dict_portfolio, time_params
  
  agent = cpm.PortfolioConsumerType(**dict_portfolio)
  agent.solve()
  
  
  
  # %% A Simulation
  # Set up simulation parameters
  
  # Number of agents and periods in the simulation.
  
  # Number of instances of the class to be simulated.
  # (a few, so that its easy to visualize)
  agent.AgentCount = 5
  
  # Number of periods to be simulated
  agent.T_sim = agent.T_cycle
  
  # Set up the variables we want to keep track of.
  agent.track_vars = ['aNrmNow','cNrmNow', 'pLvlNow', 't_age', 'ShareNow','mNrmNow']
  
  # Run the simulations
  agent.initializeSim()
  agent.simulate()
  
  # Present diagnostic plots.
  plt.figure()
  plt.plot(agent.t_age_hist+time_params['Age_born'], agent.pLvlNow_hist,'.')
  plt.xlabel('Age')
  plt.ylabel('Permanent income')
  plt.title('Simulated Income Paths')
  plt.grid()
  
  # Save figure
  figname = 'Y_Sim'
  plt.savefig(os.path.join(FigPath, figname + '.png'))
  plt.savefig(os.path.join(FigPath, figname + '.jpg'))
  plt.savefig(os.path.join(FigPath, figname + '.pdf'))
  plt.savefig(os.path.join(FigPath, figname + '.svg'))
  
  plt.ioff()
  plt.draw()
  plt.pause(1)
  
  plt.figure()
  plt.plot(agent.t_age_hist+time_params['Age_born'], agent.ShareNow_hist,'.')
  plt.xlabel('Age')
  plt.ylabel('Risky share')
  plt.title('Simulated Risky Portfolio Shares')
  plt.grid()
  
  # Save figure
  figname = 'RShare_Sim'
  plt.savefig(os.path.join(FigPath, figname + '.png'))
  plt.savefig(os.path.join(FigPath, figname + '.jpg'))
  plt.savefig(os.path.join(FigPath, figname + '.pdf'))
  plt.savefig(os.path.join(FigPath, figname + '.svg'))
  
  plt.ioff()
  plt.draw()
  plt.pause(1)