# Nonlinear least squares estimate

**URL:** https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241
**Category:** New to Julia
**Tags:** question
**Created:** [February 22, 2018, 1:00am UTC](https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241 "2018-02-22T01:00:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [February 22, 2018, 1:00am UTC](https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241/1 "2018-02-22T01:00:22Z")

</div>

I am trying to calibrate an option pricing model (nonlinear) with 3 parameters to estimate, but not sure how to do it in Julia.

```julia

function GKprice(S0, K, r, T, σ, d, Flag = "call") 

    normcdf(x) = cdf(Normal(0, 1), x)
    
    d1=(log(S0/K)+(r-d+σ*σ*0.5)*T)/(σ*sqrt(T))

    d2=d1-σ*sqrt(T)
    
    Price=0.0

    if Flag == "call"
        
        Price=S0*exp(-d*T)*normcdf(d1)-K*exp(-r*T)*normcdf(d2);
        
    else
        
        Price=-S0*exp(-d*T)*normcdf(-d1)+K*exp(-r*T)*normcdf(-d2);
    
    end
    
    return Price;
    
end

    function GCprice(spot, K, r, T, sigma, d, gamma1, gamma2, FlagIsCall)

        #FlagIsCall equals 1 for call option and 0 for put option 

        D = ( log(spot / K) + (r - d + 0.5 * sigma ^ 2 ) * T ) / ( sigma * sqrt(T) )
        
        dist = Normal(0, 1)

        parity = spot * exp(-d * T) - K * exp(-r * T)

        part1 = spot * exp(-d * T) * pdf(dist, D) * sigma * sqrt(T) 

        part2 = gamma1 * (2 * sigma * sqrt(T) - D ) / ( 6 * sqrt(T))

        part3 = gamma2 * (1 - D^2 + 3 * D * sigma * sqrt(T) - 3 * sigma^2 * T) / (24 * T)  

        adjustment = part1 * (part2 - part3) 
        

        Price = GKprice(spot, K, r, T, sigma, d) + adjustment - (FlagIsCall - 1) * parity

            return Price
        
    end

```

GCprice is my model and I made up the data sample as an example (no variation in some independent variables). The real data is a exchange rate option data.

```julia

df = DataFrame(FlagIsCall = rand(0:1, 100),
    K = collect(1941:0.2:1960.8), spot = 1943.09, d = 0.021589, T = 0.0219178, 
    r = 0.00390208, GC_price = rand(1:30, 100))

```

GC\_price is my dependent variable, my independent variables include K,  
spot, d, T and r. It has 3 parameters to estimate, which are sigma, gamma1, and gamm2. I want to do a least squared estimate for them, but have to write my code from scratch and use the Optim package to find the minimum SSE. I want to know if there is easy way to fit a nonlinear model in Julia. Thanks.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 22, 2018, 1:01am UTC](https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241/2 "2018-02-22T01:01:38Z")

</div>

LsqFit.jl

---

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [February 22, 2018, 2:14am UTC](https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241/3 "2018-02-22T02:14:35Z")

</div>

I read the doc, but I am not sure if it has multivariate estimate choice.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 22, 2018, 2:15am UTC](https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241/4 "2018-02-22T02:15:45Z")

</div>

It does. You just use an array and a closure.

---

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [February 22, 2018, 2:30am UTC](https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241/5 "2018-02-22T02:30:33Z")

</div>

Do you mean I should do it like

x = Array(Float64, (x1, x2, x3…))

x1 = x [1]

x2 = x [2]

x3 = x [3]

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [February 22, 2018, 2:42am UTC](https://discourse.julialang.org/t/nonlinear-least-squares-estimate/9241/6 "2018-02-22T02:42:04Z")

</div>

Yes. And just throw a function over it so that you send `f(p)` to send it to the package.
