www.pudn.com > asm_java.zip > AsmModel.java


package asm;
import java.util.Vector;
/**
 * Title:        Artificial Stock Market
 * Description:  人工模拟股市(来源:SFI的Swarm版本)的Java版本
 * Copyright:    Copyright (c) 2003
 * Company:      http://agents.yeah.net
 * @author jake
 * @version 1.0
 */

//该类主要负责股市的整体运行仿真
public class AsmModel {
  int modelTime;    /*股市的当前的仿真周期*/

  Vector agentList;       /*Agents的一个集合*/
  Specialist specialist=new Specialist();      /*市场专家,负责市场的出清*/
  Dividend dividendProcess; /*一个随机过程模型,产生股息流*/
  World world;          /*世界模型,包括对当前股市状态的编码信息、价格、股息等参数的存储*/
  static asm localasm;  //指向主程序的指针
  AgentParam bfParams;  /*Agent的参数集合*/
  ASMParam asmModelParams;  //股市的参数集合
  public AsmModel(asm local) {
    //初始化人工股市
    modelTime=0;
    asmModelParams=local.ASMParams;
    bfParams=local.AgentParams;
    agentList=new Vector();
    localasm=local;
  }
int getNumBFagents(){
  //得到agent的数目
  return asmModelParams.numBFagents;
}

/*" Returns the initialcash value, which is held in asmModelParams"*/
double getInitialCash()
{
  return asmModelParams.initialcash;
}
public void OneStep(){
  //股市的一步仿真

  //从随机过程股息流里面产生一期的股息
  periodStepDividend();

  //让每个agent进行赋税
  for(int i=0;i 1.0)
    amplitude = 1.0;
  amplitude = 0.0001*(int)(10000.0*amplitude);
  return amplitude;
}

/*设置周期参数,最小不能小于2*/
int setPeriod(int thePeriod){
  period = thePeriod;
  if (period < 2)
    period = 2;
  return period;
}


void setDerivedParams()
/*设置各种参数*/
{
  //deviation是偏离度,没有用到这个参数
  deviation = baseline*amplitude;

  //rho是运行周期的倒数
  rho =Math.exp(-1.0/((double)period));
  rho = 0.0001*(int)(10000.0*rho);
  gauss = deviation*Math.sqrt(1.0-rho*rho);
  //产生股息
  dvdnd = baseline + gauss*normal(1000);
}

/*" 返回股息的数值.  这是核心的方法
  It does NOT use
  the global time, but simply assumes that one period passes between
  each call.  Note that "time" may not be the same as the global
  variable "t" because shifts are introduced to maintain phase when
  certain parameters are changed."*/

double dividend(){
  //pj:
  // dvdnd = baseline + rho*(dvdnd - baseline) + gauss*normal();
  //产生一个AR(1)过程
    dvdnd = baseline + rho*(dvdnd - baseline) + gauss*normal(1000);

  //限制股息的数值不能超界
  if (dvdnd < mindividend)
    dvdnd = mindividend;
  if (dvdnd > maxdividend)
    dvdnd = maxdividend;
  //dvdnd=baseline;
  return dvdnd;
}
double normal(int n){
  //产生一个正态分布的随机数
  double s=0;
  for(int i=0;i= -minexcess)
		{
		  done = true;
		  continue;
		}
	      // Update price using demand curve slope information
	      if (slopetotal != 0)
		trialprice += imbalance/(10+slopetotal);
	      else
		trialprice *= 1 + eta*imbalance;
	    }
	  break;

	case 2:
	  //人工神经网络agent
	  if (mcount == 0){
	      trialprice = worldForSpec.getPrice();
	    }
	  else{
	      trialprice = (worldForSpec.getPrice())*(1.0 + eta*(bidtotal-offertotal));
	      done = true;	// Two passes
	    }
	  break;
	case 3:
            //直接根据预期计算出股票的需求量
            //因为市场出清要求:sigma(x)=N(初始时刻全部的股票量)
            //而x=(E(p+d)-(trialprice+dividend))/lamda*dev^2
            //所以可以直接计算出trialprice的价格
            int size=agentList.size();
            double sigmac,sigmab,sigmaa;
            sigmac=sigmab=sigmaa=0;
            for(int i=0;i maxprice)
	trialprice = maxprice;

      //得到每个agent的需求和边际需求
      bidtotal = 0.0;
      offertotal = 0.0;
      slopetotal = 0.0;
      int size=agentList.size();//index = [agentList begin: [self getZone]];
      for(int i=0;i 0.0)
	    bidtotal += demand;
	  else if (demand < 0.0)
	    offertotal -= demand;
	}

      // 计算交易量
      if(bidtotal>offertotal)volume = offertotal;
      else volume=bidtotal;
      if(bidtotal>0.0)bidfrac = volume / bidtotal;
      else volume=0.0;
      if(offertotal>0.0)offerfrac = volume / offertotal;
      else offerfrac=0.0;
    }

  return trialprice;
}

void completeTrades(Vector agentList,World worldForSpec)
/*完成交易,更新每个agent的现金量、持股量、总体财富
"Updates the agents cash and position to consummate the trades
  previously negotiated in -performTrading, with rationing if
  necessary.

 * Makes the actual trades at the last trial price (which is now the
 * market price), by adjusting the agents' holdings and cash.  The
 * actual purchase/sale my be less than that requested if rationing
 * is imposed by the specialist -- usually one of "bidfrac" and
 * "offerfrac" will be less than 1.0.
 *
 * This could easiliy be done by the agents themselves, but we let
 * the specialist do it for efficiency.
 "*/
{
  Agent agent;
  double bfp, ofp, tp, profitperunit;
  double price = 0.0; //pj: was IVAR


  price = worldForSpec.getPrice();
  //获得每支股票的利润
  profitperunit = worldForSpec.getProfitPerUnit();

// Intermediates, for speed
  //
  bfp = bidfrac*price;
  ofp = offerfrac*price;
  tp = taupnew*profitperunit;

  int size=agentList.size();
  for(int i=0;i 0.0)
	{
	  agent1.position += agent1.demand*bidfrac;
	  agent1.cash-=agent1.demand*bfp;
	}
      else if (agent1.demand < 0.0) {
	  agent1.position += agent1.demand*offerfrac;
	  agent1.cash-= agent1.demand*ofp;
	}
    }

}

}