Skip to content

Commit

Permalink
Removed Lift element.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbywater committed Sep 20, 2017
1 parent 6972fc9 commit c110232
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 141 deletions.
40 changes: 17 additions & 23 deletions README.md
Expand Up @@ -412,12 +412,6 @@ assert results.fair_value.mean() > 3, results.fair_value.mean()
```


### Lift

The examples below use the `Lift` element to specify deltas with respect to each market and each period (e.g.
daily, monthly, or yearly) across the term of the contract.


## Examples of usage

The examples below use the library function `calc_print_plot()` to evaluate contracts, and print and plot results.
Expand Down Expand Up @@ -446,23 +440,23 @@ results = calc_print_plot(
title="Gas Storage",

source_code="""
def GasStorage(start, end, commodity_name, quantity, target, limit, step, period):
def GasStorage(start, end, commodity_name, quantity, target, limit, step):
if ((start < end) and (limit > 0)):
if quantity <= 0:
Wait(start, Choice(
Continue(start, end, commodity_name, quantity, target, limit, step, period),
Inject(start, end, commodity_name, quantity, target, limit, step, period, 1),
Continue(start, end, commodity_name, quantity, target, limit, step),
Inject(start, end, commodity_name, quantity, target, limit, step, 1),
))
elif quantity >= limit:
Wait(start, Choice(
Continue(start, end, commodity_name, quantity, target, limit, step, period),
Inject(start, end, commodity_name, quantity, target, limit, step, period, -1),
Continue(start, end, commodity_name, quantity, target, limit, step),
Inject(start, end, commodity_name, quantity, target, limit, step, -1),
))
else:
Wait(start, Choice(
Continue(start, end, commodity_name, quantity, target, limit, step, period),
Inject(start, end, commodity_name, quantity, target, limit, step, period, 1),
Inject(start, end, commodity_name, quantity, target, limit, step, period, -1),
Continue(start, end, commodity_name, quantity, target, limit, step),
Inject(start, end, commodity_name, quantity, target, limit, step, 1),
Inject(start, end, commodity_name, quantity, target, limit, step, -1),
))
else:
if target < 0 or target == quantity:
Expand All @@ -472,22 +466,22 @@ def GasStorage(start, end, commodity_name, quantity, target, limit, step, period
@inline
def Continue(start, end, commodity_name, quantity, target, limit, step, period):
GasStorage(start + step, end, commodity_name, quantity, target, limit, step, period)
def Continue(start, end, commodity_name, quantity, target, limit, step):
GasStorage(start + step, end, commodity_name, quantity, target, limit, step)
@inline
def Inject(start, end, commodity_name, quantity, target, limit, step, period, vol):
Continue(start, end, commodity_name, quantity + vol, target, limit, step, period) - \
vol * Lift(commodity_name, period, Market(commodity_name))
def Inject(start, end, commodity_name, quantity, target, limit, step, vol):
Continue(start, end, commodity_name, quantity + vol, target, limit, step) - \
vol * Market(commodity_name)
@inline
def BreachOfContract():
-10000000000000000
GasStorage(Date('2011-6-1'), Date('2011-12-1'), 'GAS', 0, 0, 50000, TimeDelta('1m'), 'monthly')
GasStorage(Date('2011-6-1'), Date('2011-12-1'), 'GAS', 0, 0, 50000, TimeDelta('1m'))
""",

observation_date='2011-1-1',
Expand Down Expand Up @@ -593,7 +587,7 @@ Below is a copy of the Quant DSL source code for the library's power plant model
in the example above.

```python
from quantdsl.semantics import Choice, Lift, Market, TimeDelta, Wait, inline
from quantdsl.semantics import Choice, Market, TimeDelta, Wait, inline


def PowerPlant(start, end, duration_off):
Expand Down Expand Up @@ -624,12 +618,12 @@ def ProfitFromRunning(duration_off):

@inline
def Power():
Lift('POWER', 'daily', Market('POWER'))
Market('POWER')


