Serving the Quantitative Finance Community

 
User avatar
bgimpert
Topic Author
Posts: 0
Joined: August 23rd, 2006, 3:22 pm

Pyfin, basic option pricing in Python

November 22nd, 2014, 6:04 pm

An open source basic options pricing library in Python:https://github.com/opendoor-labs/pyfinFeatures: Minimal dependencies, binomial trees, Longstaff-Schwartz. Pretty limited compared with QuantLib, but the Python-ness might be more convenient for some.
Last edited by bgimpert on November 21st, 2014, 11:00 pm, edited 1 time in total.
 
User avatar
bearish
Posts: 5186
Joined: February 3rd, 2011, 2:19 pm

Pyfin, basic option pricing in Python

November 22nd, 2014, 6:42 pm

For reasons that I don't fully understand, your link doesn't work. This should: pyfin.. Thanks for sharing.
 
User avatar
Cuchulainn
Posts: 20254
Joined: July 16th, 2004, 7:38 am
Location: 20, 000

Pyfin, basic option pricing in Python

November 22nd, 2014, 6:43 pm

QuoteOriginally posted by: bearishFor reasons that I don't fully understand, your link doesn't work. This should: pyfin.. Thanks for sharing.Instead ofhttp://https://github.com/opendoor-labs/pyfinshould behttp://github.com/opendoor-labs/pyfin
 
User avatar
bgimpert
Topic Author
Posts: 0
Joined: August 23rd, 2006, 3:22 pm

Pyfin, basic option pricing in Python

November 22nd, 2014, 8:03 pm

Thanks for the heads-up on the broken link. Something strange with the Wilmott Forum markup language, there. Fixed now!
 
JasmeetGujral
Posts: 2
Joined: June 18th, 2018, 2:55 pm

Re: Pyfin, basic option pricing in Python

June 18th, 2018, 3:06 pm

Trying to replicate pyfin code in python for longstaff schwartz. But getting incorrect results - American option values lower than European. Could someone please guide what I am possibly missing.

Code below:
   
 def option_payoff(self, stimulated_price):
        temp_zeros = np.zeros_like(stimulated_price)
        return np.maximum(self.option_flag * (stimulated_price - self.instrument.strike), temp_zeros)

    def LSM_model(self):
        _s_stimulated = self.stimulation_method()
        _intrinsic_val = self.option_payoff(self.stimulation_method())
        _option_value = np.zeros_like(_intrinsic_val)
        _option_value[:, -1] = _intrinsic_val[:, -1]

        for t in range(self.no_of_steps - 1, 0, -1):
            if len(_s_stimulated[_intrinsic_val[:, t] > 0, t]) == 0:
                continue
            else:

                _ITM_check = _intrinsic_val[:, t] > 0
                _x_axis = _s_stimulated[_ITM_check, t]
                _y_axis = _option_value[_ITM_check, t + 1] * self.step_disc_fact
                _option_value[:, t] = _option_value[:, t + 1] * self.step_disc_fact
                _rg = np.polyfit(_x_axis, _y_axis, 5)
                _pv_rg = np.polyval(_rg, _x_axis)

                _option_value[_ITM_check, t] = np.where(_intrinsic_val[_ITM_check, t] > _y_axis,
                                                        _intrinsic_val[_ITM_check, t], _y_axis)

        _option_value[:, 0] = _option_value[:, 1] * self.step_disc_fact
        return np.mean(_option_value[:, 0])