static AS3_Val getPrice(void* self, AS3_Val args)
{
    double N, T, S, K, v, r, price;
    AS3_ArrayValue( args, "DoubleType, DoubleType, DoubleType, DoubleType, DoubleType, DoubleType", &N, &T, &S, &K, &v, &r);
 
    double dt = T/N; //one time step
 
    double u = 1 + v*sqrt(dt); //up-tick
    double d = 1 - v*sqrt(dt); //down-tick
    double p = 0.5 + r*sqrt(dt)/(2*v); //risk-neutral probability of up-tick
    double df = 1/(1+r*dt); //discount factor over 1 time step, dt
 
    double* optionValues = malloc((N+1) * sizeof(double));
 
    //populate the tree (for N-steps there will be N+1 values)
    int i, j;
    double ST;
    for (i=0; i < N+1; i++) 
    {
        ST = S * pow(u, i) * pow(d, N-i);
        optionValues[i] = (ST > K)? ST - K : 0;
    }
 
    //now work backwards to get expected 
    //option value at each previous stage
    for (i=N; i >= 0; i--)
    {
        for(j=0; j<i; j++)
        {
            optionValues[j] = (p*optionValues[j+1] + (1-p)*optionValues[j])*df;
        }
    }
 
    price = optionValues[0];
    free(optionValues);
 
    return AS3_Number(price);
}
Exemplo n.º 2
0
/*
* Take the Lua stack item at index i and convert it into an
* ActionScript value.
*/
AS3_Val get_as3_value_from_lua_stack_type(lua_State * L, int i, int type)
{
  /* WARNING: Panic alert! Use L*_FN checkers here! */

  LCALL(L, stack);

  AS3_Val value;
  switch (type)
  {
    case LUA_TSTRING:  /* strings */
      {
        size_t length = 0;
        const char * str = lua_tolstring(L, i, &length);
        if (str == NULL)  /* NOTE: This is unreachable. Assert instead */
        {
          length = 6;
          str = "(null)";
        }
        /* NOTE: Alchemy .5a truncates embedded zeroes in string
        * regardless to the passed length
        */
        value = AS3_StringN(str, length);
      }
      break;

    case LUA_TBOOLEAN:  /* booleans */
      value = lua_toboolean(L, i) ? AS3_True() : AS3_False();
      break;

    case LUA_TNUMBER:  /* numbers */
      value = AS3_Number(lua_tonumber(L, i));
      break;

    case LUA_TNONE: /* fall through */
    case LUA_TNIL:  /* nil */
      value = AS3_Null();
      break;

    case LUA_TUSERDATA:  /* userdata */
      {
        void * userdata = lua_touserdata(L, i);
        lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_METATABLE);
        if (userdata == NULL || !lua_getmetatable(L, i))
        {
          lua_pop(L, 1); /* Pop AS3LUA_METATABLE */
          value = as3_value_from_foreign_userdata(L, i);
        }
        else if (!lua_rawequal(L, -2, -1))
        {
          lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */
          value = as3_value_from_foreign_userdata(L, i);
        }
        else
        {
          lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */
          AS3LuaUserData * userdata = (AS3LuaUserData *)lua_touserdata(L, i);
          value = userdata->value;
          /*
          * We just created one more reference to the AS3 value,
          * as it still lives inside Lua.
          * (And will probably be collected by GC.)
          */
          AS3_Acquire(value);
        }
      }
      break;

    case LUA_TFUNCTION: /* function */
      value = setup_callback(L, i);
      break;

    case LUA_TLIGHTUSERDATA: /* TODO: blackbox this type */
    case LUA_TTABLE: /* TODO: deal with this type */
    case LUA_TTHREAD: /* TODO: blackbox this type */
      value = AS3_String(lua_typename(L, type));
      break;

    default:  /* unreachable */
      fatal_error("unknown Lua type");
      break;
  }

#ifdef DO_SPAM
  SPAM(("get_as3_value_from_lua_stack(): end"));
  AS3_Trace(
      AS3_Call(
          getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", value)
        )
    );