@inline
def Gas():
Lift('GAS', 'daily', Market('GAS'))
Market('GAS')


@inline
Expand Down
18 changes: 9 additions & 9 deletions quantdsl/lib/powerplant1.py
@@ -1,14 +1,14 @@
from quantdsl.semantics import Add, Choice, Fixing, Lift, Market, Min, Mult, Wait, inline
from quantdsl.semantics import Add, Choice, Fixing, Market, Min, Mult, Wait, inline


def PowerPlant(start, end, commodity, cold, step, periodisation):
def PowerPlant(start, end, commodity, cold, step):
if (start < end):
Wait(start, Choice(
Add(
PowerPlant(start + step, end, commodity, Running(), step, periodisation),
ProfitFromRunning(start, commodity, periodisation, cold)
PowerPlant(start + step, end, commodity, Running(), step),
ProfitFromRunning(start, commodity, cold)
),
PowerPlant(start + step, end, commodity, Stopped(cold), step, periodisation),
PowerPlant(start + step, end, commodity, Stopped(cold), step),
))
else:
return 0
Expand All @@ -25,10 +25,10 @@ def Stopped(cold):


@inline
def ProfitFromRunning(start, commodity, periodisation, cold):
return Mult((1 - cold / 10), Fixing(start, Burn(commodity, periodisation)))
def ProfitFromRunning(start, commodity, cold):
return Mult((1 - cold / 10), Fixing(start, Burn(commodity)))


@inline
def Burn(commodity, periodisation):
return Lift(commodity, periodisation, Market(commodity))
def Burn(commodity):
return Market(commodity)
6 changes: 3 additions & 3 deletions quantdsl/lib/powerplant2.py
@@ -1,4 +1,4 @@
from quantdsl.semantics import Choice, Lift, Market, TimeDelta, Wait, inline
from quantdsl.semantics import Choice, Market, TimeDelta, Wait, inline


def PowerPlant(start, end, duration_off):
Expand Down Expand Up @@ -29,12 +29,12 @@ def ProfitFromRunning(duration_off):

@inline
def Power():
Lift('POWER', 'daily', Market('POWER'))
Market('POWER')


@inline
def Gas():
Lift('GAS', 'daily', Market('GAS'))
Market('GAS')


@inline
Expand Down
33 changes: 17 additions & 16 deletions quantdsl/lib/storage2.py
@@ -1,41 +1,42 @@
from quantdsl.semantics import Wait, Choice, inline, Market, Lift
from quantdsl.semantics import Choice, Market, Wait, inline


def GasStorage(start, end, commodity_name, quantity, target, limit, step, period):
def GasStorage(start, end, commodity_name, quantity, target, limit, step):
if ((start < end) and (limit > 0)):
if quantity <= 0:
return Wait(start, Choice(
Continue(start, end, commodity_name, quantity, limit, step, period, target),
Inject(start, end, commodity_name, quantity, limit, step, period, target, 1),
Continue(start, end, commodity_name, quantity, limit, step, target),
Inject(start, end, commodity_name, quantity, limit, step, target, 1),
))
elif quantity >= limit:
return Wait(start, Choice(
Continue(start, end, commodity_name, quantity, limit, step, period, target),
Inject(start, end, commodity_name, quantity, limit, step, period, target, -1),
Continue(start, end, commodity_name, quantity, limit, step, target),
Inject(start, end, commodity_name, quantity, limit, step, target, -1),
))
else:
return Wait(start, Choice(
Continue(start, end, commodity_name, quantity, limit, step, period, target),
Inject(start, end, commodity_name, quantity, limit, step, period, target, 1),
Inject(start, end, commodity_name, quantity, limit, step, period, target, -1),
Continue(start, end, commodity_name, quantity, limit, step, target),
Inject(start, end, commodity_name, quantity, limit, step, target, 1),
Inject(start, end, commodity_name, quantity, limit, step, target, -1),
))
else:
if target < 0 or target == quantity:
return 0
0
else:
return BreachOfContract()
BreachOfContract()


@inline
def BreachOfContract():
-10000000000000000


@inline
def Continue(start, end, commodity_name, quantity, limit, step, period, target):
GasStorage(start + step, end, commodity_name, quantity, target, limit, step, period)
def Continue(start, end, commodity_name, quantity, limit, step, target):
GasStorage(start + step, end, commodity_name, quantity, target, limit, step)


@inline
def Inject(start, end, commodity_name, quantity, limit, step, period, target, vol):
Continue(start, end, commodity_name, quantity + vol, limit, step, period, target) - \
vol * Lift(commodity_name, period, Market(commodity_name))
def Inject(start, end, commodity_name, quantity, limit, step, target, vol):
Continue(start, end, commodity_name, quantity + vol, limit, step, target) - \
vol * Market(commodity_name)
71 changes: 0 additions & 71 deletions quantdsl/semantics.py
Expand Up @@ -1025,76 +1025,6 @@ def date(self):
return self._date


class Lift(DslExpression):
def validate(self, args):
self.assert_args_len(args, min_len=2, max_len=3)
# Name of a commodity to be perturbed.
self.assert_args_arg(args, posn=0, required_type=(String, Name))
if len(args) == 2:
# Expression to be perturbed.
self.assert_args_arg(args, posn=1, required_type=DslExpression)
elif len(args) == 3:
# Periodization of the perturbation.
self.assert_args_arg(args, posn=1, required_type=(String, Name))
# Expression to be perturbed.
self.assert_args_arg(args, posn=2, required_type=DslExpression)

@property
def commodity_name(self):
return self._args[0]

@property
def mode(self):
return self._args[1] if len(self._args) == 3 else String('alltime')

@property
def expr(self):
return self._args[-1]

# def identify_perturbation_dependencies(self, dependencies, **kwds):
# perturbation = self.get_perturbation(**kwds)
# dependencies.add(perturbation)
# super(Lift, self).identify_perturbation_dependencies(dependencies, **kwds)

def get_perturbation(self, **kwds):
try:
present_time = kwds['present_time']
except KeyError:
raise DslSyntaxError(
"'present_time' not found in evaluation kwds" % self.commodity_name,
", ".join(kwds.keys()),
node=self.node
)
commodity_name = self.commodity_name.evaluate(**kwds)
mode = self.mode.evaluate(**kwds)
if mode.startswith('alltime'):
perturbation = commodity_name
elif mode.startswith('year'):
perturbation = "{}-{}".format(commodity_name, present_time.year)
elif mode.startswith('mon'):
perturbation = "{}-{}-{}".format(commodity_name, present_time.year, present_time.month)
elif mode.startswith('da'):
perturbation = "{}-{}-{}-{}".format(commodity_name, present_time.year, present_time.month,
present_time.day)
else:
raise Exception("Unsupported mode: {}".format(mode))
return perturbation

def evaluate(self, **kwds):
# # Get the active perturbation, if set.
# active_perturbation = kwds.get('active_perturbation', None)
# perturbation_factor = kwds['perturbation_factor']
#
expr_value = self.expr.evaluate(**kwds)
# if active_perturbation and self.get_perturbation(**kwds) == active_perturbation.lstrip('-'):
# # If this perturbation is active, perturb the simulated value.
# sign = -1 if active_perturbation.startswith('-') else 1
# evaluated_value = expr_value * (1 + sign * perturbation_factor)
# else:
evaluated_value = expr_value
return evaluated_value


functionalDslClasses = {
'Add': Add,
'And': And,
Expand All @@ -1108,7 +1038,6 @@ def evaluate(self, **kwds):
'FunctionDef': FunctionDef,
'If': If,
'IfExp': IfExp,
'Lift': Lift,
'Max': Max,
'Min': Min,
'Mod': Mod,
Expand Down

0 comments on commit c110232

Please sign in to comment.