#endif /* DO_SPAM */

  LCHECK_FN(L, stack, 0, fatal_error);

  return value;
}
static AS3_Val prediction(void* self, AS3_Val args) {
	AS3_Val	in_arr = AS3_Undefined(), out_arr = AS3_Array(0);
	float *tmp, d, e;
	int i,j,k,l, n,x,y, newx,newy, il, dx,dy;
	LAYER *pL;

	AS3_ArrayValue( args, "AS3ValType", &in_arr );

	for(i=0; i < 1024; ++i)
    	data[i] = AS3_IntValue(AS3_Get(in_arr, AS3_Int(4*i+1))) /255.0;
    	
    n = 1;
    x = 32;
    y = 32;
    
    #define DATA(l,j,i) data[((l)*y + (j))*x + (i)]
    #define O(k,dy,dx) o[((k)*newy + (dy))*newx + (dx)]
    #define W(k,l,j,i) pL->w[(((k)*n + (l))*pL->y + (j))*pL->x + (i)]
    
    for (il=0; il < nl; ++il) {
        flyield();
        pL = L+il;
        newx = x+1-pL->x;
        newy = y+1-pL->y;

        for (dx=0; dx < newx; ++dx)
        for (dy=0; dy < newy; ++dy)
        for (k=0; k < pL->n; ++k) {
            d = pL->b[k];
            for (l=0; l < n; ++l)
            for(j=0; j < pL->y; ++j)
            for(i=0; i < pL->x; ++i)
                d += DATA(l,j+dy,i+dx)*W(k,l,j,i);
            O(k,dy,dx) = d;
        }

        if(pL->maxpool) {
            for (k=0; k < pL->n; ++k)
            for (dx=0; dx < newx; dx+=2)
            for (dy=0; dy < newy; dy+=2) {
                d=O(k,dy,dx);
                e=O(k,dy,dx+1); if(e>d) d=e;
                e=O(k,dy+1,dx); if(e>d) d=e;
                e=O(k,dy+1,dx+1); if(e>d) d=e;
                O(k,dy/2,dx/2)=d;
            }
            newx /= 2;
            newy /= 2;
        }

        for (dx=0; dx < newx; ++dx)
        for (dy=0; dy < newy; ++dy) {
            e = 0;
            for (k=0; k < pL->n; ++k) {
                d = O(k,dy,dx);
                if(pL->nonlin==1) d=1.0/(1.0 + exp(-d));
                else if(pL->nonlin==2) d=tanh(d);
                else if(pL->nonlin==3) { d=exp(d); e += d; }
                O(k,dy,dx) = d;
            }
            if(pL->nonlin==3 && e)
            for (k=0; k < pL->n; ++k)
                O(k,dy,dx) /= e;
        }
        
        tmp = data;
        data = o;
        o = tmp;
        
        x = newx;
        y = newy;
        n = pL->n;
    }

	for(i=0; i < n*x*y; ++i)
        AS3_Set(out_arr, AS3_Int(i), AS3_Number(data[i]));

	return out_arr;
}
Exemplo n.º 4
0
/**
 * Scan in a wavetable. Wavetable should be at least one longer than the table size.
 */
static AS3_Val wavetableIn(void *self, AS3_Val args)
{
	AS3_Val settings;
	int bufferPosition; int channels; int frames;
	float *buffer; 
	int sourceBufferPosition;
	float *sourceBuffer;
	double phaseArg; float phase;
	double phaseAddArg; float phaseAdd;
	double phaseResetArg; float phaseReset;
	int tableSize;
	int count; 
	int intPhase;
	float *wavetablePosition;
	float *scratch;
	double y1Arg, y2Arg;
	float y1, y2;
	float fractional, fractionalIncrement, instantBend;
	
	
	
	AS3_ArrayValue(args, "IntType, IntType, IntType, IntType, AS3ValType", &bufferPosition, &sourceBufferPosition, &channels, &frames, &settings);
	AS3_ObjectValue(settings, "tableSize:IntType, phase:DoubleType, phaseAdd:DoubleType, phaseReset:DoubleType, y1:DoubleType, y2:DoubleType",
		&tableSize, &phaseArg, &phaseAddArg, &phaseResetArg, &y1Arg, &y2Arg);

	buffer = (float *) bufferPosition;
	sourceBuffer = (float *) sourceBufferPosition; 
	phaseAdd = (float) phaseAddArg * tableSize; // num source frames to add per output frames
	phase = (float) phaseArg * tableSize; // translate into a frame count into the table
	phaseReset = (float) phaseResetArg * tableSize;
	y1 = (float) y1Arg;
	y2 = (float) y2Arg;
	
	// Expand the pitch modulation into scratch
	// expandLine(scratch1, y1, y2, frames); // draws spline segment into scratch1
	// scratch = (float *) scratch1;	
		
	// Make sure we got everything right
	//sprintf(trace, "Wavetable size=%d phase=%f phaseAdd=%f y1=%f y2=%f", tableSize, phase, phaseAdd, y1, y2);
	//sztrace(trace);	
				
	count=frames;
	fractional = 0.0;
	fractionalIncrement = 1 / (float) frames;
	
	if (channels == 1) {
		while (count--) {
			while (phase >= tableSize) {
				if (phaseReset == -1) {
					// no looping!
					return 0; 
				} else {
					// wrap phase to the loop point
					phase -= tableSize; 
					phase += phaseReset;
				}
			}
			intPhase = (int) phase; // int phase
			wavetablePosition = sourceBuffer + intPhase;
			*buffer++ = interpolate(*wavetablePosition, *(wavetablePosition+1), phase - intPhase);
			// Increment phase by adjusting phaseAdd for instantaneous pitch bend 
			instantBend = interpolate(y1, y2, fractional);
			fractional += fractionalIncrement;
			phase +=  phaseAdd * shiftToFreq(instantBend); 
		}		
	} else if (channels == 2 ) {
		while (count--) {
			while (phase >= tableSize) {
				if (phaseReset == -1) {
					// no looping!
					return 0; 
				} else {
					// wrap phase to the loop point
					phase -= tableSize; 
					phase += phaseReset;
				}
			}
			intPhase = ((int)(phase*0.5))*2; // int phase, round to even frames, for each stereo frame pair
			wavetablePosition = sourceBuffer + intPhase;
			*buffer++ = interpolate(*wavetablePosition, *(wavetablePosition+2), phase - intPhase);
			*buffer++ = interpolate(*(wavetablePosition+1), *(wavetablePosition+3), phase - intPhase);
			// Increment phase by adjusting phaseAdd for instantaneous pitch bend 
			instantBend = interpolate(y1, y2, fractional);
			fractional += fractionalIncrement;
			phase +=  phaseAdd * shiftToFreq(instantBend);
		}
	}
	
	// Scale back down to a factor, and write the final phase value back to AS3
	phase /= tableSize;
	AS3_Set(settings, AS3_String("phase"), AS3_Number(phase));
	
	return 0;
}